简体   繁体   中英

How to get the uri of ResourceDictionary.XAML in WPF

if you want to change the ResourceDictionary in code, you have to write a long path, like new Uri(@"pack://application:,,,/MyProject;component/System/Language/Window1_EN.xaml", UriKind.Absolute) . Is there any way I can use by using the file name (Window11_EN.xaml) to get its Path of Project (/System/Language) ?

first of all you need to have property that could store your file path

public string xmlfilepath{get;set;}

now, all you have to do is to get application base directory, and concatenate with your path, as an example i have created:

 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";

or if it is in the bin/debug try using this one

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

That's my problem. I have a Window1_EN.xaml resourcedictionary file, like the following picture shows. we can use uri syntax to get the file according to its directory in Solution Explorer. we don't need to copy the folder into Bin/Debug directory. But it need to write it manually. Is the anyway I can code it?

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM