[英]reuse an usercontrol(View) with different ViewModels
I'll go directly to my issue. 我会直接谈谈我的问题。 :)
:)
I use Caliburn.Micro and mvvm. 我使用Caliburn.Micro和mvvm。 16 Usercontrols are on my MainView.
16个用户控件在我的MainView上。 All are looking the same with some buttons, labels,… like that:
所有看起来都与一些按钮,标签一样......就像那样:
<UserControl x:Class=" Projectxy.usercontrolexample01View"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Projectxy"
mc:Ignorable="d"
d:DesignHeight="200" d:DesignWidth="305">
<Grid>
<Button x:Name="button" Content="Button" Width="75"/>
<Label x:Name="label" Content="Label"/>
<ComboBox x:Name="comboBox"/>
</Grid>
</UserControl>
I call the usercontrol in the MainView like: 我在MainView中调用usercontrol,如:
<ContentControl Name=" usercontrolexample01Model" Grid.Column="2" Grid.Row="1"/>
<ContentControl Name=" usercontrolexample02Model" Grid.Column="2" Grid.Row="2"/>
(I don't plot the Bootstrapper because I think it's not necessary for my question...?) (我没有绘制Bootstrapper,因为我认为这不是我的问题......?)
What I want is not to add 16 times the same xaml code in my project like that: 我想要的是不要在我的项目中添加16倍相同的xaml代码:
usercontrolexample01View.xaml
usercontrolexample01ViewModel.cs
usercontrolexample02View.xaml
usercontrolexample02ViewModel.cs
…
It is hard to change 16 xaml files if I want to change something... 如果我想改变一些东西,很难改变16个xaml文件......
I want 16 Views in my MainView and 16 ViewModels (for every time I reuse it in the MainView) but "behind" the 16 Views should be just 1 xaml-file which should be reused(usercontrolexampleView.xaml). 我希望在我的MainView和16个ViewModel中有16个视图(每次我在MainView中重复使用它),但16个视图的“后面”应该只是1个应该重用的xaml文件(usercontrolexampleView.xaml)。
usercontrolexampleView.xaml
usercontrolexample01ViewModel.cs
usercontrolexample02ViewModel.cs
usercontrolexample03ViewModel.cs
Is there any way to reuse 1 Usercontrol (xaml-file) with Caliburn.Micro? 有没有办法重用1个Usercontrol(xaml文件)与Caliburn.Micro? I didn't find some articles about that... :( (There are some with the same titel but they have other focuses like this thread .) Finaly I found a question which is more in my direction.... But how could I manage that with Caliburn? Maybe someone have a code snipped with explanations for me?
我没有找到有关的一些物品... :((有一些与影片名称相同,但他们有其他的重点是这样的线程 )。Finaly我发现了一个问题,这更多的是我的方向....但怎么可能我用Caliburn管理它?也许某人有一个代码剪断了我的解释?
I hope my question is clear. 我希望我的问题很明确。 I'm just new to stackoverflow, wpf and caliburn micro... And not that familiar with english... :(
我刚刚接触stackoverflow,wpf和caliburn micro ...并且不熟悉英语...... :(
Thanks for all suggestions! 谢谢你的所有建议!
Caliburn.Micro
picks views for view models based on a naming convention. Caliburn.Micro
根据命名约定为视图模型选择视图。 If you have a view model class (it should be in a file in the <appfolder>/ViewModels/
subfolder and named <name>ViewModel.cs
), it will look for a view called 'View.xaml' user control in the <appfodler>/Views/
subfolder, and if it finds it, use it for displaying the view model. 如果你有一个视图模型类(它应该在
<appfolder>/ViewModels/
子文件夹中的一个文件中,并命名为<name>ViewModel.cs
),它将在<appfodler>/Views/
寻找一个名为'View.xaml'的用户控件的视图<appfodler>/Views/
子文件夹,如果找到它,用它来显示视图模型。
In your case, you can have as many instances of your particular view model in your main view model, and then bind them to a ContentControl
- each will use the same, once defined view, because they are of the same type. 在您的情况下,您可以在主视图模型中包含特定视图模型的任意数量的实例,然后将它们绑定到
ContentControl
- 每个将使用相同的,一旦定义的视图,因为它们是相同类型。
Eg: Say your view model is called SubViewModel
, and you have created a view for it, called SubView.xaml
. 例如:假设您的视图模型名为
SubViewModel
,并且您已SubViewModel
创建了一个名为SubView.xaml
的视图。 In main view model, define arbitrary number of instances, 在主视图模型中,定义任意数量的实例,
public SubViewModel sVM1 {get;set;}
public SubViewModel sVM2 {get;set;}
(or even make a List<SubviewModel>
of them), and in the main view, place them where you like, (或者甚至制作它们的
List<SubviewModel>
),并在主视图中,将它们放在你喜欢的地方,
...
<ContentControl Name="sVM1"/>
....
<ContentControl Name="sVM2"/>
then they will all be displayed using the SubView.xaml
. 然后他们将使用
SubView.xaml
显示。 (Also note that there is another binding covention here in place: By naming the ContentControl
the same as the SubViewModel
instances, they are automatically bound by caliburn.micro
). (另请注意,此处还有另一种绑定协议:通过将
ContentControl
命名为与SubViewModel
实例相同,它们将由caliburn.micro
自动绑定)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.