【发布时间】:2011-12-14 08:22:14
【问题描述】:
我正在尝试学习将 Caliburn.Micro 与 WPF 结合使用。如何在一个视图中添加多个视图?
<Window x:Class="ProjectName.Views.MainView"
...>
<Grid>
<views:MyControlView />
</Grid>
</Window>
另一个视图,带有视图模型:MyControlViewModel
<UserControl x:Class="ProjectName.Views.MyControlView"
...>
<Grid>
...
</Grid>
</UserControl>
如果我只是添加视图,它不会检测到它有一个具有适当名称的视图模型。我怎样才能将它绑定到它?
我尝试过使用不同的引导程序并使用类似 cal:Bind.Model="path/classname/merge of the two" 之类的东西。已尝试将其添加到主视图和用户控件(MyControlView)。我非常感谢有关此事的任何帮助。我几乎被卡住了,我真的很想使用 Caliburn.Micro :)
最好的问候, 钻石鱼
编辑:我仍然无法让它工作,问题似乎出在引导程序或其他东西上。但为了澄清,这是我正在为一个测试项目运行的代码。
MainView xaml:
<Window x:Class="Test.Views.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cal="clr-namespace:Caliburn.Micro;assembly=Caliburn.Micro"
xmlns:views="clr-namespace:Test.Views"
Title="MainWindow" Height="360" Width="640">
<Grid>
<views:MyControlView />
</Grid>
MainViewModel 代码:
public partial class MainViewModel : PropertyChangedBase
{
}
MyControlView xaml:
<UserControl x:Class="Test.Views.MyControlView"
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:cal="clr-namespace:Caliburn.Micro;assembly=Caliburn.Micro"
cal:Bind.Model="Test.MyControlViewModel"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<TextBlock Text="{Binding MyProp}"/>
</Grid>
MyControlView 代码:
public class MyControlViewModel : PropertyChangedBase
{
public string MyProp
{
get { return "Working"; }
}
}
错误截图:http://clip2net.com/s/1gtgt
我试过了
cal:Bind.Model="Test.ViewModels.MyControlViewModel"
也是。还尝试了 cal-reference:
xmlns:cal="http://www.caliburnproject.org"
我的项目截图http://clip2net.com/s/1gthM
由于文档主要是针对 silverlight,有时是针对 Caliburn 而不是 CM,因此我可能错误地实现了引导程序。对于这个测试项目,它就像这样:(使用 App.xaml 中的 .xaml-change)
public class BootStrapper : Bootstrapper<MainViewModel>
{
}
请帮帮我!似乎这是我缺少的一些基本东西:)
【问题讨论】:
-
-编辑帖子以包含 MVVM 标签,欢迎来到 S.O!
-
检查分析器 - 我添加了关于导出类型的部分。这是 c.m 找到与 View 相关的 ViewModel 的重要要求。
标签: c# wpf mvvm caliburn.micro caliburn