我正在尝试使用Prism和MVVM模式开发应用程序。在我的UI中,我定义了一个“上一页”和“下一页”的按钮。为了在调用Web服务时使用,我定义了一个枚举类型,它将告诉我需要遍历的方向。因此,在这种情况下,按钮直接映射到枚举值。枚举定义非常简单,如下所示:
namespace CodeExpert.Book.Helpers
{
public enum BookDirection { Previous = -1, NotSet = 0, Next = 1, }
}
我已经在我的ViewModel中定义了命令和委托,并正确地分配了属性。相关代码如下:
public DelegateCommand PreviousNextCommand { get; set; }
public IndexEntriesViewModel(GlobalVariables globalVariable, IndexEntryOperations currentOperator)
{
//a bunch of initialization code.
InitializeCommands();
}
void InitializeCommands()
{
PreviousNextCommand =
new DelegateCommand(OnPreviousNextCommandExecute);
}
private void OnPreviousNextCommandExecute(BookDirection parameter)
{
//Code to process based on BookDirection
}
因此,根据这个配置,我想将一个BookDirection枚举值传递给CommandParameter。然而,我无法正确设置XAML。以下是我认为最正确的XAML:
<UserControl 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"
mc:Ignorable="d"
x:Class="CodeExpert.Book.Views.Index"
d:DesignWidth="1024"
d:DesignHeight="768"
xmlns:helpers="clr-namespace:CodeExpert.Book.Helpers"
xmlns:command="clr-namespace:Microsoft.Practices.Composite.Presentation.Commands;assembly=Microsoft.Practices.Composite.Presentation"
xmlns:common="clr-namespace:System.Windows;assembly=System.Windows.Controls"
xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows"
xmlns:input="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input">
<Button x:Name="ButtonPrevious"
HorizontalAlignment="Left"
Margin="2,1,0,1"
Width="25"
Content="<"
Grid.Column="1"
Grid.Row="1"
Height="20"
command:Click.Command="{Binding Path=CurrentIndexViewModel.PreviousNextCommand}">
<command:Click.CommandParameter>
<helpers:BookDirection.Previous />
</command:Click.CommandParameter>
</Button>
</UserControl>
我在枚举类型的BookDirection上没有获得智能提示,并在设计和编译时得到一个错误,说“类型‘BookDirection’不包含‘Previous’的定义”。有没有办法传递该枚举,或者我可能遗漏了什么?现在我将参数类型设置为
string
而非BookDirection
,这样可以工作,但需要解析文本,代码显得冗杂。我进行了一些Google搜索,最接近答案的是这里 - 从XAML传递枚举值作为命令参数不幸的是,Silverlight不支持x:static绑定扩展,因此我无法使用该答案中描述的确切技术。任何帮助都将不胜感激。