dan*_*uso 7 c# macos url-scheme xamarin
我正在尝试从 url (例如“myapp://somestring”)启动我的 C# 应用程序,并且我已经能够做到这一点,但是我仍然无法理解如何读取该 url 的“somestring”值应该传递给应用程序。
我尝试了以下代码,但没有任何结果:
static void Main (string[] args){
foreach (string arg in args) {
Console.Writeline(arg);
}
}
Run Code Online (Sandbox Code Playgroud)
要知道,该应用程序是使用 xamarin for mac 完成的。
预先感谢您的任何帮助
Rez*_*aei 10
问题是寻找适用于 macOS 的解决方案,但提供的答案不适用于此。但我会将答案保留在这里以供将来参考,供那些在搜索引擎中找到该帖子并寻找适用于 Windows 的解决方案的人参考。
有时您希望有一个自定义 URL 方案,例如mailto:或skype:来处理一些自定义链接。为此,您可以将应用程序注册到注册表中的 URI 方案,并创建一个运行来处理对该 url 方案的请求的应用程序。
我创建了一个示例来演示该功能。创建此示例是为了处理myapp:url 方案并显示一个消息框,其中包含通过 url 传递到应用程序的值。
该示例包含 2 个项目:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace UrlSchemeSample
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var args = "";
if (Environment.GetCommandLineArgs().Length > 1)
args = Environment.GetCommandLineArgs()[1];
MessageBox.Show($"You can decide what to do with the arguments:\n{args}");
Application.Run(new Form1());
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想您想要创建myappurl 方案并拥有一个应用程序,c:\myapp.exe您希望在其中使用您的应用程序处理 url 方案。然后你应该在registry/l中创建这些键和值
HKEY_CLASSES_ROOT
myapp
(Default) = "URL:myapp"
URL Protocol = ""
DefaultIcon
(Default) = "c:\myapp.exe",0
shell
open
command
(Default) = "c:\myapp.exe" "%1"
Run Code Online (Sandbox Code Playgroud)
Environment.GetCommandLineArgs()然后,您可以使用并解析参数来获取通过 url 传递给应用程序的值。
例如myapp:Hello world!,应用程序的命令行参数是url myapp:Hello world!,您可以解析它并从参数中提取所需的信息。
举个例子,你可以有一些像这样的 url:myapp:show?form=form1¶m1=something。然后您可以解析该命令并执行您需要的操作。
1. Windows 窗体应用程序在该项目中的作用是什么?
当用户单击已注册方案的 url 时,应用程序将打开,并且 url 将作为命令行参数传递给应用程序。然后您可以解析参数并执行您需要的操作。
2.Setup项目的作用是什么?
它安装处理 url 方案的应用程序。它还使用合适的值在 Windows 注册表中注册 url 方案。
您也可以使用 C# 代码创建这些注册表项和值,而不是使用安装程序项目,但使用安装程序项目更方便。如果您没有 Visual Studio 2017 安装项目模板,可以在此处下载。
| 归档时间: |
|
| 查看次数: |
10064 次 |
| 最近记录: |