2014-09-10 31 views
0
<?xml version="1.0" encoding="utf-8" standalone="no"?> 
<resources> 
    <string name="ABOUT">ÜBER</string> 
    <string name="ABOUT_MESSAGE">Willkommen</string> 
</resources> 

我在此解決方案中使用了帶有字符串的XML文件。然後在我的XAML項目中,我有:綁定到Windows Phone 8.1中的Xml元素

<TextBlock Text="" FontFamily="Segoe WP" FontWeight="Light" Foreground="Black" FontSize="16"/> 

如何將TextBlock的Text屬性綁定到name =「ABOUT」值。我必須在哪裏放置XML,以及需要在Xaml命名空間中添加哪種引用才能找到它?

另外,如何做到這一點從C#代碼隱藏?

回答

-1

您可以使用內置的XML序列化。

using System.Xml.Serialization; 

只能綁定到一個類的屬性

// You might have to change this based on your XML 
public class sample_data 
{ 
    public string About { get; set; }  
    public string Message { get; set; }   
} 

然後將二者結合起來

sample_data sd = new sample_data(); 

// Read the data. 
// You might have to change this based on your XML 
using (StreamReader streamReader = new StreamReader(file_stream)) 
{ 

    System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(sample_data)); 
    sample_data = (sample_data)serializer.Deserialize(streamReader); 
    streamReader.Close(); 
} 

然後設置您的XAML了

<StackPanel> 
    <TextBlock x:Name="tb1" Text="{Binding About"> 
    <TextBlock x:Name="tb2" Text="{Binding Message}"> 
</StackPanel> 

然後設置你的DataContext(我不知道你的Views如何是的設置,所以我想什麼)

this.tb1.DataContext = sd; 
this.tb2.DataContext = sd; 

在我看來,你需要 「入門Page」

Getting started with developing for Windows Phone 8

Channel 9's Windows Phone Page

相關問題