WPF 全球化(也许不是)

发布于 2024-11-10 17:23:05 字数 662 浏览 7 评论 0原文

我正在尝试构建一个带有标签、按钮等的原型。但是,我确实希望通过外部文件(可能是文本文件或 Excel 文件)自定义标签或按钮中的文本。我怎样才能实现这个目标?

目前,我正在尝试使用以下格式的 CSV 文件。

仪表板标题、仪表板 框 1 列名称,框 1 按钮 1,下一步

在我的代码中,我将它们读入文本字段,如下所示。

string[] lines = File.ReadAllLines("Dashboard.csv");
for (int i = 0; i < lines.Length; i++)
{
    string currentLine = lines[i];
    string[] lineValues = currentLine.Split(',');
    switch (i)
    {
        case 0: dashboardTitle.Content = lineValues[1];
        case 1: box1ColName.Content = lineValues[1];
        case 2: button1.Content = lineValues[1];
    }
}

我现在担心的是随着字段(元素)数量的增长。我可能必须将开关部分扩展到外壳 100?我如何在 CSV 文件中指定它们以相应地填写标签和按钮?

I am trying to build an prototype with labels, buttons, and so on. However, I do want the text in the labels or buttons to be customized through an external files, perhaps a text file or excel file. How can I achieve this?

Currently, I am trying on using a CSV file as in the following format.

Dashboard Title, Dashboard
Box 1 Column Name, Box 1
Button 1, Next

And in my codes, I read them into the text field as follows.

string[] lines = File.ReadAllLines("Dashboard.csv");
for (int i = 0; i < lines.Length; i++)
{
    string currentLine = lines[i];
    string[] lineValues = currentLine.Split(',');
    switch (i)
    {
        case 0: dashboardTitle.Content = lineValues[1];
        case 1: box1ColName.Content = lineValues[1];
        case 2: button1.Content = lineValues[1];
    }
}

My worry now is that as the number of fields (elements) grow. I would have to extends the switch part perhaps up to case 100? How can I specify them perhaps in the CSV file to fill in the lable, buttons accordingly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七婞 2024-11-17 17:23:06

斯科特·汉塞尔曼 (Scott Hanselman) 最近刚刚写了一篇博文,可能适合您。它以 ASP.NET 为例编写,但明确不限于此:

ASP.NET MVC 3、JavaScript 和 jQuery 中的全球化、国际化和本地化 - 第 1 部分

这是一篇关于该主题的九页长文章的开始。

Scott Hanselman has just recently written a blog post that might fit to you. It was written with ASP.NET as an example, but it is explicitely not limited to it:

Globalization, Internationalization and Localization in ASP.NET MVC 3, JavaScript and jQuery - Part 1

This starts a nine-page long article about the topic.

別甾虛僞 2024-11-17 17:23:05

尝试在文件中使用名称=值格式。

e.g. 

按钮1 =“我的名字是按钮1”
label2 = "This is a label"

将这些值加载到字典中

Dictionary<string, string> d = new Dictionary<string, string>();    
d.Add("button1", "My name is button1");    
d.Add("label2", "This is a label");    

然后您可以执行以下算法。循环遍历表单上的所有组件。如果组件名称与字典键匹配,则设置适当的值。例如:

  Control.ControlCollection coll = this.Controls;
  foreach(Control c in coll) 
  {
    if(c != null)
    {
       if (d.ContainsKey(c.Name))
       {
         c.Text = d[c.Name];
       }
    }
  }

查找控件的另一种方法是使用:
Control.ControlCollection.Find
方法。按名称搜索控件要容易得多。

但请注意,并非所有控件都有“.Text”属性,因此您必须检查控件的类型。
不过,我也强烈建议您学习如何使用资源文件。它是一种普遍接受的处理资源字符串的方式。

Try using a name = value format in your file.

e.g. 

button1 = "My name is button1"
label2 = "This is a label"

Load these values into a dictionary

Dictionary<string, string> d = new Dictionary<string, string>();    
d.Add("button1", "My name is button1");    
d.Add("label2", "This is a label");    

You can then do the following algorithm. Loop through all the components on your form. If the component name matches the dictionary key, set the appropriate value. e.g.:

  Control.ControlCollection coll = this.Controls;
  foreach(Control c in coll) 
  {
    if(c != null)
    {
       if (d.ContainsKey(c.Name))
       {
         c.Text = d[c.Name];
       }
    }
  }

Another way of finding controls is using the:
Control.ControlCollection.Find
method. Its much easier to search for controls by name.

Note however, that not all controls will have a ".Text" property, so you have to check for the type of control.
However, I would also strongly suggest you learn how to use resource files. Its a commonly accepted way of handling resource strings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文