如何从LensPicker导航到CameraCaptureTask

时间:2013-10-15 19:40:31

标签: c# windows-phone-8

我创建了一个小样本镜头应用程序,我希望能够在默认相机应用程序中单击镜头图标时直接导航到CameraCaptureTask。在我的应用程序中,我已经在正常应用程序操作期间在按钮单击事件中调用CameraCaptureTask。我如何将其设置为与LensPicker选项一起工作?

我一直在引用 http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj662936(v=vs.105).aspx

LensExampleUriMapper.cs

private string tempUri;

public override Uri MapUri(Uri uri)
{
    tempUri = uri.ToString();

    // Look for a URI from the lens picker.
    if (tempUri.Contains("ViewfinderLaunch"))
    {
        // Launch as a lens, launch viewfinder screen.
        return new Uri("/MainPage.xaml", UriKind.Relative);
    }

    // Otherwise perform normal launch.
    return uri;
}

我在考虑在return new Uri("/MainPage.xaml", UriKind.Relative);中传递QueryString值,以便在我的MainPage OnNavigatedTo事件中我可以检查QueryString值并调用CameraCaptureTask,然后将结果路由到已存在的事件我创建的处理程序(在MainPage中显示结果图像)。出于某种原因,我在尝试创建QueryString时遇到调试错误,我不确定为什么?

编辑**不再出现错误,但在调用CameraCaptureTask时会发生无限循环。为什么呢?

LensExampleUriMapper.cs

private string tempUri;

public override Uri MapUri(Uri uri)
{
    tempUri = uri.ToString();

    // Look for a URI from the lens picker.
    if (tempUri.Contains("ViewfinderLaunch"))
    {
        // Launch as a lens, launch viewfinder screen.
        return new Uri("/MainPage.xaml?fromLensPicker=" + "fromLensPicker", UriKind.Relative);
    }

    // Otherwise perform normal launch.
    return uri;
}

MainPage.xaml.cs中

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    string fromLensPicker = null;
    if (NavigationContext.QueryString.TryGetValue("fromLensPicker", out fromLensPicker))
    {
        if (fromLensPicker == "fromLensPicker")
        {
            newButton_Click(null, null);  //click event that calls CameraCaptureTask
            fromLensPicker = null; //Temporarily nullifies value until MainPage is OnNavigatedTo after CameraCaptureTask completes
        }
    }
}

我相信当调用CameraCaptureTask时,应用程序将被逻辑删除,然后在MainPage上恢复,其中QueryStringfromLensPicker == "fromLensPicker"和整个循环重复开始。我怎么解决这个问题?

2 个答案:

答案 0 :(得分:0)

在MainPage中使用NavigationMode属性。我想你无法清除QueryString。但您可以检查如何导航到您的页面,以了解它是否从CameraCaptureTask返回

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    if(e.NavigationMode == NavigationMode.New)
    // continue further
}

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    if(e.NavigationMode == NavigationMode.Back)
    return;

    // else continue further
}

答案 1 :(得分:0)

而不是fromLensPicker = null中的MainPage.xaml.cs,我现在NavigationContext.QueryString.Remove("fromLensPicker")引用了protected override void OnNavigatedTo(NavigationEventArgs e) { string fromLensPicker = null; if (NavigationContext.QueryString.TryGetValue("fromLensPicker", out fromLensPicker)) { if (fromLensPicker == "fromLensPicker") { NavigationContext.QueryString.Remove("fromLensPicker"); //Perform Action } } }

MainPage.xaml.cs中

{{1}}