Registering as a Lens

,

To register your app as a lens, you need to add an Extension element to the app’s WMAppManifest.xml file. Open the WMAppManifest.xml file by right-clicking the file and selecting View Code. If not already present, add an Extensions element immediately after the Tokens element. Within the Extensions element add an Extension element, as shown:

<Extensions>
  <Extension ExtensionName="Camera_Capture_App"
             ConsumerID="{5B04B775-356B-4AA0-AAF8-6491FFEA5631}"
             TaskID="_default" />
</Extensions>

Once in place, your app is displayed on the lens picker page.

Tapping on your app’s icon in the lens picker page launches your app to its main page.

To determine whether the launch is from the lens picker page, examine the query string of the URI that was used to launch your app. This can be done in a page’s OnNavigatedTo method, as demonstrated in the following example:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    ...
    string action;
    if (NavigationContext.QueryString.TryGetValue("Action", out action)
            && action == "ViewfinderLaunch")
    {
        /* Launched from the lens picker. */
    }
}

To redirect to a particular page within your app, use a custom URI mapper. A URI mapper examines the requested URI for every navigation request and may redirect accordingly.


Note

Ordinarily you could place a URI mapping into your app’s App.xaml file, but because the lens app deep link URI uses a query string, and the UriMapping class does not support mapping based on query strings, a custom URI mapper class is required.


Listing 21.14 shows a custom URI mapper named UnleashedUriMapper, which can be used in the same manner as the SDKs UriMapper class. In particular, the class allows you to define URI mappings in XAML in addition to providing mapping logic within the class itself.

The UnleashedUriMapper class’s MapUri method attempts to locate a mapping from the UriMappings collections. In the case of the app being launched from the lens picker page, no such mapping is found, and the query string is used to redirect the app to the PhotoCameraView.xaml page.

LISTING 21.14. UnleashedUriMapper Class


[ContentProperty("UriMappings")]
public sealed class UnleashedUriMapper : UriMapperBase
{
    public Collection<UriMapping> UriMappings { get; private set; }

    public UnleashedUriMapper()
    {
        UriMappings = new Collection<UriMapping>();
    }

    public override Uri MapUri(Uri uri)
    {
        ArgumentValidator.AssertNotNull(uri, "uri");

        Collection<UriMapping> uriMappings = UriMappings;

        foreach (UriMapping mapping in uriMappings)
        {
            Uri mappedUri = mapping.MapUri(uri);

            if (mappedUri != null)
            {
                return mappedUri;
            }
        }

        string uriString = uri.ToString();

        if (uriString.Contains("?Action=ViewfinderLaunch"))
        {
            return new Uri("/Sensors/Camera/PhotoCameraView.xaml",
                        UriKind.Relative);
        }

        return uri;
    }
}


The UnleashedUriMapper is hooked up to the app’s PhoneApplicationFrame in the App.xaml file, as shown:

<Application.RootVisual>
    <toolkit:TransitionFrame x:Name="RootFrame" ...>
        <phone:PhoneApplicationFrame.UriMapper>
            <local:UnleashedUriMapper>
                <local:UnleashedUriMapper.UriMappings>
                    ...
                </local:UnleashedUriMapper.UriMappings>
            </local:UnleashedUriMapper>
        </phone:PhoneApplicationFrame.UriMapper>

    </toolkit:TransitionFrame>
</Application.RootVisual>

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.198.132