使用Graph#库时出现XamlParseException



我正试图在我的VSPackage项目中使用Graph#库,但不幸的是,还有一些障碍需要克服。以下是我所做的:

我将以下所有DLL复制到项目根目录中的文件夹/库中:

  • GraphSharp.dll
  • GraphSharp。Controls.dll
  • QuickGraph.dll
  • WPF扩展.dll

所有的生成操作都是"内容","复制到输出"选项设置为"不复制"。

我在我的项目中添加了这些参考资料。(添加参考…->浏览->从/Library文件夹中选择它们)

之后,我创建了以下文件。您可以看到ViewModel被设置为UserControl的DataContext,并且它定义了UI绑定到的"MethodGraph"。

XAML文件

<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"
mc:Ignorable="d" 
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<ListView Grid.Row="0" ItemsSource="{Binding SelectedMethods}">
<ListView.View>
<GridView>
<GridViewColumn Header="Method" DisplayMemberBinding="{Binding Name}"/>
<GridViewColumn Header="ReturnType" DisplayMemberBinding="{Binding ReflectionInfo.ReturnType}"/>
<GridViewColumn Header="Incoming Calls"/>
<GridViewColumn Header="Outgoing Calls"/>
</GridView>
</ListView.View>
</ListView>
<graphsharp:GraphLayout Graph="{Binding MethodGraph}"/>
</Grid>
</UserControl>

代码隐藏

public partial class ExplorationControl : UserControl
{
public ExplorationControl()
{
InitializeComponent();
// set the datacontext
DataContext = InteractiveExplorationPackage.ExplorationToolViewModel;
}
}

ViewModel

public class ExplorationToolViewModel : ViewModelBase
{
private IBidirectionalGraph<object, IEdge<object>> _methodGraph;
public IBidirectionalGraph<object, IEdge<object>> MethodGraph
{
get { return _methodGraph; }
set
{
if (value != _methodGraph)
{
_methodGraph = value;
NotifyPropertyChanged("MethodGraph");
}
}
}
public ExplorationToolViewModel()
{
InitializeViewModel();
}
private void InitializeViewModel()
{
SelectedMethods = new ObservableCollection<Method>();
CreateGraph();
}
private void CreateGraph()
{
var g = new BidirectionalGraph<object, IEdge<object>>();
// add vertices
string[] vertices = new string[5];
for (int i = 0; i < 5; i++)
{
vertices[i] = i.ToString();
g.AddVertex(vertices[i]);
}
// add edges
g.AddEdge(new Edge<object>(vertices[0], vertices[1]));
g.AddEdge(new Edge<object>(vertices[1], vertices[2]));
g.AddEdge(new Edge<object>(vertices[2], vertices[3]));
g.AddEdge(new Edge<object>(vertices[3], vertices[1]));
g.AddEdge(new Edge<object>(vertices[1], vertices[4]));
MethodGraph = g;
}
}

幸运的是,我可以编译整个项目,但在运行时,XAML中的以下错误发生在标记上(就在所需标记的正上方):

无法加载文件或程序集"GraphSharp"。控件,PublicKeyToken=null'或其依赖项之一。系统找不到该文件

但我引用了程序集,它们列在引用列表中。怎么了?在编写Visual Studio包(插件)时,是否必须以另一种方式引用程序集?

EDIT:我只是想在另一个项目中实现它,所以我只设置了一个普通的WPF应用程序,并完成了以上所有操作。在这个解决方案中,一切正常!太奇怪了

希望你能帮我:-)顺致敬意,

您是否能够在项目中使用nuget?如果是这样,您应该尝试运行install-package graphsharp命令,而不是手动复制dll-s和引用。

我认为这是某种DLL strong name问题。

你试过这个吗?http://shfb.codeplex.com/workitem/32819

上面写着:

我遇到了这样一个问题,在讨论中有人讨论过。你需要做的是打开项目属性>路径,然后删除所有路径(只保留它们为空)

我现在设法让一切正常工作。无论如何,谢谢你的帮助!

VSPackages在运行时的问题主要是它们从计算机上名为专用程序集文件夹的另一个位置加载第三方程序集。(C:\Program Files(x86)\Microsoft Visual Studio 10.0\Common7\IDE\PrivateAssemblys)因此,您所要做的就是将引用的程序集复制到此文件夹中(需要管理员权限)。为了能够在开发时使用程序集,只需像往常一样将这些引用添加到项目中,编译器就会以通常的方式使用它们。唯一的区别是在运行时,因为Visual Studio从"专用程序集"文件夹加载这些引用。

有关这方面的更多信息可以在这里找到:托管VSPackage文件位置密钥

其他解决方案:

  1. 将库作为程序集资产添加到.vsixmanifest文件中:
  2. 将此代码添加到包初始化代码中
protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress<ServiceProgressData> progress)
{
AppDomain.CurrentDomain.AssemblyResolve += LoadFromSameFolder;
static Assembly? LoadFromSameFolder(object sender, ResolveEventArgs args)
{
var executingAssemblyPath = Assembly.GetExecutingAssembly().Location;
var folderPath = Path.GetDirectoryName(executingAssemblyPath) ?? string.Empty;
var assemblyPath = Path.Combine(folderPath, $"{new AssemblyName(args.Name).Name}.dll");
return File.Exists(assemblyPath)
? Assembly.LoadFrom(assemblyPath)
: null;
}
// your code
}

最新更新