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

查看:105
本文介绍了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 .另一方面,我在此处找到了一些投诉在其他地方关于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.

第二个链接说明了如何在XAML中创建视图(UserControl)并创建视图的实例,并且希望将其绑定到视图模型,然后首先将其称为视图,告诉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天全站免登陆