2015-03-31 57 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