我刚刚开始 CM 的教程,直到这里的最后一个示例。我想更好地了解我可以用 CM 做什么,所以我做了一些我通常会在 wpf 项目中做的事情,为 Views 创建一个文件夹,为 ViewModels 创建一个文件夹。约定仍然应该找到一切,对吧?
所有其他示例都以这种方式工作得很好,但是在对 CM 和如何调试silverlight一无所知之间,我无法解释似乎是数据上下文问题 - 除了显示添加按钮之外什么都没有。
有人能发现问题吗?
干杯,
贝里尔
虚拟机
[Export(typeof(IShell))]
public class ShellWithCompositionViewModel : PropertyChangedBase
{
public BindableCollection<Model> Items { get; private set; }
public ShellWithCompositionViewModel() {
Items = new BindableCollection<Model>
{
new Model {Id = Guid.NewGuid()},
new Model {Id = Guid.NewGuid()},
new Model {Id = Guid.NewGuid()},
new Model {Id = Guid.NewGuid()}
};
}
public void Add() { Items.Add(new Model {Id = Guid.NewGuid()}); }
public void Remove(Model child) { Items.Remove(child); }
}
看法
<UserControl x:Class="Caliburn.Micro.Hello.Views.ShellWithCompositionView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cal="http://www.caliburnproject.org"
>
<StackPanel>
<ItemsControl x:Name="Items">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Button Content="Remove" cal:Message.Attach="Remove($dataContext)" />
<TextBlock Text="{Binding Id}" />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<Button Content="Add" cal:Message.Attach="Add" />
</StackPanel>
开机
namespace Caliburn.Micro.Hello
{
//public class HelloBootstrapper : Bootstrapper<ShellViewModel> { }
//public class HelloBootstrapper : Bootstrapper<ShellWithParametersViewModel> { }
public class HelloBootstrapper : Bootstrapper<ShellWithCompositionView> { }
}
修复
// left off the model the 1st time (caps not needed!)
public class HelloBootstrapper : Bootstrapper<ShellWithCompositionViewMODEL> { }