【问题标题】:Foundation.ModelNotImplementedException for Local Notification in Xamarin FormsXamarin 表单中本地通知的 Foundation.ModelNotImplementedException
【发布时间】:2017-03-16 16:55:58
【问题描述】:

我有 PCL 项目,我需要显示简单的通知(当任务完成时显示简单的字符串)。在 iOS 项目中,当我尝试在 ReceivedLocalNotification 中显示通知时:

Window.RootViewController.PresentViewController(okayAlertController, true, null);

引发异常:Foundation.ModelNotImplementedException:引发了“Foundation.ModelNotImplementedException”类型的异常。这是一个代码:

public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
    public override bool FinishedLaunching(UIApplication app, NSDictionary options)
    {
        ZXing.Net.Mobile.Forms.iOS.Platform.Init();
        global::Xamarin.Forms.Forms.Init();
        LoadApplication(new App());

        if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
        {
            var notificationSettings = UIUserNotificationSettings.GetSettingsForTypes(
                UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound, null
            );

            app.RegisterUserNotificationSettings(notificationSettings);

            // check for a notification
            if (options != null)
            {
                // check for a local notification
                if (options.ContainsKey(UIApplication.LaunchOptionsLocalNotificationKey))
                {
                    var localNotification = options[UIApplication.LaunchOptionsLocalNotificationKey] as UILocalNotification;
                    if (localNotification != null)
                    {
                        UIAlertController okayAlertController = UIAlertController.Create(localNotification.AlertAction, localNotification.AlertBody, UIAlertControllerStyle.Alert);
                        okayAlertController.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));

                        Window.RootViewController.PresentViewController(okayAlertController, true, null);

                        UIApplication.SharedApplication.ApplicationIconBadgeNumber = 0;
                    }
                }
            }

        }

        base.FinishedLaunching(app, options);
    }

    public override void ReceivedLocalNotification(UIApplication application, UILocalNotification notification)
    {
        // show an alert
        UIAlertController okayAlertController = UIAlertController.Create(notification.AlertAction, notification.AlertBody, UIAlertControllerStyle.Alert);
        okayAlertController.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));

        Window.RootViewController.PresentViewController(okayAlertController, true, null);

        UIApplication.SharedApplication.ApplicationIconBadgeNumber = 0;
    }
}

还有 NotificationService_iOS.cs:

public class NotificationService_iOS : INotificationService
{
    public void Notify(string title, string text)
    {
        var notification = new UILocalNotification();
        notification.FireDate = NSDate.FromTimeIntervalSinceNow(0); // Fire now
        notification.AlertAction = title;
        notification.AlertBody = text;
        notification.ApplicationIconBadgeNumber = 1;
        notification.SoundName = UILocalNotification.DefaultSoundName;
        UIApplication.SharedApplication.ScheduleLocalNotification(notification);
    }
}

【问题讨论】:

  • 我得到了完全相同的错误,使用直接取自 Xamarin 本地通知教程的代码

标签: xamarin xamarin.ios xamarin.forms


【解决方案1】:

window对象不可用,需要使用

var window = UIApplication.SharedApplication.KeyWindow;

在这里查看答案problem with Window object

【讨论】:

    猜你喜欢
    • 2015-12-11
    • 1970-01-01
    • 1970-01-01
    • 2018-05-04
    • 1970-01-01
    • 1970-01-01
    • 2020-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多