microsoft / XboxGameBarSamples

Sample code for Xbox Game Bar Widgets
https://developer.microsoft.com/games/products/game-bar/
MIT License
145 stars 28 forks source link

UI Element Not Updating from AppService request #135

Open SkyTheComputerGuy opened 3 months ago

SkyTheComputerGuy commented 3 months ago

Hello!

I’m experiencing an issue with a Xbox Game Bar widget where a Rectangle element’s Fill property is not visually updating as expected when receiving a request from a WPF app. I was wondering If I could get some assistance here.

Context: (Projects are not packaged)

The Issue: (Code Snippets are below)

This has led me to believe it has something to do with which thread calls what methods, or something of that sort? Any help would be really appreciated. Thank you.

Code Snippets:

image

image

vidager commented 3 months ago

I assume you've debugged to ensure no exceptions are thrown and that you're indeed on the correct dispatcher thread for ColorRec. If you want to be sure you can grab the dispatcher directly off ColoRec (in case it differs from MainPage...though it shouldn't). Keep in mind the Button_Click will already be called in the UI thread, whereas AppServices call will come on a threadpool thread. So it seems like you're dispatch logic in Button_click is redundant, but you are seemingly doing the right thing for RunAsync.

I'm not as familiar with C# threading behavior as I am with C++/WinRT. However, in C++/WinRT you have to be careful that resuming from co_await on anything other than IAsyncAction/Operation can result in resumption on a threadpool thread. Perhaps your await dialog.ShowAsync() resumes on a threadpool thread instead of back on the dispatcher thread. I would liter some threadid debug logs at various points to study what's going on.

SkyTheComputerGuy commented 2 months ago

It is definitely not because of the await's, since even if you remove the dialog boxes, the color still does not update. I even added these lines in both the SetInputAsync (before and after the awaits, and even without the dlg box ) and the in-widget button click: Debug.WriteLine($"Thread: {Environment.CurrentManagedThreadId}");

and both times it was 'Thread: 4'

So maybe it is because of how the WPF app connects to the AppService or maybe it is a bug?

image

image

Coming from assembler and C+, C# is really having a toll on me. Thank you for your reply!