【问题标题】:Visual Studio WPF designer crashes when using a + in a binding在绑定中使用 + 时,Visual Studio WPF 设计器崩溃
【发布时间】:2013-10-29 06:59:59
【问题描述】:

我的 Visual Studio 2010 设计器因以下错误而崩溃:

Error   3   Type 'vm:MessageViewModel+MessageAction' was not found.

这是导致错误的行:

<Button Content="View" Command="{Binding Path=ActionCommand}" CommandParameter="{x:Static vm:MessageViewModel+MessageAction.OpenView}"/>

请记住,程序编译并运行良好。参数甚至可以正确传递给命令。

枚举在一个类中,所以我需要使用加号来引用它。这是类结构:

public class MessageViewModel : ModelWrapViewModel<MessageModel>
{
    private ICommand _actionCommand;

    public enum MessageAction
    {
        OpenView,
        OpenNote,
        OpenAcknowledge,
        Cancel,
        Save,
        Acknowledge
    }

    public ICommand ActionCommand
    {
        get
        {
            if (_actionCommand == null)
            {
                _actionCommand = new RelayCommand(
                    param => this.DoSomething((MessageAction)param),
                    param => true
                );
            }
            return _actionCommand;
        }
    }

}

只是想知道这是否有原因在 VS 设计器中不起作用。如果我将枚举移到 MessageViewModel 类之外,则设计器不会崩溃。

【问题讨论】:

  • 你不能这样吗? vm:MessageViewModel.MessageAction.OpenView
  • 不,这在 WPF 中的语法不正确。如果要在 xaml 中引用类内部的类型,请使用 +。使用 . 甚至不会编译,所以这是倒退了一步。
  • 好的,谢谢你的信息:)

标签: c# wpf visual-studio-2010 mvvm


【解决方案1】:

视觉工作室有时会很痛苦,不是吗?不确定这是否会有所帮助,但您能否将其设为动态资源,以便仅在运行时加载。希望这会有所帮助 - 里克 CommandParameter="{DynamicResource {x:Static vm:MessageViewModel+MessageAction.OpenView}}"/>

【讨论】:

  • 感谢您的回复。这对我在设计器中或运行程序时都不起作用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多