Windows Phone 8相机在未处于调试/主控模式时挂起

时间:2013-12-27 16:20:35

标签: c# windows-phone-8 camera unity3d

这个问题一直困扰着我。我还没有解决方案。

我有这个XAML代码,DrawingSurfaceBackgroundGrid是Unity游戏,其余的是折叠相机。

<DrawingSurfaceBackgroundGrid x:Name="DrawingSurfaceBackground" Loaded="DrawingSurfaceBackground_Loaded">

        <!-- Augmented Reality -->

        <Canvas VerticalAlignment="Center" x:Name="arCameraStack" Canvas.ZIndex="1" Width="732" Height="549" HorizontalAlignment="Left" Visibility="Collapsed" Tap="viewfinderCanvas_Tap">
            <Canvas.Background>
                <VideoBrush x:Name="viewfinderBrush" />
            </Canvas.Background>
            <Image x:Name="imgTarget" Source="/Assets/Icons/camera.target.png"  VerticalAlignment="Center" HorizontalAlignment="Center" Canvas.Left="114" Canvas.Top="27"/>
        </Canvas>
</DrawingSurfaceBackgroundGrid>

这些是我的开始/停止相机功能:

        public void StartCamera(bool hasTarget)
        {
            camera = new PhotoCamera(CameraType.Primary);
            viewfinderBrush.SetSource(camera);
            Dispatcher.BeginInvoke(() =>
            {
                if (hasTarget)
                {
                    imgTarget.Visibility = Visibility.Visible;
                }
                else
                {
                    imgTarget.Visibility = Visibility.Collapsed;
                }
            });
        }

        public void StopCamera()
        {
            if (camera != null)
            {
                camera.Dispose();
                camera = null;
            }
        }

我有一个屏幕系统可根据您想要查看的内容更改视图,有点像更改页面。

在显示Camera部分的页面中调用此方法。

public void Show()
{
    MainPage.Instance.Dispatcher.BeginInvoke(() =>
    {
        MainPage.Instance.arCameraStack.Visibility = Visibility.Visible;

    });
    MainPage.Instance.StartCamera(false);
}

当我想隐藏Unity部分时会调用它。

    public void Hide()
    {
        UnityApp.SetNativeResolution(0, 0);
        UnityApp.Obscure(false);
        UnityApp.Deactivate();
        MainPage.Instance.Dispatcher.BeginInvoke(() =>
        {
            MainPage.Instance.ApplicationBar.IsVisible = true;
        });
    }

当我有解决方案构建并通过Visual Studio运行时,一切正常,但是一旦我从手机启动应用程序,而不是从VS的主/调试模式,相机就会挂起。我发现当我切换页面中任何内容的可见性时,相机会更新一秒钟,然后再次挂起。

1 个答案:

答案 0 :(得分:1)

好吧,我发现了问题所在。

我的显示/隐藏功能现在看起来像这样:

public void Hide()
    {

        UnityApp.Obscure(false);
        UnityApp.Deactivate();
        MainPage.Instance.Dispatcher.BeginInvoke(() =>
        {
            MainPage.Instance.DrawingSurfaceBackground.SetBackgroundContentProvider(null);
            MainPage.Instance.DrawingSurfaceBackground.SetBackgroundManipulationHandler(null);
            MainPage.Instance.HideUnityBorder.Visibility = Visibility.Visible;
            MainPage.Instance.ApplicationBar.IsVisible = true;
        });
    }

    public void Show()
    {
        var content = Application.Current.Host.Content;
        var width = (int)Math.Floor(content.ActualWidth * content.ScaleFactor / 100.0 + 0.5);
        var height = (int)Math.Floor(content.ActualHeight * content.ScaleFactor / 100.0 + 0.5);
        UnityApp.SetNativeResolution(width, height);
        UnityApp.UnObscure();
        MainPage.Instance.Dispatcher.BeginInvoke(() =>
        {
            MainPage.Instance.DrawingSurfaceBackground.SetBackgroundContentProvider(UnityApp.GetBackgroundContentProvider());
            MainPage.Instance.DrawingSurfaceBackground.SetBackgroundManipulationHandler(UnityApp.GetManipulationHandler());
            MainPage.Instance.HideUnityBorder.Visibility = Visibility.Collapsed;
            MainPage.Instance.ApplicationBar.IsVisible = false;
        });
    }

主要问题出在 DrawingSurfaceBackground.SetBackgroundContentProvider DrawingSurfaceBackground.SetBackgroundManiuplationHandler 函数中。 现在在Unity部分和WP部分之间切换很容易。