Wpf &引用;无法解析符号“;在ResourcesDictionary XAML中(待合并)

Wpf &引用;无法解析符号“;在ResourcesDictionary XAML中(待合并),wpf,xaml,resourcedictionary,graph-sharp,eventsetter,Wpf,Xaml,Resourcedictionary,Graph Sharp,Eventsetter,我有一个文件GraphView.XAML。我已将资源部分拆分为两个资源字典文件(顶点.xaml和边.xaml),我将其合并如下: GraphView.XAML <Window x:Class="graph_app.GraphView" ... > <Grid> <Grid.Resources> <ResourceDictionary> <ResourceDictionary.MergedDic

我有一个文件GraphView.XAML。我已将资源部分拆分为两个资源字典文件(顶点.xaml边.xaml),我将其合并如下:

GraphView.XAML

<Window x:Class="graph_app.GraphView" ... >

<Grid>
    <Grid.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Design/Vertices.xaml"/>
                <ResourceDictionary Source="Design/Edges.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Grid.Resources>
    ...
<\Grid>
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:graph_app">

      <Style TargetType="{x:Type controls:VertexControl}">
          <EventSetter Event="MouseDoubleClick" Handler="ChangeVertexColor_OnClick"/>
                                                        ^^^cannot resolve symbol^^^^
      </Style>
</ResourceDictionary>

...
如果不拆分代码,则代码可以工作,但如果拆分,则会在顶点.xaml中出现错误,告诉我无法解决方法ChangeVertexColor\u OnClick

顶点.XAML

<Window x:Class="graph_app.GraphView" ... >

<Grid>
    <Grid.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Design/Vertices.xaml"/>
                <ResourceDictionary Source="Design/Edges.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Grid.Resources>
    ...
<\Grid>
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:graph_app">

      <Style TargetType="{x:Type controls:VertexControl}">
          <EventSetter Event="MouseDoubleClick" Handler="ChangeVertexColor_OnClick"/>
                                                        ^^^cannot resolve symbol^^^^
      </Style>
</ResourceDictionary>

^^^无法解析符号^^^^
我重复一遍,如果放入一个XAML,代码就可以工作了。此外,ChangeVertexColor\u OnClick方法是在GrapView.xaml.cs中实现的,因此应该可以识别它,但是在分割顶点之后,xaml不知怎的失去了对x:Class的跟踪(我认为它忽略了它的存在,因为它是一个单独的文件)

如何从分离的ResourcesDictionary文件中访问ChangeVertexColor\u OnClick


谢谢

这里没有发生奇怪的事情:)-它不应该工作,因为这是它的设计方式。我可以看到两种可能的解决方案:

  • 为您的资源字典添加codebehind文件,并将您的
    ChangeVertexColor\u OnClick
    方法放在那里
  • 不要在样式中设置MouseDoubleClick,而是直接在
    控件:VertexControl
    上设置(这样,恢复分割更改)

  • 谢谢@walkerbox,有了解决方案#1,我现在可以执行OnClick方法,但是由于它使用了当前窗口(名称空间:graph_app)的属性(从中接收单击),我想知道从新类(名称空间:graph_app.Design)访问它们的更好方法是什么。非常感谢。我不希望以样式设置事件,也不希望在资源字典中使用代码隐藏。如果您想编写漂亮的代码,请阅读有关MVVM模式的内容。它是WPF/UWP的完美选择。若你们是新手,MVVM是一个更大的话题,所以作为一个解决办法,你们可以:1。未在ResourceDictionary或2中设置事件。编写一个帮助器类来存储您的数据。我已经实现了帮助器,现在一切都很顺利!谢谢@walkerbox