在Windows Store应用绑定资源文件中值的字符串使用C#和XAML [英] Bind resource file value strings in Windows Store App using C# and XAML

查看:164
本文介绍了在Windows Store应用绑定资源文件中值的字符串使用C#和XAML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的资源文件:Strings\en-US\Resources.resw

I have the resource file: Strings\en-US\Resources.resw

我试图绑定到从该文件中像这样的字符串:

I'm trying to bind to a string from this file like so:

<TextBlock Text="{Binding Path=LocalizedResources.app_name, Source={StaticResource LocalizedStrings}}" />



我想这种做法是错误的,因为我得到资源的TestString无法得到解决。 。

I guess this approach is wrong because I get "The Resource 'TestString' could not be resolved.".

如何绑定从资源文件中的字符串?

How do I bind to a string from a resource file?

PS:我的TestString在资源文件

PS: I have TestString in the resource file.

PPS:我需要使用Studio 2012,而不是Windows 8.1的应用程序创建的Windows 8商店应用

PPS: I need to create Windows 8 store app with Studio 2012, not Windows 8.1 app.

推荐答案

有一个工具 - <。 code> PublicResXFileCodeGenerator ,你可以用它来访问这样的事情的资源文件中的值:

There's a tool - PublicResXFileCodeGenerator which you can use to access the resource file values in such a matter :

MyResourceFile.ResourceString;

您可以在值存储到一个属性,随后将它绑定在用户界面上。

You can store that value into a property and bind it afterwards on the UI.

编辑绑定方法

您需要将其设置为<$一个ViewModel 。C $ C>的DataContext 上查看

You'll need a ViewModel which will be set as your DataContext on that View.

让我们说你有三排值资源文件:名字,姓氏年龄。所以,你会访问它们,如:

Let's say you have three rows with values in your resource file : FirstName, LastName and Age. So you will access them like :

string firstName = MyResourceFile.FirstName;
string lastName = MyResourceFile.LastName;
int age = Convert.ToInt32(MyResourceFile.Age); //made it an int

您必须创建一个类,它会站在作为你的视图模型。

You will have to create a class which will stand as your ViewModel.

public class MyViewModelClass : INotifyPropertyChanged 
{
    private string firstName;
    public string FirstName
    {
        get { return firstName; }
        set { firstName = value; OnPropertyChanged(); }
    }

    private string lastName;
    public string LastName
    {
        get { return lastName; }
        set { lastName = value; OnPropertyChanged(); }
    }

    private string age;
    public string Age
    {
        get { return age; }
        set { age= value; OnPropertyChanged(); }
    }

    //Also the INotifyProperty members ...
}

然后在相应的视图代码隐藏cs文件:

Then in the corresponding View code-behind .cs file :

1)定义和声明的实例 MyViewModelClass

1) Define and declare an instance of the MyViewModelClass.

public MyViewModelClass viewModel = new MyViewModelClass();



2)设置的DataContext 来实例。先前设定

this.DataContext = this.viewModel;



3)从资源文件中添加自己的价值观:

3) Add your values from the Resource file :

this.viewModel.FirstName = MyResourceFile.FirstName;
this.viewModel.LastName = MyResourceFile.LastName;
this.viewModel.Age = MyResourceFile.Age;



4)将它们绑定在你的XAML:

4) Bind them in your XAML :

<TextBlock Text="{Binding FirstName}" /> 
<TextBlock Text="{Binding LastName}" />
<TextBlock Text="{Binding Age}" />
//The names of the public properties in the View Model class



的公共属性的名称我很困惑这一切!怎么所有这些东西是什么意思?

如果您使用 INotifyPropertyChanged的,你将能够以更新UI automaticaly当你绑定的值的变化。这是因为在二传手每个属性的,还有的 OnPropertyChange(); 通话将通知您的UI值的变化。 OnPropertyChange()调用应该始终之后的价值观的变化,否则,所做的更改不会在UI上体现出来。

If you use INotifyPropertyChanged you will be able to update your UI automaticaly when your bound values change. This is because in the setter of each of the properties, there's the OnPropertyChange(); call which will notify your UI of the value changes. OnPropertyChange() call should always be after the values change otherwise, the changes will not be reflected on the UI.

每个页面有一个的DataContext (不仅是网页,许多元素一样)。从中,你可以使用 {结合东西} 在XAML绑定你的价值观。我们正在创建视图模型类的一个实例,该实例会在当前页面的DataConext。结合系统将寻找相应的属性名称。如果发现它们的值被显示在用户界面。如果没有,没有出现错误,但如果你期望的值,什么都不会显示出来。

Each page has a DataContext (not only pages, many elements do). From it, you can bind your values using the {Binding something} in your XAML. We're creating an instance of the View Model class, and that instance will be the DataConext for the current page. The binding system will be looking for the corresponding properties names. If they're found the values are shown on the UI. If not, no errors occur, but where you expect a value, nothing will show up.

您可以找到文档这么多的资源有关这种模式,只需搜索<我> MVVM(模型 - 视图 - 视图模型)

You can find so many resources of documentation about this pattern, just search for MVVM (Model-View-ViewModel).

这篇关于在Windows Store应用绑定资源文件中值的字符串使用C#和XAML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆