从Javascript Metro App Win8中的URI获取参数信息

时间:2021-12-05 19:34:31

I'm developing a JavaScript Metro app in Visual Studio 2012 and Win8.1

我正在开发Visual Studio 2012和Win8.1中的JavaScript Metro应用程序

I call my app using a URI on any webBrowser like:

我在任何webBrowser上使用URI调用我的应用程序,如:

myapp:///

but I want to send several parameters like this

但我想发送这样的几个参数

myapp://parameters/?p1="hello"&p2="Jesus"

and recover the values inside my app for several purposes, is it possible?? how to do that??

为了几个目的恢复我的应用程序内的值,是否可能?怎么做??

thanks in advance

提前致谢


EDIT:

编辑:

this is the code I got so far but still not working:

这是我到目前为止的代码,但仍然无法正常工作:

app.onactivated = function (args) {


    if (args.detail.kind === activation.ActivationKind.launch) {
        ...
    }

        //Handles URI activation (by Protocol)
    else if (args.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.protocol) {
        // Displays on screen the URI parameters
        console.log("the parameters: " + args.detail.uri.queryParsed);

        args.setPromise(WinJS.UI.processAll());
    }
};

从Javascript Metro App Win8中的URI获取参数信息

it says (translated from spanish):

它说(翻译自西班牙语):

The value of this expression may not be correct. Unable to evaluate because 'An identifier was expected' Click this button to attempt evaluation now

此表达式的值可能不正确。无法评估,因为“标识符已被预期”单击此按钮可立即尝试评估

this is the full error

这是完整的错误

"TypeError: GetAt: invalid argument \n at onactivated (ms-appx://7f50cf46-2f92-4088-b44e-2da6ccd24a08/js/default.js:68:13)\n at wrapper (ms-appx://microsoft.winjs.1.0/js/base.js:549:61)\n at dispatchOne (ms-appx://microsoft.winjs.1.0/js/base.js:6987:25)\n at dispatchEvent (ms-appx://microsoft.winjs.1.0/js/base.js:6986:21)\n
at drainQueue (ms-appx://microsoft.winjs.1.0/js/base.js:7038:9)\n at queueEvent (ms-appx://microsoft.winjs.1.0/js/base.js:7057:13)\n at Anonymous function (ms-appx://microsoft.winjs.1.0/js/base.js:7110:13)\n at CompletePromise_then (ms-appx://microsoft.winjs.1.0/js/base.js:1790:21)\n at activatedHandler (ms-appx://microsoft.winjs.1.0/js/base.js:7109:9)"

“TypeError:GetAt:无效参数\ n atactivated(ms-appx://7f50cf46-2f92-4088-b44e-2da6ccd24a08/js/default.js:68:13)\ n at wrapper(ms-appx:// microsoft .winjs.1.0 / js / base.js:549:61)\ n在dispatchOne(ms-appx://microsoft.winjs.1.0/js/base.js:6987:25)\ n在dispatchEvent(ms-appx) ://microsoft.winjs.1.0/js/base.js:6986:21)\ n在drainQueue(ms-appx://microsoft.winjs.1.0/js/base.js:7038:9)\ n在queueEvent (ms-appx://microsoft.winjs.1.0/js/base.js:7057:13)\ n匿名函数(ms-appx://microsoft.winjs.1.0/js/base.js:7110:13 )\ n在CompletePromise_then(ms-appx://microsoft.winjs.1.0/js/base.js:1790:21)\ n at activatedHandler(ms-appx://microsoft.winjs.1.0/js/base.js :7109:9)”


EDIT2: This is what I did to get the closest thing to my answer so far URI

EDIT2:这就是我迄今为止最接近我的答案的做法

app.onactivated = function (args) {
    if (args.detail.kind === activation.ActivationKind.launch) {
         ...

        // Handles URI activation
        WinJS.Application.addEventListener("activated", onActivatedHandler, false);
        args.setPromise(WinJS.UI.processAll());
    }
};

function onActivatedHandler(eventArgs) {
    if (eventArgs.detail.kind == Windows.ApplicationModel.Activation.ActivationKind.protocol) {

        var uri = String(eventArgs.detail.uri.rawUri);

        var par1    = uri.substring(uri.indexOf("?p1=") + 7, uri.indexOf("&p2=") -3);
        var par2    = uri.substring(uri.indexOf("&p2=") + 7, uri.length - 3);

        Windows.UI.Popups.MessageDialog("parameter1: " + par1 + ", and the parameter2: " + par2).showAsync();
    }
}

this shows a popup which says:

这显示了一个弹出窗口,上面写着:

title
parameter1: hello, and the parameter2: Jesus 

I Expected to get an array or dictionary of parameters...

我希望得到一个数组或参数字典......

1 个解决方案

#1


1  

Yes, it's possible. First check if your app has been activated by protocol (eventArgs.detail.kind == Windows.ApplicationModel.Activation.ActivationKind.protocolol assuming eventArgs are activation event args) then you can access parsed query string object via :

是的,这是可能的。首先检查您的应用程序是否已通过协议激活(eventArgs.detail.kind == Windows.ApplicationModel.Activation.ActivationKind.protocolol,假设eventArgs是激活事件参数),然后您可以通过以下方式访问已解析的查询字符串对象:

eventArgs.detail.uri.queryParsed

Hope that helps.

希望有所帮助。

#1


1  

Yes, it's possible. First check if your app has been activated by protocol (eventArgs.detail.kind == Windows.ApplicationModel.Activation.ActivationKind.protocolol assuming eventArgs are activation event args) then you can access parsed query string object via :

是的,这是可能的。首先检查您的应用程序是否已通过协议激活(eventArgs.detail.kind == Windows.ApplicationModel.Activation.ActivationKind.protocolol,假设eventArgs是激活事件参数),然后您可以通过以下方式访问已解析的查询字符串对象:

eventArgs.detail.uri.queryParsed

Hope that helps.

希望有所帮助。