windows phone 8.1中无法显示对话框

本文关键字:显示 对话框 phone windows | 更新日期: 2023-09-27 18:05:34

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

应用程序调用了为不同线程编组的接口。(Exception from HRESULT: 0x8001010E (rpce_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();
}

windows phone 8.1中无法显示对话框

要在UI线程上执行操作,您需要使用分派器运行代码。

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

尝试这个代码(我认为它应该工作)。