我一直在关注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提供了一种简便的方法吗?
谢谢
答案 0 :(得分:2)
有一个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);
}
}
答案 1 :(得分:1)
感谢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);
}
}
注意事项
在Bootstrapper.cs中您不能拥有
DisplayRootViewFor<ShellViewModel(new WindowManager())>();
将新的WindowManager发送到ShellViewModel中,因此您不能具有ShellViewModel的构造函数来接受WindowManager。因此,我将“窗口”更改为private,该窗口在ShellViewModel启动时进行了初始化。
您不能拥有
DeactivateItem(model);
编译器抱怨您需要第二个参数“ 关闭:指示是否在停用后关闭该项目”。我将代码更改为具有第二个参数。
我希望这对以后的人有所帮助。