NSUrl请求在Xamarin Today Widget中不起作用

时间:2015-08-28 16:47:49

标签: c# ios xamarin.ios nsurl

嘿伙计们我正在使用C#开发Xamarin的Today Widget,它打开了iPhone上安装的某个应用程序。我使用NSUrl请求执行此操作但是当我运行窗口小部件并单击按钮时它表示无法加载

这是我的代码:

 using Foundation;
using System;
using System.CodeDom.Compiler;
using UIKit;
using NotificationCenter;

namespace ToolBelt
{
    partial class ToolBeltView : UIViewController
    {
        public ToolBeltView (IntPtr handle) : base (handle)
        {
        }
        public override void ViewWillAppear (bool animated)
        {
            base.ViewWillAppear (animated);

            var controller = NCWidgetController.GetWidgetController ();
            controller.SetHasContent(true, "com.d4a.toolbelt.Widget");
        }

        public override void ViewDidLoad ()
        {
            base.ViewDidLoad ();

            webbutton.TouchUpInside += delegate {
                NSUrl request = new NSUrl ("http://www.google.com");

                try {
                    UIApplication.SharedApplication.OpenUrl (request);
                } catch (Exception ex) {
                    Console.WriteLine ("Cannot open url: {0}, Error: {1}", request.AbsoluteString, ex.Message);
                    var alertView = new UIAlertView ("Error", ex.Message, null, "OK", null);

                    alertView.Show ();
                }
            };

            emailbutton.TouchUpInside += delegate {
                NSUrl request = new NSUrl ("mailto:");

                try {
                    UIApplication.SharedApplication.OpenUrl (request);
                } catch (Exception ex) {
                    Console.WriteLine ("Cannot open url: {0}, Error: {1}", request.AbsoluteString, ex.Message);
                    var alertView = new UIAlertView ("Error", ex.Message, null, "OK", null);

                    alertView.Show ();
                }
            };

            musicbutton.TouchUpInside += delegate {
                NSUrl request = new NSUrl ("music://");

                try {
                    UIApplication.SharedApplication.OpenUrl (request);
                } catch (Exception ex) {
                    Console.WriteLine ("Cannot open url: {0}, Error: {1}", request.AbsoluteString, ex.Message);
                    var alertView = new UIAlertView ("Error", ex.Message, null, "OK", null);

                    alertView.Show ();
                }
            };


            itunesbutton.TouchUpInside += delegate {
                NSUrl request = new NSUrl ("http://itunes.apple.com/genre/music/id36?mt=8");

                try {
                    UIApplication.SharedApplication.OpenUrl (request);
                } catch (Exception ex) {
                    Console.WriteLine ("Cannot open url: {0}, Error: {1}", request.AbsoluteString, ex.Message);
                    var alertView = new UIAlertView ("Error", ex.Message, null, "OK", null);

                    alertView.Show ();
                }
            };


            appstorebutton.TouchUpInside += delegate {
                NSUrl request = new NSUrl ("http://itunes.apple.com/genre/mobile-software-applications/id36?mt=8");

                try {
                    UIApplication.SharedApplication.OpenUrl (request);
                } catch (Exception ex) {
                    Console.WriteLine ("Cannot open url: {0}, Error: {1}", request.AbsoluteString, ex.Message);
                    var alertView = new UIAlertView ("Error", ex.Message, null, "OK", null);

                    alertView.Show ();
                }
            };

        }

    }

}

我是Xamarin Development的新手,所以任何帮助都会令人惊叹!

1 个答案:

答案 0 :(得分:1)

扩展程序有自己的打开URL的方式(因为在扩展程序上打开URL时可能会锁定设备),因此您需要使用ExtensionContext

ExtensionContext.OpenUrl(new NSUrl("yoururl://"), null);