ViewModel 或模型与 Caliburn.Micro 绑定

发布于 2024-10-22 19:22:31 字数 2254 浏览 7 评论 0原文

这更像是一个 MVVM 问题,而不是一个 caliburn 问题,但它与我如何使用 caliburn 完成它有关。

我是 Sliverlight/WP7 开发的新手,所以如果我对自己的描述不够好,请告诉我。

我将 caliburn.micro 正确连接到带有phonecontainer/simplecontainer和viewmodels等的WP7应用程序。我遇到的问题是如何将模型集合正确绑定到屏幕。

例如,我有以下模型:

SummaryItem
{
    int Id
    string Name
    string Description
}

和相应的视图模型:

SummaryViewModel : Conductor<IScreen>.Collection.OneActive
{
    ObservableCollection<SummaryItem> SummaryItems;

    OnInitialize()
    {
        SummaryItems = // REST api call to load items
    }
}

和视图:

         <ListBox x:Name="SummaryItems" Height="617" HorizontalAlignment="Left" VerticalAlignment="Top" Width="468" Background="Transparent">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Button Width="460" Height="120">
                        <Button.Content>
                            <StackPanel Orientation="Horizontal" Height="120" Width="400">
                                <TextBlock Text="{Binding Id}" Height="120" FontSize="40" Width="350" />
                                <TextBlock Text="{Binding Name}" Height="120" FontSize="40" Width="350" />
                                <TextBlock Text="{Binding Description}" FontSize="40" Width="50" TextAlignment="Right" />
                            </StackPanel>
                        </Button.Content>
                    </Button>
                    <ContentControl cal:View.Model="{Binding}" VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

我的问题是,在 silverlight/caliburn 中将模型对象集合绑定到将在导航中使用的视图的正确方法是什么。正如您所看到的,当有人单击其中一个按钮时,我将在下一个视图模型上进行另一个剩余 api 调用,以获取该项目数据并将其拍摄到屏幕上。但是,我无法使用该列表框代码让项目显示在屏幕上。我尝试使用 ItemsSource=SummaryItems,这有效,但它似乎并不位于样本的设置方式。我在示例中注意到,Items 属性在视图模型上使用,我不确定它如何与模型对象集成。

我可能只是不清楚绑定在所有这些情况下如何工作以及 calibburn 如何与之集成。有人能指出我正确的方向吗?

任何帮助将不胜感激。谢谢!

肖恩

This is more of a MVVM question than a caliburn question, but it relates to how I can accomplish it with caliburn.

I am new to Sliverlight/WP7 development so please let me know if I'm not describing myself good enough.

I have caliburn.micro wired in properly to a WP7 app with phonecontainer/simplecontainer and viewmodels etc. The problem I am running into, is how to properly bind a collection of model's to the screen.

For instance I have the following model:

SummaryItem
{
    int Id
    string Name
    string Description
}

And the corresponding viewmodel:

SummaryViewModel : Conductor<IScreen>.Collection.OneActive
{
    ObservableCollection<SummaryItem> SummaryItems;

    OnInitialize()
    {
        SummaryItems = // REST api call to load items
    }
}

And the view:

         <ListBox x:Name="SummaryItems" Height="617" HorizontalAlignment="Left" VerticalAlignment="Top" Width="468" Background="Transparent">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Button Width="460" Height="120">
                        <Button.Content>
                            <StackPanel Orientation="Horizontal" Height="120" Width="400">
                                <TextBlock Text="{Binding Id}" Height="120" FontSize="40" Width="350" />
                                <TextBlock Text="{Binding Name}" Height="120" FontSize="40" Width="350" />
                                <TextBlock Text="{Binding Description}" FontSize="40" Width="50" TextAlignment="Right" />
                            </StackPanel>
                        </Button.Content>
                    </Button>
                    <ContentControl cal:View.Model="{Binding}" VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

My question is, What is the proper way in silverlight/caliburn to bind a collection of model objects to the view that you will use in navigation. As you can see, when someone clicks one of the buttons I will make another rest api call on the next viewmodel to get that items data and shot it on the screen. However, I can't get the items to appear on the screen using that listbox code. I tried using ItemsSource=SummaryItems, and that worked but it doesn't seem to be situated how the samples are seutp. I've noticed in the samples, that the Items property is used on the viewmodel's and I'm not sure how this integrates with the model objects.

I probably just don't have a clear view of how binding works in all these situations and how caliburn integrates with that. Could anyone point me in the right direction?

Any help would be greatly appreciated. Thanks!

Sean

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你与昨日 2024-10-29 19:22:31

您需要将项目添加到 ObservableCollection,而不是通过调用创建新的 ObservableCollection:

SummaryViewModel : Conductor<IScreen>.Collection.OneActive
{
    private readonly ObservableCollection<SummaryItem> _items;
    public ObservableCollection<SummaryItem> SummaryItems 
    {
        get 
        {
            return _items;
        }
    }
    ObservableCollection<SummaryItem> SummaryItems;

    public SummaryViewModel() 
    {
        _items = new ObservableCollection<SummaryItem>();
    }

    protected override void OnInitialize()
    {
        var items = MyRestCall();
        SummaryItems.Clear();
        foreach(SummaryItem s in items)
        {
            SummaryItems.Add(s);
        }
    }
}

You need to add the items to your ObservableCollection rather than creating a new ObservableCollection from the call:

SummaryViewModel : Conductor<IScreen>.Collection.OneActive
{
    private readonly ObservableCollection<SummaryItem> _items;
    public ObservableCollection<SummaryItem> SummaryItems 
    {
        get 
        {
            return _items;
        }
    }
    ObservableCollection<SummaryItem> SummaryItems;

    public SummaryViewModel() 
    {
        _items = new ObservableCollection<SummaryItem>();
    }

    protected override void OnInitialize()
    {
        var items = MyRestCall();
        SummaryItems.Clear();
        foreach(SummaryItem s in items)
        {
            SummaryItems.Add(s);
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文