2014-09-10 57 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

相关问题