首页 > 解决方案 > 在 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)

标签: c#wpffilepath

解决方案


推荐阅读