错误显示相机或信息

时间:2013-04-11 11:05:26

标签: c# windows-runtime

我有一个程序(WinRT),显示来自网络摄像头的视频流。在初始化 Media Foundation 之前和之后,programm调用函数 ShowMessage 。我评论了这些电话。如果代码运行,如下所示,我可以看到来自网络摄像头的视频流。 但是,如果我取消注释呼叫功能 ShowMessage ,第一次呼叫(//第一次呼叫)完成,来自网络摄像头的视频流将显示,但第二次呼叫( //第二次呼叫)不是呼叫。(我没有看到第二条消息)。我怎么能这样做,来自网络摄像头的视频流将显示,并将同时调用ShowMessage?

XAML:

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
    <CaptureElement x:Name="myCaptureElement" Margin="504,124,400,350" FlowDirection="RightToLeft" HorizontalAlignment="Center" VerticalAlignment="Center" Stretch="Fill" MinWidth="300" MinHeight="200" Height="294" Width="462" />
    <Rectangle x:Name="rect1" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="100" Margin="43,292,0,0" Stroke="Black" VerticalAlignment="Top" Width="100"/>
</Grid>

C#:

public MainPage()
    {
        this.InitializeComponent();

        Func<Task> unnamed = async () =>
        {
            await TestPhoto();
        };
        unnamed();
    }


    private async Task TestPhoto()
    {
        try
        {
            //await ShowMessage("Do something",true);//first call

            var devInfoCollection = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
            var settings = new Windows.Media.Capture.MediaCaptureInitializationSettings();
            settings.VideoDeviceId = devInfoCollection[0].Id;
            var mediaCaptureMgr = new MediaCapture();
            await mediaCaptureMgr.InitializeAsync();
            mediaCaptureMgr.SetPreviewMirroring(true);
            myCaptureElement.Source = mediaCaptureMgr;
            await mediaCaptureMgr.StartPreviewAsync();



            ImageEncodingProperties imageProperties2 = ImageEncodingProperties.CreateJpeg();
            var memStream3 = new Windows.Storage.Streams.InMemoryRandomAccessStream();
            var mediaCaptureMgr1 = new MediaCapture();
            await mediaCaptureMgr1.InitializeAsync();
            mediaCaptureMgr1.SetPreviewMirroring(true);
            await mediaCaptureMgr1.CapturePhotoToStreamAsync(imageProperties2, memStream3);
            await memStream3.FlushAsync();
            memStream3.Seek(0);


            WriteableBitmap wp1 = new WriteableBitmap(1, 1); ;
            await wp1.SetSourceAsync(memStream3);

            //await ShowMessage("Do something",false);//second call
        }
        catch (Exception)
        {

        }

    }

    public async Task ShowMessage(String s, bool b)
    {

        await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                                                                     {
                                                                            if (b)
                                                                            {
                                                                                rect1.Visibility=Visibility.Collapsed;
                                                                            }
                                                                            else
                                                                            {
                                                                                rect1.Visibility=Visibility.Visible;
                                                                            }
                                                                     });
        Random r=new Random();
        await new MessageDialog(s+" "+r.Next(1000), "Information").ShowAsync();
    }

0 个答案:

没有答案