ArcGIS AddIN開發,winform的樣式確實不太好看,而且高清屏上老是出現縮放問題。最近在轉WPF,WPF的界面確實美觀了不少,最近發現了一個WPF開源UI ,能否使用該類庫製作更酷炫的AddIN界面呢?
地址:https://github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit
簡易教程:https://github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit/wiki/Super-Quick-Start
下面介紹一下如何在ArcGIS AddIN項目中使用該類庫
1.Nuget中添加MaterialDesign類庫
在管理NuGet程序包界面中,搜索MaterialDesign,點擊安裝即可將MaterialDesignXamlToolkit相關類庫添加至項目中。
2.在WPF窗體中配置資源
官網的教程是在App.xaml中配置MaterialDesign相關資源,而ArcMap AddIN項目是無App.xaml文件,查閱相關資料,可以在具體窗體的.xaml文件中增加<Window.Resource>節點進行配置。示例代碼如下:
<Window x:Class="WaterAssisterToolbar.AttrSpecialStrRemove.RemoveAttrSpecialStrFrm"
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"
mc:Ignorable="d"
Title="設置-移除特殊字符"
ResizeMode="NoResize"
Height="330" Width="350" MaxWidth="350" MaxHeight="330">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.DeepPurple.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Lime.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid Name="RootGrid">
其他界面代碼:。。。。
</Grid>
</Window>
美化前後的界面:
3.其他處理
上述配置編譯後,彈出界面時,出現如下異常:
大體問題是 沒有找到MaterialDesign相關類庫
百度、Google了一大堆帖子,沒有找到有價值的解決方案。
之後經過測試發現,ArcGIS AddIN插件的運行環境與WPF的運行環境存在一些區別,相當於ArcGIS AddIn的運行類庫是在ArcGIS的安裝目錄下,默認的是C:\Program Files (x86)\ArcGIS\Desktop10.3\bin,而ArcGIS AddIN在安裝的時候,安裝的路勁爲 C:\Users\<用戶名>\AppData\Local\ESRI\Desktop10.3\AssemblyCache\{插件Guid},包括AddIN項目編譯的類庫以及添加的第三方類庫。因此,ArcGIS AddIN插件中的WPF窗體,在顯示的時候,無法獲取到MaterialDesign類庫,導致產生上述異常。
解決方法:將MaterialDesign相關類庫拷貝到ArcGIS的安裝目錄下,即可解決上述問題。可以在InitialComponent()方法之前,判斷MaterialDesign類庫是否存在,如果不存在,去AddIN的目錄中將這些類庫拷貝至運行目錄即可。代碼如下:
public class LibraryBaseHelper
{
/// <summary>
/// 類庫初始化
/// </summary>
/// <param name="le"></param>
public static void Initial(LibraryEnum le)
{
//獲取ArcGIS安裝路徑
string targetPath = ESRI.ArcGIS.RuntimeManager.ActiveRuntime.Path;
targetPath = targetPath+"\\bin";
Type tp = typeof(LibraryBaseHelper);
string location = tp.Assembly.Location;
location = System.IO.Path.GetDirectoryName(location);
DirectoryInfo di = new DirectoryInfo(location);
FileInfo[] fis = di.GetFiles();
for (int i = 0; i < fis.Length; i++)
{
string dllpath = System.IO.Path.Combine(targetPath, fis[i].Name);
if (!File.Exists(dllpath))
{
if (detect(fis[i], le))
{
fis[i].CopyTo(dllpath);
}
}
}
}
private static bool detect(FileInfo fi, LibraryEnum le)
{
bool re=false;
switch (le)
{
case LibraryEnum.MaterialDesign:
if (fi.Name.Contains("MaterialDesign"))
{
re = true;
}
break;
}
return re;
}
public enum LibraryEnum
{
MaterialDesign = 0
}
}
在WPF窗體的構造函數中增加對上述方法的引用即可。
public RemoveAttrSpecialStrFrm()
{
GISCommonHelper.LibraryBaseHelper.Initial(GISCommonHelper.LibraryBaseHelper.LibraryEnum.MaterialDesign);
InitializeComponent();
}