Skip to content
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

DataContext is set too early when using MEF #19

Open
maikhanns opened this issue Nov 10, 2021 · 1 comment
Open

DataContext is set too early when using MEF #19

maikhanns opened this issue Nov 10, 2021 · 1 comment
Labels
area-Waf Win Application Framework (WAF) question

Comments

@maikhanns
Copy link

We have migrated an WPF application from an old WPFApplicationFramework version to System.WAF.

Our application uses MEF and a plugin class that inherits from ViewModel. The plugins have an additional Initialize method. So far the DataContext was set after this method. (Dispatcher.BeginInvoke in ViewModel)

Since System.WAF 4.0.0-alpha3 that behavior changed. The DataContext is already set after the MEF import and before our Initialize method.

This leads to the fact that bindings are already executed on a plugin that is not initialized for us. Within the application there are e.g. also DataContextChanged events which are now executed too early.

Is it possible to prevent the setting of the DataContext in the OnImportsSatisfied or to get more control over it?

Thanks in advance :-)

@jbe2277
Copy link
Owner

jbe2277 commented Nov 13, 2021

Explanation:
I have changed the behavior of initializing the DataContext within the ViewModel because the delay created by Dispatcher.BeginInvoke produced some issues when using nested ViewModels. However, this is a MEF based solution and so it works only when using MEF. If other ways are used to create the ViewModels then still the Dispatcher.BeginInvoke solution is used.

Solutions:
Here are possible solutions for your requirements:

  1. Approach: Implement your own ViewModel base class to get the old WAF behavior:
public abstract class ViewModel2<TView> : ViewModelCore<TView> where TView : IView
{
   protected ViewModel2(TView view) : base(view, false)
   {
       if (SynchronizationContext.Current is DispatcherSynchronizationContext)
       {
           Dispatcher.CurrentDispatcher.BeginInvoke((Action)delegate
           {
               ViewCore.DataContext = this;
           });
       }
       else
       {
           view.DataContext = this;
       }
   }
}
  1. Approach: Don't use the automatic DataContext initialization at all. Do it yourself in your Initialize method. Use ViewModelCore base class for that and call Initialize on the base class to set the DataContext.

I use the second approach for Xamarin and other non-WPF applications.

@jbe2277 jbe2277 added area-Waf Win Application Framework (WAF) question labels Nov 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-Waf Win Application Framework (WAF) question
Projects
None yet
Development

No branches or pull requests

2 participants