c# - 在 WPF 窗口 ctor 之外找不到文件路径
问题描述
我在我的 WPF 应用程序中使用以下世界热图: https ://lvcharts.net/App/examples/v1/wpf/GeoHeatMap
//Geomap
private static string GEOMAP_PATH = System.IO.Path.GetDirectoryName(System.AppDomain.CurrentDomain.BaseDirectory) + "\\Materials\\GeoMaps\\World.xml";
public MainMaterialWindow()
{
//Default application initialization
InitializeComponent();
//geomap chart
var values = new Dictionary<string, double>();
foreach (actual_headmapdata data in GetWorldHeadmapData())
{
values[data.isocode] = data.count;
}
geoMap1.HeatMap = values;
var lang = new Dictionary<string, string>();
lang["DE"] = "Germany"; // change the language if necessary
geoMap1.LanguagePack = lang;
geoMap1.Source = GEOMAP_PATH;
}
我不明白的是,如果我这样做,为什么找不到文件路径。
//Geomap
private static string GEOMAP_PATH = System.IO.Path.GetDirectoryName(System.AppDomain.CurrentDomain.BaseDirectory) + "\\Materials\\GeoMaps\\World.xml";
public MainMaterialWindow()
{
//Default application initialization
InitializeComponent();
//init GeoMap
initGeoMap();
}
public void initGeoMap() {
//geomap chart
var values = new Dictionary<string, double>();
foreach (actual_headmapdata data in GetWorldHeadmapData())
{
values[data.isocode] = data.count;
}
geoMap1.HeatMap = values;
var lang = new Dictionary<string, string>();
lang["DE"] = "Germany"; // change the language if necessary
geoMap1.LanguagePack = lang;
geoMap1.Source = GEOMAP_PATH;
}
我得到:System.IO.FileNotFoundException:“找不到这个文件。”
所以问题是,为什么一旦地理地图的初始化在 MainMaterialWindow() 之外,文件路径就不再有效。文件仍然存在。
XAML:
<lvc:GeoMap x:Name="geoMap1" HeatMap="{Binding Values}" LanguagePack="{Binding LanguagePack}" Margin="10,0,-12,22" />
完整的文件路径作为字符串也会发生同样的情况:GEOMAP_PATH="D:\\Dev\\projectname\\bin\\debug\\Materials\\GeoMaps\\World.xml"
这也只适用于 MainMaterialWindow(),所以它不是关于:
私有静态字符串 GEOMAP_PATH = System.IO.Path.GetDirectoryName(System.AppDomain.CurrentDomain.BaseDirectory)
解决方案
推荐阅读
- node.js - 使节点等待数据库调用完成
- swift - UIAlertController 崩溃错误
- javascript - 使用 Firebase 令牌进行身份验证
- javascript - 如何验证按钮单击 jquery/javascript 上的登录表单?
- android - 片段上的RecyclerView分页无限滚动问题
- sockets - ipvsadm 未显示 kubeadm 集群中的任何条目
- python - 如何在 Tensorflow 中导入 keras.engine.topology?
- c++ - 配置 Visual Studio 2017 以使用现有的 Android SDK 和 NDK 组件
- python - 来自python的arduino自动LED开/关信号
- postgresql - 由于关系列不存在,无法在 postgresq 中加载 CSV 文件