简体   繁体   English

WPF设计器问题引用枚举

[英]WPF designer issue referencing enum

So I have referenced an enum in this example using the staticextension, which works fine at runtime, but fails at design time due to the error "Could not create an instance of type 'StaticExtension'." 因此,在此示例中,我使用staticextension引用了一个枚举,该枚举在运行时可以正常运行,但由于错误“无法创建'StaticExtension'类型的实例”而在设计时失败。

I understand this to mean that it thinks it needs an instance of the enum type to actually reference it. 我理解这意味着它认为它需要一个枚举类型的实例才能实际引用它。 However, the enum is defined in the window as static so I don't understand why it is having issues. 但是,该枚举在窗口中定义为静态,因此我不明白为什么会有问题。

Is there any reasonable way to keep the designer working? 有什么合理的方法可以保持设计师的工作? The closest I have found so far is to put it in an objectdataprovider and create methods to return the enum values. 到目前为止,我发现最接近的方法是将其放在objectdataprovider中,并创建方法以返回枚举值。 Behind the scenes this is basically creating an object to reference the static type and seems like too much work to just pull the enum values out. 在幕后,这基本上是在创建一个引用静态类型的对象,似乎只是为了拉出枚举值而做的工作太多。 The goal here is just to be able to reference individual enum types and display them. 这里的目标只是能够引用各个枚举类型并显示它们。

<Window 
  x:Class="DaedalusGraphViewer.GraphViewerWindow"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:DaedalusGraphViewer="clr-namespace:DaedalusGraphViewer">
 <StackPanel>
    <Label Content="testtesttest">
      <Label.ContextMenu>
        <ContextMenu>
          <MenuItem Header="{x:Static DaedalusGraphViewer:GraphViewerWindow+Test.test1}"/>
        </ContextMenu>
      </Label.ContextMenu>
    </Label>
  </StackPanel>  
</Window>

C#: C#:

using System.Windows;

namespace DaedalusGraphViewer
{
  /// <summary>
  /// Interaction logic for GraphViewerWindow.xaml
  /// </summary>
  public partial class GraphViewerWindow : Window
  {
    public GraphViewerWindow()
    {
      InitializeComponent();
      Application.Current.MainWindow = this;
    }

    public enum Test
    {
      test1, test2
    }
  }
}

This is a known bug in designer. 这是设计中的已知错误 The workaround is not to use nested types with designer. 解决方法是不要在设计器中使用嵌套类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM