0

I am making a WASDK WinUI 3 app. In my App.xaml.cs, I have this:

public static XamlRoot AppXamlRoot { get; set; }

Then, on my main window, I've got this:

private void MainGrid_Loaded(object sender, RoutedEventArgs e)
{
    App.AppXamlRoot = this.Content.XamlRoot;
}

MainGrid is a Grid element in the XAML code. Finally, on a ViewModel class, I have an async method that uses the App.AppXamlRoot, which should be accessible because it is static and App class declaration is accessible from the whole C# project. In my async method, the App.AppXamlRoot throws a COM exception.

await MyMessageBox.Show($"{ex.Message}\n\n{ex.InnerException}", App.AppXamlRoot);
2
  • How did you access UI elements? with DispatcherQueue? For example, CustomControls. Commented Mar 14, 2024 at 1:42
  • You cannot access UI elements from a non-UI thread. You will have to marshal any code that access UI elements to the UI thread using the DispatcherQueue. Commented Mar 15, 2024 at 12:37

1 Answer 1

0

If you need to access the UI from a non-UI thread, you need to use the DispatcherQueue.

For example, in your App.xaml.cs:

public static DispatcherQueue DispatcherQueue { get; } = DispatcherQueue.GetForCurrentThread();

and use it like this:

App.DispatcherQueue.TryEnqueue(async () =>
{
    // Acceess the UI here...
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.