Can't show Dialog box in windows phone 8.1

0

i am devloping a qrcode scanner custom app for windows phone 8.1. i am using the Nokia Imaging SDK to render the back camera so as to preview the QRCode image, after decoding, i cant display a message dialog. it throws the following exception:

The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))`

here is my code when initializing preview

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;

}

this is how i decode

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();
}
c#
windows-phone-8.1
asked on Stack Overflow Sep 3, 2016 by trinity • edited Sep 3, 2016 by Mong Zhu

1 Answer

0

to perform operation on UI thread you need run code with dispatcher.

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

Try this code (i think it should work).

answered on Stack Overflow Sep 5, 2016 by Arek Kubiak

User contributions licensed under CC BY-SA 3.0