校准微教程问题

时间:2011-02-03 03:20:27

标签: silverlight caliburn.micro caliburn

我刚刚开始使用CM教程,直到最后一个示例here。我希望能够更好地了解我可以用CM做什么,所以我做了一件我通常在wpf项目中做的事情,为Views创建一个文件夹,为ViewModels创建一个文件夹。公约应该还能找到一切,对吧?

所有其他示例都以这种方式工作,但在对CM和如何调试Silverlight无知之间,我无法解释看起来像数据上下文的问题 - 除了显示添加按钮之外什么都没有。

有人能发现问题吗?

干杯,
Berryl

VM

[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); }
}

VIEW

<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>

BOOT

namespace Caliburn.Micro.Hello
{
    //public class HelloBootstrapper : Bootstrapper<ShellViewModel> { }
    //public class HelloBootstrapper : Bootstrapper<ShellWithParametersViewModel> { }
    public class HelloBootstrapper : Bootstrapper<ShellWithCompositionView> { }
}

FIX

// left off the model the 1st time (caps not needed!)
public class HelloBootstrapper : Bootstrapper<ShellWithCompositionViewMODEL> { }

1 个答案:

答案 0 :(得分:2)

问题是你的Bootstrapper引用了View而不是ViewModel。您应该将其更改为:

public class HelloBootstrapper : Bootstrapper<IShell> { }

此外,除了应用export属性外,还要确保在ShellWithCompositionViewModel上实现IShell接口。

相关问题