Caliburn.Micro 能否很好地与用户控件配合使用? [英] Does Caliburn.Micro play nicely with user controls?

查看:18
本文介绍了Caliburn.Micro 能否很好地与用户控件配合使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名新手 WPF 程序员.我正在尝试向我的代码添加一些结构:用户控件和 MVVM.

I'm a novice WPF programmer. I'm trying to add some structure to my code: both User Controls and MVVM.

在这里研究,我发现人们推荐Caliburn.Micro.另一方面,我发现了一些投诉 here其他地方 关于 Caliburn.Micro 不能很好地与用户控件.

Researching here, I've found that people recommend Caliburn.Micro. On the other hand, I've found some complaints here and elsewhere about Caliburn.Micro not playing nicely with UserControls.

所以我的问题是:Caliburn.Micro 与用户控件配合得很好吗?

推荐答案

是的,Caliburn.Micro 与用户控件配合得很好.这是一个固执己见的框架,但不会强迫你走上特定的发展道路.正如链接问题的答案所暗示的,如果您有任何特定问题,您始终可以使用普通的旧 WPF 绑定.

Yes, Caliburn.Micro plays nicely with user controls. It's an opinionated framework, but not to the point of forcing you down a particular development path. As the answers to the linked questions suggest, you can always use plain old WPF binding if you have any particular issues.

事实上,我根本不会让这两个链接阻止您,第一个是描述一种将单独的属性绑定到单个用户控件的方法,并且该解决方案是有效的.更好的解决方案可能是使用带有自定义 DataTemplateItemsControl,然后在其视图模型上创建包含属性名称和值的 DTO 集合.

In fact, I wouldn't let those two links deter you at all, the first is describing a way of binding separate properties to a single user control, and the solution is valid. A better solution would probably be to use an ItemsControl with a custom DataTemplate, and then create a collection of DTOs on his view model which contain the property names and values.

第二个链接说明如果您创建视图 (UserControl) 并在 XAML 中创建视图的实例,并且希望将其绑定到视图模型,则称为视图首先,你必须告诉 Caliburn.Micro 视图模型要绑定到哪里:

The second link is stating how if you create a view (UserControl) and create an instance of the view in XAML, and you wish to bind it to a view model, then that is called view first, and you have to tell Caliburn.Micro where the view model is to bind to:

<UserControl ...
   cal:Bind.Model="EasyPlayer.MediaControl.NowPlayingViewModel" />

所以,这在概念上可以被认为是一个视图模型/视图,而不是一个具有依赖属性等的 UserControl.

So, this conceptually can be thought of as a viewmodel/view rather than a UserControl with dependency properties etc.

事实上,你会发现,当你使用 Caliburn.Micro 时,你可能会越来越少地使用 UserControl 来执行视图合成.这是因为使用视图模型、视图和视图模型优先方法很容易创建可重用的 UI 片段.

In fact, you'll find when you use Caliburn.Micro, you'll probably use less and less UserControls to perform view composition. This is because it is very easy to create reusable pieces of UI using view models, views, and the view model first approach.

当您的视图中有一个 ContentControl 与父视图模型上的视图模型属性同名时,Caliburn.Micro 将定位相应视图模型的视图,将其注入ContentControl,并绑定视图/视图模型.

When you have a ContentControl in a view with the same name as a view model property on your parent view model, then Caliburn.Micro will locate the view of the corresponding view model, inject it into the ContentControl, and bind up the view/view model.

例如:

public class MyParentViewModel : Screen
{
  public MenuViewModel MenuViewModel { get; set; }

  public DetailsViewModel DetailsViewModel { get; set; }

  public MyParentViewModel()
  {
    this.MenuViewModel = new MenuViewModel();
    this.DetailsViewModel = new DetailsViewModel();
  }
}

<Grid> 
  <Grid.ColumnDefinitions> 
    <ColumnDefinition Width=".2*" />
    <ColumnDefinition Width=".8*" />
  </Grid.ColumnDefinitions>

  <ContentControl Grid.Column="0" x:Name="MenuViewModel" />
  <ContentControl Grid.Column="1" x:Name="DetailsViewModel" />

</Grid>

这篇关于Caliburn.Micro 能否很好地与用户控件配合使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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