shell caliburn micro 或其他 MVVM 框架中的更多活动屏幕/视图

发布于 2024-11-28 14:28:03 字数 345 浏览 5 评论 0原文

如果我使用 caliburn micro,是否可以在一个 shell 中拥有更多活动屏幕/视图?

像这样的 shell 视图代码 - wpf 窗口:

        <ContentControl x:Name="ActiveItem_1" Grid.Row=1/>

        <ContentControl x:Name="ActiveItem_2" Grid.Row=2/>


        <ContentControl x:Name="ActiveItem_9" Grid.Row=9/>

谢谢您的建议,或者在哪个 MVVM 中这可能?

It is possible have more active screen / view in one shell if I am using caliburn micro ?

Something like this, code for shell view - wpf window:

        <ContentControl x:Name="ActiveItem_1" Grid.Row=1/>

        <ContentControl x:Name="ActiveItem_2" Grid.Row=2/>


        <ContentControl x:Name="ActiveItem_9" Grid.Row=9/>

Thank you for advice, or in which MVVM this possible?

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

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

发布评论

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

评论(2

独行侠 2024-12-05 14:28:03

对于 AllActive 导体,您将在 ShellViewModel 中继承 Items 集合。当调用 ActivateItem(vm) 时,视图模型将被添加到 Items 集合中,而当调用 DeactivateItem(vm, close: true) 时,视图模型将被删除。然后,在 ShellView.xaml 中,您可以将 Items(视图模型的集合)绑定到 ItemsControl

MyView.xaml

<UserControl x:Class="AllActive_Test.MyView"
             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>
    <TextBlock Text="{Binding Path=MyProperty}"/>
  </Grid>
</UserControl>

MyViewModel.cs

class MyViewModel : Screen
{
    private int myVar;

    public int MyProperty
    {
        get
        {
            return myVar;
        }
        set
        {
            myVar = value;
            this.NotifyOfPropertyChange(() => MyProperty);
        }
    }
}

ShellView.xaml

<Window x:Class="AllActive_Test.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"
        Width="300" Height="300">

    <Grid Background="White">
    <ItemsControl ItemsSource="{Binding Path=Items}">
      <ItemsControl.ItemTemplate>
        <DataTemplate>
          <ContentControl cal:View.Model="{Binding}"/>
        </DataTemplate>
      </ItemsControl.ItemTemplate>
    </ItemsControl>
    </Grid>
</Window>

ShellViewModel.cs

public class ShellViewModel : Conductor<Screen>.Collection.AllActive, IShell
{
    public ShellViewModel()
    {
        for (int i = 0; i < 3; i++)
        {
            MyViewModel vm = new MyViewModel();
            vm.MyProperty = i;
            ActivateItem(vm);
        }
    }
}

包含 3 个活动屏幕的 ShellView

In case of AllActive conductor you will have inherited Items collection in your ShellViewModel. When calling ActivateItem(vm) View Model will be added into Items collection and when calling DeactivateItem(vm, close: true) - removed. Then in ShellView.xaml you can bind Items (collection of View Models) to ItemsControl.

MyView.xaml

<UserControl x:Class="AllActive_Test.MyView"
             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>
    <TextBlock Text="{Binding Path=MyProperty}"/>
  </Grid>
</UserControl>

MyViewModel.cs

class MyViewModel : Screen
{
    private int myVar;

    public int MyProperty
    {
        get
        {
            return myVar;
        }
        set
        {
            myVar = value;
            this.NotifyOfPropertyChange(() => MyProperty);
        }
    }
}

ShellView.xaml

<Window x:Class="AllActive_Test.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"
        Width="300" Height="300">

    <Grid Background="White">
    <ItemsControl ItemsSource="{Binding Path=Items}">
      <ItemsControl.ItemTemplate>
        <DataTemplate>
          <ContentControl cal:View.Model="{Binding}"/>
        </DataTemplate>
      </ItemsControl.ItemTemplate>
    </ItemsControl>
    </Grid>
</Window>

ShellViewModel.cs

public class ShellViewModel : Conductor<Screen>.Collection.AllActive, IShell
{
    public ShellViewModel()
    {
        for (int i = 0; i < 3; i++)
        {
            MyViewModel vm = new MyViewModel();
            vm.MyProperty = i;
            ActivateItem(vm);
        }
    }
}

ShellView containing 3 active Screens

若能看破又如何 2024-12-05 14:28:03

是的。这可以通过...

Conductor<T>.Collection.AllActive

参见屏幕、导体和组合文档页面。

  1. 在 ShellViewModel 中继承上面的类。
  2. 使用要在 ShellView 中显示的视图的 ViewModel 填充 ShellViewModel 的 Items 属性。
  3. 将 ShellView 中 ItemsControl(您不应该需要 ItemTemplate)的 ItemsSource 属性绑定到 ShellViewModel 上的 Items 属性。

我相当确定 Caliburn.Micro 会照顾其余的事情,但我目前无法进行测试。

Yes. This is possible with...

Conductor<T>.Collection.AllActive

See Screens, Conductors and Composition doc page.

  1. Inherit the class above in your ShellViewModel.
  2. Populate the ShellViewModel's Items property with the ViewModels for the views that you want to display within the ShellView.
  3. Bind the ItemsSource property of an ItemsControl (you should not need an ItemTemplate) in the ShellView to the Items property on the ShellViewModel.

I am fairly sure that Caliburn.Micro will look after the rest but I am unable to test at the moment.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文