WPF用户控件后台访问资源字典

那句-诺言 2013-04-27 04:23:42
小弟在做WPF控件(非WPF应用程序,没有App.xaml)的多语言功能,在网上找了好多资料,感觉用“资源字典”做出来的比较好且便于维护些,大体思路:
将每种语言用到的字符都建立一个单独的资源字典文件,文件中各项Key相同,Value就是各种语言文字,切换语言时只需要加载不同的资源文件即可。

具体如下:
我在解决方案里新建了一个文件夹Resources,所有语言的资源文件都放到此文件夹下。

在XAML里面访问没有任何问题,代码如下:

<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/StringResource.zh-CN.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>


这样添加完资源之后在后台使用

label1.Content = (string)this.Resources["WinTitle"];

语句就能得到资源文件"StringResource.zh-CN.xaml"中的"WinTitle"项的值。

但是由于要在运行时动态切换语言,所以需要用C#代码在后台控件对资源文件的访问。在网上找的方法类似于:

ResourceDictionary languageResDic = new ResourceDictionary();

languageResDic.Source = new Uri(@"Resources/StringResource.zh-CN.xaml", UriKind.RelativeOrAbsolute);

this.Resources.MergedDictionaries.Add(languageResDic);


问题来了:
在用XAML中相对路径方式来访问的时候无论怎样写都会提示找不到文件,只能通过绝对路来访问:

languageResDic.Source = new Uri(@"D:\Code\DigitalChina\Demo\WinFormToTestWPFControl\WinFormToTestWPFControl\bin\Debug\Resources", UriKind.Absolute);

怎么通过后台代码像在XAML中一样访问到资源文件呢?
PS:需要在App.xaml中添加引用的解决方案就算了,因为控件项目没有这个文件。
...全文
1415 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
HelpSystem 2014-03-12
  • 打赏
  • 举报
回复
make
一剑枯荣 2014-02-20
  • 打赏
  • 举报
回复
路过,学习了!
那句-诺言 2013-09-02
  • 打赏
  • 举报
回复
忘记结帖了,在StackOverflow上找到的路径表示方法: @"pack://application:,,,/AgentMapWPF;component/Resources/Test.xaml" AgentMapWPF为程序集名,component之后的路径为解决方案下的相对路径。这样就能访问到了。
  • 打赏
  • 举报
回复
wpf并不支持传统winform那种放在工程的资源页你的做法。 你可以把文件放到工程中,在解决方案树上鼠标右键点击文件,在属性页上把编译属性改为 Resource。这就OK了。
风一样的大叔 2013-04-27
  • 打赏
  • 举报
回复
//创建一个节点  
private void buttonCreateNode_Click(object sender, EventArgs e)  
        {  
            // Load the XML document  
            XmlDocument document = new XmlDocument();  
            document.Load("../../Books.xml");  
 
 
            // Get the root element  
            XmlElement root = document.DocumentElement;  
 
 
            // Create the new nodes  
            XmlElement newBook = document.CreateElement("book");  
            XmlElement newTitle = document.CreateElement("title");  
            XmlElement newAuthor = document.CreateElement("author");  
            XmlElement newCode = document.CreateElement("code");  
            XmlText title = document.CreateTextNode("Beginning Visual C# 3rd Edition");  
            XmlText author = document.CreateTextNode("Karli Watson et al");  
            XmlText code = document.CreateTextNode("1234567890");  
            XmlComment comment = document.CreateComment("This book is the book you are reading");  
 
 
            // Insert the elements  
            newBook.AppendChild(comment);  
            newBook.AppendChild(newTitle);  
            newBook.AppendChild(newAuthor);  
            newBook.AppendChild(newCode);  
            newTitle.AppendChild(title);  
            newAuthor.AppendChild(author);  
            newCode.AppendChild(code);  
            root.InsertAfter(newBook, root.LastChild);  
 
 
            document.Save("../../Books.xml");  
 
 
            listBoxXmlNodes.Items.Clear();  
            RecurseXmlDocument((XmlNode)document.DocumentElement, 0);  
        }  
//删除一个节点  
private void buttonDeleteNode_Click(object sender, EventArgs e)  
        {  
            // Load the XML document  
            XmlDocument document = new XmlDocument();  
            document.Load("../../Books.xml");  
 
 
            // Get the root element  
            XmlElement root = document.DocumentElement;  
 
 
            // Find the node. root is the < books> tag, so its last child which will be the  
            // last < book> node  
            if (root.HasChildNodes)  
            {  
                XmlNode book = root.LastChild;  
 
 
                // Delete the child  
                root.RemoveChild(book);  
 
 
                // Save the document back to disk  
                document.Save("../../Books.xml");  
                listBoxXmlNodes.Items.Clear();  
 
 
                RecurseXmlDocument((XmlNode)document.DocumentElement, 0);  
            }  
        }  
//在一个ListBox中显示文档的所有节点名称以及文本节点的内容  
private void RecurseXmlDocument(XmlNode root, int indent)  
    {  
      // Make sure we don't do anything if the root is null  
      if (root == null)  
        return;  
 
 
      if (root is XmlElement) // Root is an XmlElement type  
      {  
        // first, print the name  
        listBoxXmlNodes.Items.Add(root.Name.PadLeft(root.Name.Length + indent));  
 
 
        // Then check if there are any child nodes and if there are, call this  
        // method again to print them  
        if (root.HasChildNodes)  
          RecurseXmlDocument(root.FirstChild, indent + 2);  
 
 
        // Finally check to see if there are any siblings and if there are  
        // call this method again to have them printed  
        if (root.NextSibling != null)  
          RecurseXmlDocument(root.NextSibling, indent);  
      }  
      else if (root is XmlText)  
      {  
        // Print the text  
        string text = ((XmlText)root).Value;  
        listBoxXmlNodes.Items.Add(text.PadLeft(text.Length + indent));  
      }  
      else if (root is XmlComment)  
      {  
        // Print text  
        string text = root.Value;  
        listBoxXmlNodes.Items.Add(text.PadLeft(text.Length + indent));  
 
 
        // Then check if there are any child nodes and if there are, call this  
        // method again to print them  
        if (root.HasChildNodes)  
          RecurseXmlDocument(root.FirstChild, indent + 2);  
 
 
        // Finally check to see if there are any siblings and if there are  
        // call this method again to have them printed  
        if (root.NextSibling != null)  
          RecurseXmlDocument(root.NextSibling, indent);  
      }  
    }  
//XPath选择一个节点  
//XPath语法相关参考http://www.w3school.com.cn/xpath/xpath_syntax.asp  
private void buttonQueryNode_Click(object sender, EventArgs e)  
        {  
            // Load the XML document  
            XmlDocument document = new XmlDocument();  
            document.Load(@filePath);  
 
 
            // Get the root element  
            XmlElement root = document.DocumentElement;  
 
 
            string queryStr = textBoxQueryText.Text;  
 
 
            XmlNodeList nodeList = root.SelectNodes(queryStr);  
            listBoxXmlNodes.Items.Clear();  
 
 
            foreach (XmlNode n in nodeList)  
            {  
                RecurseXmlDocument(n, 0);  
            }  
        } 

110,566

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧