因此,我在本示例中使用 staticextension 引用了一个枚举,它在运行时工作正常,但在设计时由于错误“无法创建类型为‘StaticExtension’的实例而失败。”
我理解这意味着它认为需要枚举类型的实例来实际引用它。但是,枚举在窗口中定义为静态,因此我不明白为什么它会出现问题。
有什么合理的方法可以让设计师继续工作吗?到目前为止,我发现的最接近的方法是将其放入 objectdataprovider 中并创建方法来返回枚举值。在幕后,这基本上是创建一个对象来引用静态类型,并且似乎需要太多工作才能将枚举值取出。这里的目标只是能够引用单个枚举类型并显示它们。
<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#:
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
}
}
}
最佳答案
这是已知的bug在设计师中。解决方法是不要将嵌套类型与设计器一起使用。
关于c# - WPF 设计器引用枚举问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18451597/