首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Xamarin表格局部通知的Foundation.ModelNotImplementedException

Xamarin表格局部通知的Foundation.ModelNotImplementedException
EN

Stack Overflow用户
提问于 2017-03-10 11:01:45
回答 1查看 1.8K关注 0票数 1

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

代码语言:javascript
复制
Window.RootViewController.PresentViewController(okayAlertController, true, null);

抛出异常: Foundation.ModelNotImplementedException:抛出类型为“Foundation.ModelNotImplementedException”的异常。这是一个代码:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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);
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-16 08:55:58

窗口对象不可用,需要使用

var window = UIApplication.SharedApplication.KeyWindow;

见这里的答案,窗口对象问题

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42716781

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档