我一直在关注Winforms教程,并使用Caliburn.Micro将其转换为WPF和MVVM。但是,在winforms教程中,它会调用一个新表单。

CreatePrizeForm frm = new CreatePrizeForm();
frm.Show;

我以为我可以使用Caliburn.Micro ActivateItem做类似的事情。我已经包含了一些实验代码。

我的ShellViewModel
public class ShellViewModel : Conductor<object>
{
    public void LoadFormOne()
    {
        MessageBox.Show("You are about to activate FirstChildViewModel");
        ActivateItem(new FirstChildViewModel());
    }

    public void LoadFormTwo()
    {
        MessageBox.Show("You are about to activate SecondChildViewModel");
        ActivateItem(new SecondChildViewModel());
    }
}

我的Shellview
<Window x:Class="ConductorTest.ShellView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:ConductorTest"
    mc:Ignorable="d"
    Title="ShellViewModel" Height="450" Width="800">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <Button Grid.Column="1" Grid.Row="1"  x:Name="LoadFormOne" Content="Load Form One" />
    <Button Grid.Column="1" Grid.Row="3"  x:Name="LoadFormTwo" Content="Load Form Two" />
</Grid>
</Window>

一个ChildViewModel
    public class FirstChildViewModel : Screen
    {
        public FirstChildViewModel()
        {
        }
    }

child 观
<Window x:Class="ConductorTest.FirstChildView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:ConductorTest"
    mc:Ignorable="d"
    Title="FirstChildView" Height="450" Width="800">
<Grid>
    <TextBlock>You are now in First Child View</TextBlock>
</Grid>
</Window>

所以我的想法是,如果我单击LoadFormOne按钮,它将执行,
ActivateItem(new FirstChildViewModel());

然后会创建一个FirstChildViewModel(),就像
DisplayRootViewFor<ShellViewModel>();

然后在标准bootstrapper.cs中启动新的WPF表单。

但显然不是。

我缺少什么或Caliburn.Micro没有提供简便的方法?

谢谢

最佳答案

为此有一个IWindowManager接口(interface)。

public class ShellViewModel : Conductor<object> {
    private readonly IWindowManager window;

    public ShellViewModel(IWindowManager window) {
        this.window = window;
    }

    public void LoadFormOne() {
        MessageBox.Show("You are about to activate FirstChildViewModel");
        var model = new FirstChildViewModel();
        ActivateItem(model);
        window.ShowDialog(model);
        //or
        //window.ShowWindow(model); //For non-modal
        DeactivateItem(model);
    }
}

关于c# - 有没有简单的Cailburn.Micro方法来显示新的窗口/对话框?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52140832/

10-12 15:52