Xamarin.Mac URL方案

jon*_*ers 4 c# macos monomac xamarin xamarin.mac

如何使用Xamarin.Mac设置和调试URL方案?

我在笔记上添加了以下内容Info.plist:

的Info.plist

然后我构建了一个安装程序包并安装了应用程序.但是,如果我mytest://在浏览器中打开或运行open mytest://命令行,则不会启动我的应用程序.

另外,有没有办法在运行后在Xamarin Studio中附加调试器mytest://?在Windows上我会使用Debugger.Break,Debugger.Attach但这些方法似乎没有在Mono中实现.

The*_*man 5

它没有直接解决您的问题,但这个问题的答案对您有帮助吗?

具体来说,它使用项目上的自定义执行命令选项进行寻址.您可以定义自定义命令以在调试器中执行您的应用程序:

打开"项目选项",进入"运行>自定义命令"部分,为"执行"添加自定义命令

它还提到了Debugger.Break行为:

如果您的应用程序在带有Mono 2.11或更高版本的Mono Soft Debugger中运行[...],它将为软调试器设置软断点并按预期工作


编辑:

您可以在已经运行的Mac应用程序上调用URL ...您是否可以设置处理程序来捕获事件,在内部设置断点并检查您的URL是否正确调用已在运行的应用程序?它可能会为您提供行为的线索或进一步调试的方法.像这样的东西:

    public override void FinishedLaunching(NSObject notification)
    {
        NSAppleEventManager appleEventManager = NSAppleEventManager.SharedAppleEventManager;

        appleEventManager.SetEventHandler(this, new Selector("handleGetURLEvent:withReplyEvent:"), AEEventClass.Internet, AEEventID.GetUrl);
    }

    [Export("handleGetURLEvent:withReplyEvent:")]
    private void HandleGetURLEvent(NSAppleEventDescriptor descriptor, NSAppleEventDescriptor replyEvent)
    {
        // Breakpoint here, debug normally and *then* call your URL
    }
Run Code Online (Sandbox Code Playgroud)


Bra*_*ore 5

正如@TheNextman所发布的那样,该解决方案确实有效,但这是一个更完整的解决方案。我从以下信息 Xamarin论坛线程。作为用户(和Xamarin员工)Sebastien Pouliot(@poupou)指出,

我从未使用过该特定的API,但枚举值中的四个字符在Apple API中很常见。

四个字符(4个字节)被编译为整数。如果没有可用的C#枚举,则可以将字符串转换为以下代码的整数:

public static int FourCC (string s) {
    return (((int)s [0]) << 24 |
        ((int)s [1]) << 16 |
        ((int)s [2]) << 8 |
        ((int)s [3]));
}
Run Code Online (Sandbox Code Playgroud)

因此完整的示例如下

public override void FinishedLaunching(NSObject notification)
{
    NSAppleEventManager.SharedAppleEventManager.SetEventHandler(this, new Selector("handleGetURLEvent:withReplyEvent:"), AEEventClass.Internet, AEEventID.GetUrl);
}

[Export("handleGetURLEvent:withReplyEvent:")]
private void HandleGetURLEvent(NSAppleEventDescriptor descriptor, NSAppleEventDescriptor replyEvent)
{
    string keyDirectObject = "----";
    uint keyword = (((uint)keyDirectObject[0]) << 24 |
                   ((uint)keyDirectObject[1]) << 16 |
                   ((uint)keyDirectObject[2]) << 8 |
                   ((uint)keyDirectObject[3]));
    string urlString = descriptor.ParamDescriptorForKeyword(keyword).StringValue;
}
Run Code Online (Sandbox Code Playgroud)