我使用自定义协议从Windows运行时应用程序打开另一个应用程序。 我正在使用以下代码片段:
await Launcher.LaunchUriAsync(new Uri("appb://hello"));
当有简单的string时,它工作正常。 但它传递一个JSONstring时会出现parsing错误。 Invalid URI: The hostname could not be parsed.
我正在创buildJSON:
JObject jObj = new JObject(); jObj.Add("Name", "abcdef"); jObj.Add("Address", "acvdfs"); string json = jObj.ToString();
它给JSON如下:
{“Name”:“abcdef”,“Address”:“acvdfs”}
你的问题是因为你发送的整个 json
字符串被创建为Uri。
您将需要首先从字符串中获取所需的值, 然后将这些值传递给您的方法。
作为一个例子,让我们说你的
JObject jObj = new JObject(); jObj.Add("Name", "abcdef"); jObj.Add("Address", "acvdfs"); string json = jObj.ToString();
代码会给你一个“ {Name}/{Address}
”输出 – appb://abcdef/acvdfs
而不是直接解析一个string
,你将需要首先获取值。
否则你的
{“Name”:“abcdef”,“Address”:“acvdfs”}
是什么造成的
无效的URI:主机名不能被分析。
错误。
你可以这样做,以便从该字符串中检索值:
var values = jObj.Properties().Select(x => x.Value.ToString()).ToArray(); // Gives you an array of the values. var path = string.Join("/", values); // Creates an "a/b" path by joining the array. await Launcher.LaunchUriAsync(new Uri("appb://" + path)); // Give that path to create the Uri and pass to your method.
这段代码假设你只用键来创建我所拥有的路径。 它将适用于任何数量的键,因为它只是用“/”将所有值连接在一起 – 例如a/b/c/d/e/f/g
等。
任何问题,只要问:)
希望这可以帮助!