MVVMCross - display view inside view

你。 提交于 2021-02-04 21:34:15

问题


I cannot seem to find any simple examples of this.

I have a WPF UI that I wish to display a view as a child control within another view. The MvxWpfView inherits from UserControl so it should be possible, however I cannot seem to work out how to do the binding.

I get a BindingExpression path error, as it cannot find ChildView property in my ParentViewModel.

So how do I bind a view to control content?


回答1:


Firstly it's possible that you just need to add the BViewModel you want displayed on AView as a property on ViewModelA

E.g.

public class AViewModel: MvxViewModel
{
    public BViewModel ChildViewModel 
    {
        get;set;//With appropriate property changed notifiers etc.
    }
}

Then inside AView you just add a BView, and you can set the datacontext of BView as follows:

<UserControl DataContext="{Binding ChildViewModel}"/>

However, if you want something more flexible (and you want the presentation handled differently for different platforms) then you will need to use a Custom Presenter

Inside your setup.cs you override CreateViewPresenter:

    protected override IMvxWpfViewPresenter CreateViewPresenter(Frame rootFrame)
    {
        return new CustomPresenter(contentControl); 
    }

Now create the class CustomPresenter you need to inherit from an existing presenter. You can choose between the one it's probably using already SimpleWpfPresenter or you might want to go back a bit more to basics and use the abstract implementation

The job of the presenter is to take the viewmodel you have asked it to present, and display it "somehow". Normally that mean identify a matching view, and bind the two together.

In your case what you want to do is take an existing view, and bind a part of it to the second view mode.

This shows how I have done this in WinRT - but the idea is very similar!

    public override void Show(MvxViewModelRequest request)
    {
        if (request.ViewModelType == typeof (AddRoomViewModel))
        {
            var loader = Mvx.Resolve<IMvxViewModelLoader>();
            var vm = loader.LoadViewModel(request, new MvxBundle());
            if (_rootFrame.SourcePageType == typeof (HomeView))
            {
                HomeView view = _rootFrame.Content as HomeView;
                view.ShowAddRoom(vm);
            }
        }
        else
        {
            base.Show(request);
        }
    }

So what I'm doing is I'm saying if you want me to present ViewModel AddRoom, and I have a reference to the HomeView then I'm going to just pass the ViewModel straight to the view.

Inside HomeView I simply set the data context, and do any view logic I may need to do (such as making something visible now)

internal void ShowAddRoom(Cirrious.MvvmCross.ViewModels.IMvxViewModel vm)
{
    AddRoomView.DataContext = vm;
}

Hopefully that makes sense! It's well worth putting a breakpoint in the show method of the presenters so you get a feel how they work - they are really simple when you get your head around them, and very powerful.



来源:https://stackoverflow.com/questions/23797667/mvvmcross-display-view-inside-view

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!