簡體   English   中英

如何在WPF中獲取ResourceDictionary.XAML的uri

[英]How to get the uri of ResourceDictionary.XAML in WPF

如果要在代碼中更改ResourceDictionary,則必須編寫一條長路徑,例如new Uri(@"pack://application:,,,/MyProject;component/System/Language/Window1_EN.xaml", UriKind.Absolute) 通過使用文件名(Window11_EN.xaml)來獲取其項目路徑(/ System / Language),可以使用任何方法嗎?

首先,您需要具有可以存儲文件路徑的屬性

public string xmlfilepath{get;set;}

現在,您要做的就是獲取應用程序基本目錄,並與您的路徑連接,例如我創建的示例:

 static string path = System.AppDomain.CurrentDomain.BaseDirectory;
        static string debug = Path.GetDirectoryName(path);
        static string bin = Path.GetDirectoryName(debug);
        static string projectfolder = Path.GetDirectoryName(bin);
        public string xamlFilepath = projectfolder + "\\System\\Language\\Window1_EN.xaml";

或者如果它在垃圾箱/調試中,請嘗試使用此工具

xmlfilepath= System.AppDomain.CurrentDomain.BaseDirectory + "your folder name and file name path will come here";

那是我的問題。 我有一個Window1_EN.xaml resourcedictionary文件,如下圖所示。 我們可以使用uri語法根據解決方案資源管理器中的目錄獲取文件。 我們不需要將文件夾復制到Bin / Debug目錄。 但是它需要手動編寫。 反正我可以編碼嗎?

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM