我一直在关注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()
{
}
}
一个ChildView
<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
界面。
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);
}
}
感谢Nkosi指出我正确的方向;看了他的回答,我发现了这个链接https://csharp.hotexamples.com/examples/Caliburn.Micro/WindowManager/ShowDialog/php-windowmanager-showdialog-method-examples.html
然后我修改了我的代码工作。
public class ShellViewModel : Conductor<object>
{
private readonly IWindowManager window = new WindowManager();
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
bool CloseItemAfterDeactivating = true;
DeactivateItem(model, CloseItemAfterDeactivating);
}
}
注意事项
DisplayRootViewFor<ShellViewModel(new WindowManager())>();
将新的WindowManager发送到ShellViewModel中,因此您无法使用ShellViewModel的构造函数来接受WindowManager。所以我将'window'改为private,在ShellViewModel启动时初始化。
编译器抱怨您需要第二个参数“close:指示在停用后是否关闭该项目。”。我已经将代码更改为第二个参数。
我希望将来可以帮助别人。