Description
Is your feature request related to a problem? Please describe.
I have multiple different views that share the same ViewModel:
class ViewA : UserControl, IViewFor<WaitingFunVM>
{
}
Locator.CurrentMutable.Register( typeof(ViewA), typeof(IVewFor<WaitingFunVM>));
class ViewB: UserControl, IViewFor<WaitingFunVM>
{
}
// specify a ContractB here
Locator.CurrentMutable.Register( typeof(ViewB), typeof(IVewFor<WaitingFunVM>), ConstractB);
// specify a ViewContract=ConstractB
<rxui:ViewModelViewHost x:Name="myview" ViewContract="ConstractB" />
The current implementation of ViewModelViewHost
falls back to resolve view with no contract if nothing found.
private void ResolveViewForViewModel(object? viewModel, string? contract)
{
// ...
var viewInstance = viewLocator.ResolveView(viewModel, contract) ?? viewLocator.ResolveView(viewModel);
In other words, it will render ViewA
if nothing found even if I request a ViewContract=ContractB
.
In my case, the ViewB
is implemented by other plugin authors and is optional. What I want is:
- if user provide the
ViewB
implementation, it rendersViewB
- if user doesn't implement
ViewB
, it renders nothing (instead ofViewA
)
Describe the solution you'd like
add a property of ContractFallbackByPass
for the ViewModelHost so that we can bypass the fallback to viewLocator.ResolveView(viewModel);?
e.g. :
var viewInstance = viewLocator.ResolveView(viewModel, contract) ;
if(viewInstance == null && !this.ContractFallbackByPass)
{
viewInstance = viewLocator.ResolveView(viewModel);
}
Describe alternatives you've considered
make the private ResolveViewForViewModel
protected virtual so that we can override it.
protected virtual ResolveViewForViewModel(object? viewModel, string? contract) {
}
Describe suggestions on how to achieve the feature
- add a property of
ContractFallbackByPass
with a default value = false for all the ViewModelViewHost - or make the private ResolveViewForViewModel method protected virtual
Additional context
See discussion #3712