Skip to content

ViewModelViewHost: Better to Allow Developers Bypass the Default Fallback Behavior That Resovles View With No Contract  #3713

Closed
@newbienewbie

Description

@newbienewbie

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 renders ViewB
  • if user doesn't implement ViewB, it renders nothing (instead of ViewA)

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

  1. add a property of ContractFallbackByPass with a default value = false for all the ViewModelViewHost
  2. or make the private ResolveViewForViewModel method protected virtual

Additional context

See discussion #3712

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions