无法在Windows Phone 8.1中显示对话框

时间:2016-09-03 13:29:43

标签: c# windows-phone-8.1

我正在为Windows Phone 8.1开发一个qrcode扫描仪自定义应用程序。 我正在使用诺基亚成像SDK渲染后置摄像头,以便预览QRCode图像,解码后,我无法显示消息对话框。它抛出以下异常:

  

该应用程序调用了一个为不同线程编组的接口。 (来自HRESULT的异常:0x8001010E(RPC_E_WRONG_THREAD))`

这是初始化预览时的代码

private async void Init()
{
    //Get back camera
    var devices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
    var backCameraId = devices.FirstOrDefault(x => x.EnclosureLocation != null && x.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back).Id;

    //Start preview
    _cameraPreviewImageSource = new CameraPreviewImageSource();
    await _cameraPreviewImageSource.InitializeAsync(backCameraId);
    var properties = await _cameraPreviewImageSource.StartPreviewAsync();

    //Setup preview
    _width = 300.0;
    _height = (_width / properties.Width) * properties.Height;
    var bitmap = new WriteableBitmap((int)_width, (int)_height);

    _writeableBitmap = bitmap;

    PreviewImage.Source = _writeableBitmap;

    _writeableBitmapRenderer = new WriteableBitmapRenderer(_cameraPreviewImageSource, _writeableBitmap);

    _cameraPreviewImageSource.PreviewFrameAvailable += _cameraPreviewImageSource_PreviewFrameAvailable;

    _videoDevice = (VideoDeviceController)_cameraPreviewImageSource.VideoDeviceController;

    //Set timer for auto focus
    if (_videoDevice.FocusControl.Supported)
    {
        var focusSettings = new FocusSettings
        {
            AutoFocusRange = AutoFocusRange.Macro,
            Mode = FocusMode.Auto,
            WaitForFocus = false,
            DisableDriverFallback = false
        };

        _videoDevice.FocusControl.Configure(focusSettings);

        _timer = new DispatcherTimer
        {
            Interval = new TimeSpan(0, 0, 0, 2, 0)
        };
        _timer.Tick += TimerOnTick;
        _timer.Start();
    }

    await _videoDevice.ExposureControl.SetAutoAsync(true);

    _initialized = true;

}

这是我解码的方式

private async void Deocode(byte[] rawRgb, BitmapFormat bitmapFormat)
{
    await Task.Run(() =>
    {
       if (_decoding)
           return;

       _decoding = true;

       var decoded = _reader.Decode(rawRgb, (int)_width, (int)_height, bitmapFormat);

       if (decoded != null)
       {
           cde = decoded.Text;
           Stop();

       }

       _decoding = false;
   });
   MeesageDialog msg = new MessageDialog(cde);
   await msg.ShowAsync();
}

1 个答案:

答案 0 :(得分:0)

要在UI线程上执行操作,您需要使用调度程序运行代码。

CoreDispatcher dispatcher = CoreWindow.GetForCurrentThread().Dispatcher;
await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
      {
          MeesageDialog msg = new MessageDialog(cde);
          await msg.ShowAsync();
      });

试试这段代码(我认为它应该有用)。