We'd normally use Egors Toasts plugin, but as it requires permissions on iOS for a current project we've gone a different route using Rg.Plugins.Popup nuget (https://github.com/rotorgames/Rg.Plugins.Popup).
这是我改进的 ShowAlert版本的伊恩沃伯顿的版本,以确保吐司是显示甚至在弹出页面。
此外,如果用户单击吐司外部,吐司将被解除。
I used UIAlertControllerStyle.ActionSheet that look likes toast but it also work with UIAlertControllerStyle.Alert
After some search, I came across this unrelated answer which helped. The poster commented "This looks stupid but works", which is right on both counts.
因此,我用这些代码行修改了上面的 ShowAlert ()函数,它们似乎可以工作:
var rootVC = UIApplication.SharedApplication.KeyWindow.RootViewController;
while ( rootVC.PresentedViewController != null) {
rootVC = rootVC.PresentedViewController;
}
rootVC.PresentViewController( alert, true, null);
var toastConfig = new ToastConfig("Toasting...");
toastConfig.SetDuration(3000);
toastConfig.SetBackgroundColor(System.Drawing.Color.FromArgb(12, 131, 193));
UserDialogs.Instance.Toast(toastConfig);
ToastEvent toastEvent = new ToastEvent();
var toastConfig = new ToastConfig(toastEvent,"Toasting...","");
toastConfig.SetDuration(2000);
UserDialogs.Instance.Toast(toastConfig);
...
var vm = new MyPopUpViewModel()
vm.DisplayText = "this could be your text";
vm.DelayTimeForDismiss = 5000;
vm.IsLightDismissAllowed = false; //used that you can not close the popup by clicking around
await vm.OpenPopupAsync();
然后在您的 vm OpenPopUpAsync ()中
...other properties and stuff
internal PopUpLoadingView popup = new PopUpLoadingView();//this is the view created with the informations from MS
public async Task OpenPopUp()
{
popup.BindingContext = this;
Application.Current.MainPage.Navigation.ShowPopup(popup: popup);
await Task.Delay(DelayTimeForDismiss);
popup.DismissThisPopup();
}
并在 PopUpLoadingView 中插入以下方法:
...other stuff
public void DismissThisPopup()
{
Dismiss(this);
}