0

我创建了一个小的示例镜头应用程序,我希望能够在默认相机应用程序中单击镜头图标时直接导航到 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;
}

我正在考虑传入一个 QueryString 值,return new Uri("/MainPage.xaml", UriKind.Relative);以便在 MainPageOnNavigatedTo事件中检查该 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"和整个循环会重复地重新开始。我该如何解决这个问题?

4

2 回答 2

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
}
于 2013-10-16T09:04:20.343 回答
0

而不是 make fromLensPicker = nullin MainPage.xaml.cs,我现在有从WP7 NavigationNavigationContext.QueryString.Remove("fromLensPicker")中引用的参数

MainPage.xaml.cs

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           
        }
    }
}
于 2013-10-16T17:10:14.747 回答