1

I am trying to use a GraphicsCapturePicker from the code-behind in a MAUI app and am hitting a COM exception implying I am calling this from the wrong thread. Typically code in WPF/XAML code-behind files are dispatched from the main thread so I am a bit confused about what's going wrong here:

// A regular button event handler in MainPage.xaml.cs
private void OnScreenCaptureClicked(object sender, EventArgs e)
{
    var picker = new GraphicsCapturePicker();
    GraphicsCaptureItem item = picker.PickSingleItemAsync().GetResults();
}

Throws exception:

"The application called an interface that was marshalled for a different thread"
Exception thrown: 'System.Runtime.InteropServices.COMException' in System.Private.CoreLib.dll

Note: In the above example I am calling the method syncronously as a test to make it easier to catch the exception, the same thing happens when it's wrapped in a task.

This exact code works fine in a regular non-MAUI UWP/C# project.

James
  • 30,496
  • 19
  • 86
  • 113
  • Not sure what you mean with 'wrapped in a task' means. Async methods should in general be [awaited](https://stackoverflow.com/questions/44099401/frombluetoothaddressasync-iasyncoperation-does-not-contain-a-definition-for-get/44102996#44102996). Also, as far as I can tell, MAUI is still in preview, so bugs might be expected. – JonasH May 31 '21 at 15:02

0 Answers0