MVVMCross - 在视图内显示视图 [英] MVVMCross - display view inside view

查看:63
本文介绍了MVVMCross - 在视图内显示视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎找不到任何简单的例子.

I cannot seem to find any simple examples of this.

我有一个 WPF UI,我希望将一个视图显示为另一个视图中的子控件.MvxWpfView 继承自 UserControl 所以它应该是可能的,但是我似乎无法弄清楚如何进行绑定.

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.

我收到一个 BindingExpression 路径错误,因为它在我的 ParentViewModel 中找不到 ChildView 属性.

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?

推荐答案

首先,你可能只需要添加你想要在 AView 上显示的 BViewModel 作为 ViewModelA 上的一个属性

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

例如

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

然后在AView里面你只需要添加一个BView,你就可以设置BView的datacontext如下:

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

在 setup.cs 中覆盖 CreateViewPresenter:

Inside your setup.cs you override CreateViewPresenter:

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

现在创建您需要从现有演示者继承的类 CustomPresenter.您可以选择它可能已经在使用的一个 SimpleWpfPresenter 或者您可能想要更多地回到基础知识并使用 抽象实现

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.

这显示了我如何在 WinRT 中完成此操作 - 但想法非常相似!

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);
        }
    }

所以我正在做的是,如果你想让我展示 ViewModel AddRoom,我有一个对 HomeView 的引用code> 然后我将直接将 ViewModel 传递给视图.

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.

HomeView 内部,我只是设置数据上下文,并执行我可能需要执行的任何视图逻辑(例如现在使某些内容可见)

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.

这篇关于MVVMCross - 在视图内显示视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆