我将以下所有DLL文件复制到项目根目录下的/Libraries文件夹中:
- GraphSharp.dll
- GraphSharp.Controls.dll
- QuickGraph.dll
- WPFExtensions.dll
我将这些引用添加到了我的项目中。(添加引用... -> 浏览 -> 从 /Library 文件夹中选择它们)
之后,我创建了以下文件。您可以看到 ViewModel 被设置为 UserControl 的数据上下文,并定义了一个“MethodGraph”,供 UI 进行绑定。
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;
}
}
视图模型
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;
}
}
幸运的是,我可以编译整个项目,但在运行时,在所需的标签(<Graphsharp:GraphLayout x:Name="graphLayout">
)上出现了以下XAML错误:
无法加载文件或程序集"GraphSharp.Controls, PublicKeyToken=null"或其其中一个依赖项。系统找不到指定的文件。
但我已经引用了这些程序集,它们列在了引用列表中。 问题出在哪里呢?写一个Visual Studio Package(插件)时,程序集是否需要以另一种方式进行引用?
编辑:我刚刚尝试在另一个项目中让它正常工作,所以我只是建立了一个普通的WPF应用程序并完成了以上所有步骤。在这个解决方案中一切正常工作!太奇怪了!
希望你能帮助我 :-) 最好的问候!