【问题标题】:caliburn micro master detailcaliburn 微大师细节
【发布时间】:2013-08-14 00:49:37
【问题描述】:

我已经查看了我能找到的所有 Caliburn Micro 的东西,我想我只是让自己感到困惑。我整理了一个简单的样本作为测试。

模型 = Person.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WpfTestApp
{
    public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
}

PersonView.xaml

<UserControl x:Class="WpfTestApp.PersonView"
             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" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="{Binding FirstName}" />
            <TextBlock Text="{Binding LastName}" />
        </StackPanel>
    </Grid>
</UserControl>

ShellViewModel.cs

using Caliburn.Micro;
using System.ComponentModel.Composition;
namespace WpfTestApp {
    [Export(typeof(IShell))]
    public class ShellViewModel : PropertyChangedBase, IShell 
    {
        public BindableCollection<PersonViewModel> Items { get; set; }

        public ShellViewModel()
        {
            Items = new BindableCollection<PersonViewModel> {
                new PersonViewModel(new Person { FirstName="Bart", LastName="Simpson" }),
                new PersonViewModel(new Person { FirstName="Lisa", LastName="Simpson" }),
                new PersonViewModel(new Person { FirstName="Homer", LastName="Simpson" }),
                new PersonViewModel(new Person { FirstName="Marge", LastName="Simpson" }),
                new PersonViewModel(new Person { FirstName="Maggie", LastName="Simpson" })
            };
        }
    }
}

ShellView.xaml

<Window x:Class="WpfTestApp.ShellView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:cal="http://www.caliburnproject.org">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <StackPanel>
            <ListBox x:Name="Items"/>
        </StackPanel>
        <ContentControl cal:View.Model="{Binding SelectedItem, Mode=TwoWay}" Grid.Row="1" />
    </Grid>
</Window>

我正在按照 Caliburn Micro 文档使用 MEFBootstrapper。

1) 为什么当我在 ListBox 中选择一个项目时,ContentControl 中什么也没有出现。我显然遗漏了一些东西,但我认为 SelectedItem 被约定所吸引。我试过使用 x:Name="ActiveItem" 也没有用?

2) 如果我的 ShellViewModel.cs 包含 Person 的 BindableCollection 而不是 PersonViewModel,这将如何工作?

3) 我可以将 BindableCollection 命名为 Items 以外的其他名称(是的 - 我知道 Items 是 Caliburn Micro 的约定)?

问候 艾伦

【问题讨论】:

    标签: caliburn.micro master-detail


    【解决方案1】:

    让你在 ShellView 中的 ContentControl 为

    <ContentControl Name="ActiveItem"/>
    

    他们从 Conductor.Collection.OneActive 继承你的 ShellViewModel

    public class ShellViewModel : Conductor<IScreen>.Collection.OneActive, IShell
    {
    }
    

    由于 Conductor 已有用于绑定 ChildView 的 Items 属性,请从 ShellViewModel 中删除您的 Items 属性。

    我们还要编写代码来激活 ShellView 中的视图

    将列表框改为

    <ListBox x:Name="Items" cal:Message.Attach="[Event SelectionChanged]=[Action ActivateChildView($this.SelectedItem)]"/>
    

    ShellViewModel 内部还有一个新方法

    public void ActiveChildView(object view)
    {
        ActiveItem(view);
    }
    

    我还没有测试过,但希望这会奏效。

    【讨论】:

      【解决方案2】:
        1234563从您的视图模型中绑定数据项。
      1. 您可以改为直接绑定到Person 的集合。你可以说这破坏了Law of Demeter,但如果你的PersonViewModel 没有用任何额外的数据扩充Person 模型,那么在这种情况下你可能会考虑视图模型过剩。

      2. 是的,您可以将其命名为任何名称,并且约定仍然有效,例如Users 将按照惯例映射到 SelectedUser(或 ActiveUserCurrentUser)。 People 不会按照惯例映射到 SelectedPerson,但如果您需要此功能,您可以更改 ConventionManager Singularize 委托。

      【讨论】:

      • 如何使用绑定到 ActiveItem 的 ContentControl。您还需要 SelectedItem 属性吗?使用 Person 而不是 PersonViewModel,我添加了 public Person SelectedPerson { get { return _selectedPerson; } set { _selectedPerson = value; NotifyOfPropertyChange(() =&gt; SelectedPerson); } } public Person SelectedPerson 我仍然看不到所选人员是如何显示的。
      • 我找不到使用 Caliburn Micro 显示主/详细信息的普通示例(例如,类似于显示家庭姓氏列表的内容,选择一个项目会显示每个家庭的成员,选择一个家庭成员允许对其进行编辑/删除)。
      • 如果您使用带有集合的导体(例如,Conductor.Collection.OneActive),那么您已经有一个 Items 集合和一个 ActiveItem,因此您可以绑定到它们,例如... 和 。否则,您可以使用自己的集合和引用类型。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-17
      • 2017-11-20
      • 1970-01-01
      • 1970-01-01
      • 2019-02-26
      相关资源
      最近更新 更多