0

我有一个程序 (WinRT) 可以显示来自网络摄像头的视频流。在初始化媒体基础之前和之后,程序调用函数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();
    }
4

0 回答 0