草庐IT

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

coder 2024-01-23 原文

大家好,我正在使用 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 的新手,所以任何帮助都会很棒!

最佳答案

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

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

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

有关c# - NSUrl 请求在 Xamarin Today Widget 中不起作用的更多相关文章

  1. ruby-on-rails - Rails HTML 请求渲染 JSON - 2

    在我的Controller中,我通过以下方式在我的index方法中支持HTML和JSON:respond_todo|format|format.htmlformat.json{renderjson:@user}end在浏览器中拉起它时,它会自然地以HTML呈现。但是,当我对/user资源进行内容类型为application/json的curl调用时(因为它是索引方法),我仍然将HTML作为响应。如何获取JSON作为响应?我还需要说明什么? 最佳答案 您应该将.json附加到请求的url,提供的格式在routes.rb的路径中定义。这

  2. ruby-on-rails - 如果 Object::try 被发送到一个 nil 对象,为什么它会起作用? - 2

    如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象

  3. ruby-on-rails - s3_direct_upload 在生产服务器中不工作 - 2

    在Rails4.0.2中,我使用s3_direct_upload和aws-sdkgems直接为s3存储桶上传文件。在开发环境中它工作正常,但在生产环境中它会抛出如下错误,ActionView::Template::Error(noimplicitconversionofnilintoString)在View中,create_cv_url,:id=>"s3_uploader",:key=>"cv_uploads/{unique_id}/${filename}",:key_starts_with=>"cv_uploads/",:callback_param=>"cv[direct_uplo

  4. c# - 如何在 ruby​​ 中调用 C# dll? - 2

    如何在ruby​​中调用C#dll? 最佳答案 我能想到几种可能性:为您的DLL编写(或找人编写)一个COM包装器,如果它还没有,则使用Ruby的WIN32OLE库来调用它;看看RubyCLR,其中一位作者是JohnLam,他继续在Microsoft从事IronRuby方面的工作。(估计不会再维护了,可能不支持.Net2.0以上的版本);正如其他地方已经提到的,看看使用IronRuby,如果这是您的技术选择。有一个主题是here.请注意,最后一篇文章实际上来自JohnLam(看起来像是2009年3月),他似乎很自在地断言RubyCL

  5. jquery - 我的 jquery AJAX POST 请求无需发送 Authenticity Token (Rails) - 2

    rails中是否有任何规定允许站点的所有AJAXPOST请求在没有authenticity_token的情况下通过?我有一个调用Controller方法的JqueryPOSTajax调用,但我没有在其中放置任何真实性代码,但调用成功。我的ApplicationController确实有'request_forgery_protection'并且我已经改变了config.action_controller.consider_all_requests_local在我的environments/development.rb中为false我还搜索了我的代码以确保我没有重载ajaxSend来发送

  6. C# 到 Ruby sha1 base64 编码 - 2

    我正在尝试在Ruby中复制Convert.ToBase64String()行为。这是我的C#代码:varsha1=newSHA1CryptoServiceProvider();varpasswordBytes=Encoding.UTF8.GetBytes("password");varpasswordHash=sha1.ComputeHash(passwordBytes);returnConvert.ToBase64String(passwordHash);//returns"W6ph5Mm5Pz8GgiULbPgzG37mj9g="当我在Ruby中尝试同样的事情时,我得到了相同sha

  7. 基于C#实现简易绘图工具【100010177】 - 2

    C#实现简易绘图工具一.引言实验目的:通过制作窗体应用程序(C#画图软件),熟悉基本的窗体设计过程以及控件设计,事件处理等,熟悉使用C#的winform窗体进行绘图的基本步骤,对于面向对象编程有更加深刻的体会.Tutorial任务设计一个具有基本功能的画图软件**·包括简单的新建文件,保存,重新绘图等功能**·实现一些基本图形的绘制,包括铅笔和基本形状等,学习橡皮工具的创建**·设计一个合理舒适的UI界面**注明:你可能需要先了解一些关于winform窗体应用程序绘图的基本知识,以及关于GDI+类和结构的知识二.实验环境Windows系统下的visualstudio2017C#窗体应用程序三.

  8. ruby-on-rails - "assigns"在 Ruby on Rails 中有什么作用? - 2

    我目前正在尝试学习RubyonRails和测试框架RSpec。assigns在此RSpec测试中做什么?describe"GETindex"doit"assignsallmymodelas@mymodel"domymodel=Factory(:mymodel)get:indexassigns(:mymodels).shouldeq([mymodel])endend 最佳答案 assigns只是检查您在Controller中设置的实例变量的值。这里检查@mymodels。 关于ruby-o

  9. ruby - HTTP 请求中的用户代理,Ruby - 2

    我是Ruby的新手。我试过查看在线文档,但没有找到任何有效的方法。我想在以下HTTP请求botget_response()和get()中包含一个用户代理。有人可以指出我正确的方向吗?#PreliminarycheckthatProggitisupcheck=Net::HTTP.get_response(URI.parse(proggit_url))ifcheck.code!="200"puts"ErrorcontactingProggit"returnend#Attempttogetthejsonresponse=Net::HTTP.get(URI.parse(proggit_url)

  10. ruby-on-rails - 获取并发布相同匹配项的请求 - 2

    在我的路线文件中我有:match'graphs/(:id(/:action))'=>'graphs#(:action)'如果是GET请求(工作)或POST请求(不工作),我想匹配它我知道我可以使用以下方法在资源中声明POST请求:post'/'=>:show,:on=>:member但是我怎样才能为比赛做到这一点呢?谢谢。 最佳答案 如果你同时想要POST和GETmatch'graphs/(:id(/:action))'=>'graphs#(:action)',:via=>[:get,:post]编辑默认值可以设置如下match'g

随机推荐