c# - NSUrl 请求在 Xamarin Today 小部件中不起作用
<p><p>嘿伙计们,我正在使用 C# 在 Xamarin 中开发一个 Today Widget,它打开了安装在 iPhone 上的某个应用程序。我正在使用 NSUrl 请求来执行此操作,但是当我运行小部件并单击显示无法加载的按钮时</p>
<p>这是我的代码:</p>
<pre><code> 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 ();
}
};
}
}
}
</code></pre>
<p>我是 Xamarin 开发的新手,所以任何帮助都会很棒!</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>扩展有自己的打开 URL 的方式(因为在扩展上打开 URL 时设备可能会被锁定),所以你需要使用 ExtensionContext</p>
<p><code>ExtensionContext.OpenUrl(new NSUrl("yoururl://"), null);</code></p></p>
<p style="font-size: 20px;">关于c# - NSUrl 请求在 Xamarin Today 小部件中不起作用,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/32275996/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/32275996/
</a>
</p>
页:
[1]