XAML -> C# 从 C# 中的资源更新文本 属性
XAML -> C# Update a Text property from Resources in C#
我在 Xaml 中有一个 TextBlock 来显示标题。我以前设置了一个x:Uid="MyTitle"
,描述在一个资源文件中。但是现在,我的头衔可以改变。我尝试在我的 .cs 中绑定一个标题变量(但我们不能绑定 x:Uid)。
所以!我尝试直接在 C# 上更改我的标题......我失败了。
这是我的想法:
我的树
Root
Source
-code.xaml
-code.xaml.cs
Resources
En
-resources.resw
"Mytitle_1.Text", "This is my first title"
"Mytitle_2.Text", "This is the other one"
code.xaml
<TextBlock TextWrapping="Wrap" x:Name="Exemples" FontSize="20" Margin="20, 0, 20, 0" LineHeight="25" MaxLines="2" FontFamily="Segoe UI Light" Height="65"/>
code.xaml.cs
private string GetResources(string key)
{
ResourceLoader rl = ResourceLoader.GetForCurrentView("../Resources");
string resources = rl.GetString(key);
return resources;
}
private void ChangeTitle()
{
if (something)
Exemples.Text = GetResources("Mytitle_1");
else
Exemples.Text = GetResources("Mytitle_2");
}
我找到问题了。我只是使用我的密钥,如 "Mytitle_1" 或 "Mytitle_1.Text"。
我需要的是:
private string GetResources(string key)
{
ResourceLoader rl = ResourceLoader("../Resources");
string resources = rl.GetString(key);
return resources;
}
private void ChangeTitle()
{
if (something)
Exemples.Text = GetResources("Mytitle_1/Text");
else
Exemples.Text = GetResources("Mytitle_2/text");
}
瞧!
ResourceLoader loader = new ResourceLoader();
private void ChangeTitle()
{
if (something)
Exemples.Text = loader.GetString("MyTextInResources1");
else
Exemples.Text = loader.GetString("MyTextInResources2");
}
并且在您的资源文件中不要将 .Text 添加到您的字符串中,它仅添加用于 xaml 控件。
所以在你的资源文件中
只需添加一个新行作为
MyTextInResources1 your string to be displayed
我在 Xaml 中有一个 TextBlock 来显示标题。我以前设置了一个x:Uid="MyTitle"
,描述在一个资源文件中。但是现在,我的头衔可以改变。我尝试在我的 .cs 中绑定一个标题变量(但我们不能绑定 x:Uid)。
所以!我尝试直接在 C# 上更改我的标题......我失败了。 这是我的想法:
我的树
Root
Source
-code.xaml
-code.xaml.cs
Resources
En
-resources.resw
"Mytitle_1.Text", "This is my first title"
"Mytitle_2.Text", "This is the other one"
code.xaml
<TextBlock TextWrapping="Wrap" x:Name="Exemples" FontSize="20" Margin="20, 0, 20, 0" LineHeight="25" MaxLines="2" FontFamily="Segoe UI Light" Height="65"/>
code.xaml.cs
private string GetResources(string key)
{
ResourceLoader rl = ResourceLoader.GetForCurrentView("../Resources");
string resources = rl.GetString(key);
return resources;
}
private void ChangeTitle()
{
if (something)
Exemples.Text = GetResources("Mytitle_1");
else
Exemples.Text = GetResources("Mytitle_2");
}
我找到问题了。我只是使用我的密钥,如 "Mytitle_1" 或 "Mytitle_1.Text"。
我需要的是:
private string GetResources(string key)
{
ResourceLoader rl = ResourceLoader("../Resources");
string resources = rl.GetString(key);
return resources;
}
private void ChangeTitle()
{
if (something)
Exemples.Text = GetResources("Mytitle_1/Text");
else
Exemples.Text = GetResources("Mytitle_2/text");
}
瞧!
ResourceLoader loader = new ResourceLoader();
private void ChangeTitle()
{
if (something)
Exemples.Text = loader.GetString("MyTextInResources1");
else
Exemples.Text = loader.GetString("MyTextInResources2");
}
并且在您的资源文件中不要将 .Text 添加到您的字符串中,它仅添加用于 xaml 控件。
所以在你的资源文件中 只需添加一个新行作为
MyTextInResources1 your string to be displayed