绑定时出现“方法或操作未实现”错误

8
我目前正在开发一个Visual Studio插件(VSPackage),最终应该能够可视化调用关系。为了表示它们,我想使用Graph#库,该库管理图形(避免重叠边等)。 不幸的是,在我的XAML中运行时出现以下错误消息: XamlParseException:方法或操作未实现。 该错误在标签上弹出。
<UserControl x:Class="Biocoder.InteractiveExploration.View.ExplorationControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:graphsharp="clr-namespace:GraphSharp.Controls;assembly=GraphSharp.Controls"
         xmlns:zoom="clr-namespace:WPFExtensions.Controls;assembly=WPFExtensions"
         xmlns:graph="clr-namespace:Biocoder.InteractiveExploration.Graph"
         xmlns:viewmodels="clr-namespace:Biocoder.InteractiveExploration.ViewModel"
         xmlns:controls="clr-namespace:Biocoder.InteractiveExploration.Controls" mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">



<UserControl.DataContext>
    <viewmodels:ExplorationToolViewModel/>
</UserControl.DataContext>

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>

    <zoom:ZoomControl Grid.Row="1"
                      Zoom="0.2"
                      ZoomBoxOpacity="0.5"
                      Background="Yellow">

        <graph:CallRelationGraphLayout Graph="{Binding RelationGraph}"/>

    </zoom:ZoomControl>

</Grid>

</UserControl>

我还创建了自己的顶点、边和图形布局类。我的图表最终应该表示方法(顶点)之间的调用关系(边)。

MethodVertex.cs

public class MethodVertex
{
    public string ID { get; private set; }
    public bool IsMale { get; private set; }

    public MethodVertex(string id, bool isMale)
    {
        ID = id;
        IsMale = isMale;
    }

    public override string ToString()
    {
        return string.Format("{0}-{1}", ID, IsMale);
    }
}

RelationEdge.cs

public class RelationEdge : Edge<MethodVertex>
{
    public string Id { get; private set; }

    public RelationEdge(string id, MethodVertex source, MethodVertex target)
        : base(source, target)
    {
        Id = id;
    }
}

CallRelationGraphLayout.cs

public class CallRelationGraphLayout : GraphLayout<MethodVertex, RelationEdge, CallRelationGraph>
{}

CallRelationGraph.cs

public class CallRelationGraph : BidirectionalGraph<MethodVertex, RelationEdge>
{
    public CallRelationGraph()
    {}

    public CallRelationGraph(bool allowParallelEdges)
        : base(allowParallelEdges)
    { }

    public CallRelationGraph(bool allowParallelEdges, int vertexCapacity)
        : base(allowParallelEdges, vertexCapacity)
    {}
}

ExplorationToolViewModel 中,我声明了 RelationGraph 如下:
private CallRelationGraph _relationGraph;
public CallRelationGraph RelationGraph
{
    get { return _relationGraph; }
    set
    {
        if (value != _relationGraph)
        {
            _relationGraph = value;
            NotifyPropertyChanged("RelationGraph");
        }
    }
}

public event PropertyChangedEventHandler PropertyChanged;

public void NotifyPropertyChanged(string propertyName)
{
    if (PropertyChanged != null)
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}

我也许应该提一下,有时候会显示以下错误,但项目可以编译和运行。 GenericArguments [1],“Biocoder.InteractiveExploration.Graph.RelationEdge”,在“GraphSharp.Algorithms.Layout.ILayoutAlgorithm`3 [TVertex,TEdge,TGraph]”上违反了类型“TEdge”的约束。 也许这是问题的源头,但由于它已经编译了,并且我按照tutorial进行操作,所以我忽略了它。
奇怪的是,在使用Graph#提供的DLL的普通WPF应用程序中,它实际上是有效的。当我省略Graph属性时,错误不会出现,因此我想它与Graph属性有关。有关如何解决此问题的任何提示都将不胜感激!
非常感谢!

@Palesz 谢谢你,Palesz!我刚刚添加了我的顶点、边和布局类。基本上我做的一切都像Sacha Baber的博客中所示。 - Biocoder
"NotImplementedException" 确实帮不上什么忙!我已经撰写了一份指南,介绍如何在 stackoverflow.com/a/74460800/1688738 上诊断潜在问题。 - Hugh W
2个回答

2
我在使用VSPackage中的Graph#时遇到了同样的问题。通过在CodeBehind中分配"Graph"属性,而不是使用图形的绑定,我成功地解决了这个问题。
当分配"Graph"属性时,会出现无法加载WPFExtensions程序集的异常。我怀疑原因是在GraphSharp.Controls中,在XAML中使用了该程序集,但在编译时没有添加该引用,因为在代码中没有任何引用。在分配"Graph"属性之前,我可以通过添加以下行来修复此问题:
var a = System.Reflection.Assembly.Load("WPFExtensions, Version=1.0.3437.34043, Culture=neutral, PublicKeyToken=null");

这行代码在XAML中的引用加载WPFExtensions库之前先加载它。这样做可以确保图形正确显示。


这对我有用!只需在 XAML 页面的构造函数中添加 System.Reflection.Assembly.Load("RequiredLibrary, Version={VersionNumber}, Culture=neutral, PublicKeyToken=null");,然后再加上 InitializeComponent(); 就可以解决问题了。谢谢! - klugerama

1
在我的情况下,一个程序集没有被复制到输出文件夹,因为复制本地设置为false。将复制本地设置为true即可解决问题。(程序集A依赖于没有复制本地标志的程序集B。)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接