Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

ReBinding entire object, is it acceptable to simply set the BindingContext again?

$
0
0

So I have a situation where I update a ViewModel based on the Messenger. When the messenger get's a new message, I rebind the entire View to the new ViewModel.

First I tried this

        MessagingCenter.Subscribe<MainMenu, ModuleSubscriptionMessage>(subscriber, "CurrentModuleMessage",
            async (s, e) =>
            {
                _viewModel = e.Module;
            });

but it didn't work because the OnPropertyChanged() notifications never triggered on the bound ViewModel

Second I did this

        MessagingCenter.Subscribe<MainMenu, ModuleSubscriptionMessage>(subscriber, "CurrentModuleMessage",
            async (s, e) =>
            {
                _viewModel.Foo = e.Module.Foo;
                _viewModel.Bar = e.Module.Bar;
                // etc for a large number of properties
            });

This worked fine since the OnPropertyChanged() was being triggered on the bound ViewModel, but really cluttered the code

Last, I simply updated the BindingContext

        MessagingCenter.Subscribe<MainMenu, ModuleSubscriptionMessage>(subscriber, "CurrentModuleMessage",
            async (s, e) =>
            {
                _viewModel = e.Module;
                BindingContext = _viewModel;
            });

Now this works, and ALSO cleans up the code.

My question... is this an acceptable way to update the view based on an entire object being updated?


Viewing all articles
Browse latest Browse all 58056

Trending Articles