【发布时间】:2017-03-24 11:57:38
【问题描述】:
我在我的 Xamarin PCL 项目中创建了一个 Toast 通知。我使用this 创建了这个控件。一旦这个 toast 消息消失,我的应用程序就会变成空白。我不知道为什么?哪里都不例外??
便携式:
namespace ABC
{
public interface IMessage
{
void LongAlert(string message);
void ShortAlert(string message);
}
}
在机器人中:
public class MessageAndroid : IMessage
{
public void LongAlert(string message)
{
Toast.MakeText(Application.Context, message, ToastLength.Long).Show();
}
public void ShortAlert(string message)
{
Toast.MakeText(Application.Context, message, ToastLength.Short).Show();
}
}
在 Windows 10 中:
public class ToastNotificationManagerRenderer : IMessage
{
public void LongAlert(string message)
{
var notificationXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText01);
var toeastElement = notificationXml.GetElementsByTagName("text");
toeastElement[0].AppendChild(notificationXml.CreateTextNode(message));
var toastNotification = new ToastNotification(notificationXml);
ToastNotificationManager.CreateToastNotifier().Show(toastNotification);
}
public void ShortAlert(string message)
{
var notificationXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText01);
var toeastElement = notificationXml.GetElementsByTagName("text");
toeastElement[0].AppendChild(notificationXml.CreateTextNode(message));
var toastNotification = new ToastNotification(notificationXml);
ToastNotificationManager.CreateToastNotifier().Show(toastNotification);
}
}
在 iOS 中:
public class MessageIOS : IMessage
{
const double LONG_DELAY = 3.5;
const double SHORT_DELAY = 2.0;
NSTimer alertDelay;
UIAlertController alert;
public void LongAlert(string message)
{
ShowAlert(message, LONG_DELAY);
}
public void ShortAlert(string message)
{
ShowAlert(message, SHORT_DELAY);
}
void ShowAlert(string message, double seconds)
{
alertDelay = NSTimer.CreateScheduledTimer(seconds, (obj) =>
{
dismissMessage();
});
alert = UIAlertController.Create(null, message, UIAlertControllerStyle.Alert);
UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(alert, true, null);
}
void dismissMessage()
{
if (alert != null)
{
alert.DismissViewController(true, null);
}
if (alertDelay != null)
{
alertDelay.Dispose();
}
}
}
对于 Ios:我也试过这个插件。MessageBarLib。可移植但在 Ios 中的代码与上述相同 -
public void ShortAlert(string message)
{
MessageBarManager.SharedInstance.ShowMessage("Success", message, MessageType.Success);
}
但退出上述功能后,我的应用关闭了。
【问题讨论】:
-
理想情况下,我们可以使用一些代码来查看,但我建议当模式通知从视图中删除时,基本 UIViewController 可能正在运行它的“ViewWillAppear”或“ViewDidLoad”方法我冒昧地猜测你里面有一些它不喜欢的东西。如果您可以给我们您的代码示例而不是您的工作内容,那么确认这一点会更容易。
-
每个版本都关闭吗? IE。 iOS、Android 和 Windows?我可以在您的 iOS 课程中看到一些可能会混淆的地方,但让我们尝试进一步缩小范围。
-
@Digitalsa1nt 仅在 IOS 中发生。
-
@Digitalsa1nt 看看我的回答。
标签: ios xamarin xamarin.ios