我正在尝试使用 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
,但是我必须解析文本和代码气味。我已经做了一些谷歌搜索,我得到的最接近的答案是在这里 -从 XAML 传递一个枚举值作为命令参数不幸的是,Silverlight 不支持 x:static 绑定扩展,所以我不能使用该答案中描述的确切技术。
任何帮助将非常感激。