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

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

Closed
newbienewbie opened this issue Jan 19, 2024 · 2 comments

Comments

@newbienewbie
Copy link
Contributor

newbienewbie commented Jan 19, 2024

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

@glennawatson
Copy link
Contributor

Probably do both is likely the best solution here.

Would you like to submit a pull request. If you are new to the process I can help out.

Be the fastest way to get it done

newbienewbie added a commit to newbienewbie/ReactiveUI that referenced this issue Jan 24, 2024
…iveui#3713)

This changes applys to the Maui/Wpf/XamarinForm platform.
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 11, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants