我需要编写一个小应用程序来读取配置文件并使用它生成一些报告。我希望最终能够使用MVVM,但开始使用起来相当棘手。哦,我正在使用Caliburn.Micro框架。
所以这就是我所拥有的,一个shell(托管其他视图的主视图),它有一个带有3个按钮的功能区:
1)打开文件 2)显示设置 3)显示结果
另外两个视图,SettingsView和ResultsView,带有生成和删除报告的按钮。
所以我猜视图结构会是这样的:
ShellView
Ribbon
OpenFileButton
SettingsButton
ResultsButton
ContentControl (hosts SettingsView and ResultsView)
SettingsView
CalculateResultsButton
ResultsView
CancelResultsButton
棘手的部分是:
1. "Show settings" button is disabled until a file is opened (via Open file).
2. "Show results" button is disabled until a report is calculated (via a
method in SettingsViewModel).
3. If a report is calculated, the CalculateResultsButton is disabled and
CancelResultsButton is enabled and vice versa.
请告知我怎样才能实现这一目标?我不知道应该采取什么策略。我的非MVVM思维大脑说我应该创建一个状态变量,然后以某种方式将这些按钮绑定到该变量,但我想这不会在MVVM世界中工作,对吧?任何代码示例都非常非常感谢!
非常感谢!
答案 0 :(得分:1)
由于您使用的是CM,因此不需要任何代码隐藏。如果需要,可以删除.xaml.cs文件。
这是一个非常基本的例子,但它应该让你知道如何控制按钮的状态。在此示例中,将启用Open
,并禁用其他两个。如果您点击Open
,则会启用Settings
。单击Results
后,Settings
也会发生同样的情况。
如果需要一种方法来执行全局状态,可以通过向ViewModel中注入单个SharedViewModel
来应用相同的概念,并且CanXXX方法可以检查SharedViewModel
中的值。 This是一个不同东西的SL演示,但其中一个是注入单例来共享数据,同样的想法适用于wpf。
ShellView:
<Window x:Class="CMWPFGuardSample.ShellView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<StackPanel Grid.Row="0"
Orientation="Horizontal">
<Button x:Name="Open"
Content="Open" />
<Button x:Name="Settings"
Content="Settings" />
<Button x:Name="Results"
Content="Results" />
</StackPanel>
</Grid>
</Window>
ShellViewModel:
[Export(typeof (IShell))]
public class ShellViewModel : PropertyChangedBase, IShell
{
private bool _isOpen;
public bool IsOpen
{
get { return _isOpen; }
set
{
_isOpen = value;
NotifyOfPropertyChange(() => IsOpen);
NotifyOfPropertyChange(() => CanSettings);
}
}
private bool _isSettings;
public bool IsSettings
{
get { return _isSettings; }
set
{
_isSettings = value;
NotifyOfPropertyChange(() => IsSettings);
NotifyOfPropertyChange(() => CanResults);
}
}
public bool IsResults { get; set; }
public void Open()
{
IsOpen = true;
}
public bool CanSettings
{
get { return IsOpen; }
}
public void Settings()
{
IsSettings = true;
}
public bool CanResults
{
get { return IsSettings; }
}
public void Results()
{
}
}
答案 1 :(得分:0)
MVVM和WPF命令完全符合您的“棘手部分”要求,因为内置了ICommand.CanExecute()方法,允许根据自定义逻辑启用/禁用相应的按钮。
要使用此naice功能,请先查看MSDN RoutedCommand Class上的How to: Enable a Command和自解释示例(请参阅下面的代码段)。
总的来说关于MVVM,它真的很简单!试试吧,如果没有它,你就不会离开;)简而言之 - 你必须为每个EntityView.xaml
对应的EntityViewModel
类创建,然后在代码中明确地将它的实例放入View的DataContext中或使用绑定:
var entityViewModel = new EntityViewModel();
var view = new EntityView();
view.DataContext = entityViewModel;
MVVM Command and Command.CanExecute绑定:
<强> XAML:强>
<Window x:Class="WCSamples.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="CloseCommand"
Name="RootWindow"
>
<Window.CommandBindings>
<CommandBinding Command="ApplicationCommands.Close"
Executed="CloseCommandHandler"
CanExecute="CanExecuteHandler"
/>
</Window.CommandBindings>
<StackPanel Name="MainStackPanel">
<Button Command="ApplicationCommands.Close"
Content="Close File" />
</StackPanel>
</Window>
C#代码背后:
// Create ui elements.
StackPanel CloseCmdStackPanel = new StackPanel();
Button CloseCmdButton = new Button();
CloseCmdStackPanel.Children.Add(CloseCmdButton);
// Set Button's properties.
CloseCmdButton.Content = "Close File";
CloseCmdButton.Command = ApplicationCommands.Close;
// Create the CommandBinding.
CommandBinding CloseCommandBinding = new CommandBinding(
ApplicationCommands.Close, CloseCommandHandler, CanExecuteHandler);
// Add the CommandBinding to the root Window.
RootWindow.CommandBindings.Add(CloseCommandBinding);