2015-03-31 65 views
0

我在Xaml中有一个TextBlock来显示标题。我曾经设置x:Uid="MyTitle",在资源文件中描述。但现在,我的头衔可以改变。我尝试在我的.cs文件中绑定一个title变量(并且我们不能在x:Uid上绑定)。XAML - > C#从C#中的资源更新文本属性

所以!我试图直接在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"); 
} 

回答

0

我发现这个问题。我只是使用我的密钥,如“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"); 
} 

瞧!

0
ResourceLoader loader = new ResourceLoader(); 

private void ChangeTitle() 
{ 
    if (something) 
     Exemples.Text = loader.GetString("MyTextInResources1"); 
    else 
     Exemples.Text = loader.GetString("MyTextInResources2"); 
} 

并且在您的资源文件中donot将.Text添加到您的字符串中,它仅添加用于xaml控件。

所以在你的资源文件 只需添加一个新行作为

MyTextInResources1  your string to be displayed 
+0

是的!在我的解决方案中,我忘记了更改ResourceLoader loader = new ResourceLoader();我编辑你的,你错过了一个观点。 – 2015-04-01 12:59:11

+0

这适用于Windows商店应用程序。 8 – 2015-04-02 03:21:50