OStack程序员社区-中国程序员成长平台

标题: c# - NSUrl 请求在 Xamarin Today 小部件中不起作用 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 10:09
标题: c# - NSUrl 请求在 Xamarin Today 小部件中不起作用

嘿伙计们,我正在使用 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 开发的新手,所以任何帮助都会很棒!



Best Answer-推荐答案


扩展有自己的打开 URL 的方式(因为在扩展上打开 URL 时设备可能会被锁定),所以你需要使用 ExtensionContext

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

关于c# - NSUrl 请求在 Xamarin Today 小部件中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32275996/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4