5

我正在尝试使用 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 绑定扩展,所以我不能使用该答案中描述的确切技术。

任何帮助将非常感激。

4

4 回答 4

3

只需像简单对象类型一样传递命令参数。然后你可以使用直接转换到你的枚举。

于 2016-02-29T14:52:18.307 回答
1

首先,我不确定您是否可以直接从 Silverlight 中的 XAML 传递枚举。无论如何,这是 Prism 的一个小问题。看到命令在 CommandParameter 之前被设置,当命令在内部设置时会发生什么 Prism 调用 UpdateEnabledState(),它在你的 DelegateCommand 内部调用 CanExecute(object parameter) 并传递 CommandParameter(记住尚未设置)

这是Prism中DelegateCommand.cs的基本代码

 bool ICommand.CanExecute(object parameter)
    {
        return CanExecute((T)parameter);
    }

由于此时参数为“null”,因此强制转换抛出异常。这是我解决这个问题的方法。

你的枚举。

namespace CodeExpert.Book.Helpers
{
    public enum BookDirection { Previous = -1, NotSet = 0, Next = 1, }
}

这是我的委托声明,注意使用 object.. 而不是 Enum 本身。我还公开了 ViewModel 的属性,这将公开 2 个不同的方向。

public DelegateCommand<object> PreviousNextCommand { get; set; }

public BookDirection Previous { get { return BookDirection.Previous; }}
public BookDirection Next { get { return BookDirection.Next; }}

现在在您的 OnPreviousNextCommandExecute 确保您收到一个对象并将其转换回正确的枚举

private void OnPreviousNextCommandExecute(object parameter)
{

    BookDirection direction = (BookDirection)parameter;

    //Code to process based on BookDirection
}

并在 XAML 中直接绑定到公开的 BookDirection 属性。

<Button Content="Next"  Commands:Click.Command="{Binding PreviousNextCommand}" Commands:Click.CommandParameter="{Binding Next}" />
<Button Content="Previous" Commands:Click.Command="{Binding PreviousNextCommand}" Commands:Click.CommandParameter="{Binding Previous}" />

我不确定您的绑定情况,因为在我的情况下,我将 DataContext 直接设置为 ViewModel。但这应该对你有用。

对不起我糟糕的英语和口才,希望这能让你走上正轨。

于 2009-07-13T05:36:56.593 回答
1

绑定枚举确实是一个麻烦的操作,即使在 WPF 中也是如此。但似乎有一个优雅的解决方案,可以在Caliburn框架中使用。

但是,该解决方案在框架代码中不可用,而是在LOB Samples. 查找项目中的BindableEnumBindableEnumCollection<T>Caliburn.Silverlight.ApplicationFramework

HTH。

于 2009-07-20T04:11:03.017 回答
0

我自己没有尝试过,但你可能会成功使用 ValueConverter 从字符串之类的东西到你的枚举。这就是我在 Silverlight xaml 中四处寻找枚举时得到的印象。

于 2009-05-28T20:41:51.780 回答