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.
EDIT for Binding approach
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 ...
}
Then in the corresponding View code-behind .cs file :
1) Define and declare an instance of the MyViewModelClass
.
public MyViewModelClass viewModel = new MyViewModelClass();
2) Set the DataContext
to the instance previously set.
this.DataContext = this.viewModel;
3) Add your values from the Resource file :
this.viewModel.FirstName = MyResourceFile.FirstName;
this.viewModel.LastName = MyResourceFile.LastName;
this.viewModel.Age = MyResourceFile.Age;
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
I'm confused about all this! What do all these things mean?
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.
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.
You can find so many resources of documentation about this pattern, just search for MVVM (Model-View-ViewModel).