Registering Custom Protocol for Windows Phone 8 Ap

2020-06-29 02:25发布

问题:

I'm searching for a way to register a custom protocol for Windows Phone 8. I tried to register a custom protocol with WebRequest.RegisterPrefix("xxx://", this) but this will not work for the WebBrowser control. Since our protocol scheme is no valid uri the app will just crash if it try to navigate (via location.url='xxx://this-is-no-valid-uri').

I hope anyone can spread light into the darkness ;)

EDIT: Here is my test project. I think this is a major bug because you can force every app to crash with just a simple JavaScript statement.

回答1:

Recently i had to create a custom uri scheme,and that was pretty easy in your WMAppManifest add:

<Extensions>
  <Protocol Name="XXXX" NavUriFragment="encodedLaunchUri=%s" TaskID="_default" />
</Extensions>

this is stands after Tokens

than in your App.cs file you redirect to the class which is responsible for the navigation

RootFrame.UriMapper = new CustomUriMapper();

for example

class CustomUriMapper : UriMapperBase
{
    private string tempUri;
    public override Uri MapUri(Uri uri)
    {
        tempUri = System.Net.HttpUtility.UrlDecode(uri.ToString());

        if (tempUri.Contains("XXXX"))
        {
         return new Uri("/MainPage.xaml?parameter=XXXX", UriKind.Relative);
        }
        else
            {
                return new Uri("/MainPage.xaml", UriKind.Relative);
            }           
    }
} 

i hope i helped

EDIT

i have create a small project, and i'm sure it works, please try this

            string url = "xxxx:";
            WebBrowserTask browser = new WebBrowserTask();
            browser.URL = url;
            browser.Show();


回答2:

I finally got a Solution for my Problem, you need to register a own UriParser.

My UriParser:

public class MyUriParser : UriParser
{
    public MyUriParser()
    {

    }

    protected override string GetComponents(Uri uri, UriComponents components, UriFormat format)
    {
        return "";
    }
    protected override bool IsWellFormedOriginalString(Uri uri)
    {
        return true;
    }
    protected override void InitializeAndValidate(Uri uri, out UriFormatException parsingError)
    {
        parsingError = null;
    }
    protected override bool IsBaseOf(Uri baseUri, Uri relativeUri)
    {
        return false;
    }
    protected override string Resolve(Uri baseUri, Uri relativeUri, out UriFormatException parsingError)
    {
        parsingError = null;
        return "";
    }
}

Registered via:

if (!UriParser.IsKnownScheme(SCHEMENAME_0))
    UriParser.Register(new MyUriParser(), SCHEMENAME_0, 80);