-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move IDispatcher
logic out of IPlatformServices
#2862
Conversation
IDispatcher
logic out of IPlatformServices
I wasn't aware that this was possible in Xamarin.Forms. If it isn't desired in MAUI, maybe we can keep it only in compatibility mode (opt-in). Or, if it is still necessary, I just hope that it doesn't "compromise" the normal way of building UI on their UI thread. I am waiting for multi-window and CollectionView to be available on Windows (in Preview 11 hopefully). Then, I will port my app to MAUI and see if any of these potential issues still occur. Edit: I'm glad to see |
src/Controls/samples/Controls.Sample.SingleProject/Properties/launchSettings.json
Show resolved
Hide resolved
Co-authored-by: Shane Neuville <shneuvil@microsoft.com>
Description of Change
This PR moves the dispatcher and "run on main thread" logic out of the
Forms
/Device
/IPlatformServices
space and into Core and Controls.Core has the basic logic for executing on a "platform amin thread" and Controls attaches that logic to all
BindableObjects
.Part of #1965
Discussion
Due to backwards compatibility, I have actually implemented this "wrong" in the objective sense. This is because we allow UI objects to be created and used off the UI thread.
For example, If I start up a background thread and on that background thread I create a
Button
. Firstly, that is incorrect because logic in the constructor is now running on a background thread. Second, when I use theDispatcher
property, what thread should I use? Right now I just thrash around and try things until I find one.Now, the thing that sort of holds this together is that there typically is only 1 UI thread. In most cases. Even in Windows with multiple windows because the default way we create windows is on the same thread. So we are lucky. However, this is not fixed. The user or maui may opt to create windows in separate threads - especially if the windows do a lot of work.
We then may enter a world of pain. For example this execution:
myButton
on BmyButton.Dispatcher.Invoke
from B (probably will pick UI 1)myButton.Dispatcher.Invoke
from B :Additions made
IDispatcher
andIDispatcherProvider
to Core.IMauiContext
.MauiContext
ctors and into the correct scoping methods as they fit better there.Device
.Device.*
to use the dispatchers.