You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello there,
I will give a training of ReactiveUI and am putting together a sample app to show some dynamic binding of a ViewModel to an automatically found View.
As I understand the docs, if you have a ShellView and a ShellViewModel, you should be able to put a ViewModelViewHost to the ShellView and bind its ViewModel to a MainViewModel (corresponding to which a MainView is also in the app).
When the ShellView is activated and sets its ViewModel to the ShellViewModel, the ViewModelViewHost should automatically contain and show the MainView.
In my app the MainView is indeed instantiated, but in WhenActivated I need to manually set DataContext and ViewModel of the MainView which I do like that:
this.WhenActivated(d =>
{
DataContext = ViewModel = ((Parent as ViewModelViewHost)?.ViewModel as MainViewModel) ?? Locator.Current.GetService();
});
This works, but the drawback is that I now constrain MainView/Model to only work inside a ViewModelViewHost, and it is also a bit fragile (but I would instead search upward for the next ViewModelViewHost).
Shouldn't the ViewModel and DataContext automatically be set when the VMVH marries MainView with its ViewModel?
Or is there still something missing in my app.
The demo app is attached as a zip
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hello there,
I will give a training of ReactiveUI and am putting together a sample app to show some dynamic binding of a ViewModel to an automatically found View.
As I understand the docs, if you have a ShellView and a ShellViewModel, you should be able to put a ViewModelViewHost to the ShellView and bind its ViewModel to a MainViewModel (corresponding to which a MainView is also in the app).
When the ShellView is activated and sets its ViewModel to the ShellViewModel, the ViewModelViewHost should automatically contain and show the MainView.
In my app the MainView is indeed instantiated, but in WhenActivated I need to manually set DataContext and ViewModel of the MainView which I do like that:
this.WhenActivated(d =>
{
DataContext = ViewModel = ((Parent as ViewModelViewHost)?.ViewModel as MainViewModel) ?? Locator.Current.GetService();
});
This works, but the drawback is that I now constrain MainView/Model to only work inside a ViewModelViewHost, and it is also a bit fragile (but I would instead search upward for the next ViewModelViewHost).
Shouldn't the ViewModel and DataContext automatically be set when the VMVH marries MainView with its ViewModel?
Or is there still something missing in my app.
The demo app is attached as a zip
Thanks very much!
RxUIToolbox.zip
Beta Was this translation helpful? Give feedback.
All reactions