C# 绑定到Windows Phone 8.1中的Xml元素
我的解决方案中有一个带字符串的XML文件。在我的XAML项目中,我有:C# 绑定到Windows Phone 8.1中的Xml元素,c#,xml,xaml,binding,windows-phone-8.1,C#,Xml,Xaml,Binding,Windows Phone 8.1,我的解决方案中有一个带字符串的XML文件。在我的XAML项目中,我有: <?xml version="1.0" encoding="utf-8" standalone="no"?> <resources> <string name="ABOUT">ÜBER</string> <string name="ABOUT_MESSAGE">Willkommen</string> </resources>
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<resources>
<string name="ABOUT">ÜBER</string>
<string name="ABOUT_MESSAGE">Willkommen</string>
</resources>
如何将TextBlock的Text属性绑定到name=ABOUT值。我必须把XML放在哪里,需要在Xaml名称空间中添加什么样的引用才能找到它
另外,如何从隐藏的C代码中执行此操作?您可以使用内置的XML序列化
<TextBlock Text="" FontFamily="Segoe WP" FontWeight="Light" Foreground="Black" FontSize="16"/>
只能绑定到类属性
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; }
}
然后设置XAML
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();
}
在我看来,你需要进入入门页面
<StackPanel>
<TextBlock x:Name="tb1" Text="{Binding About">
<TextBlock x:Name="tb2" Text="{Binding Message}">
</StackPanel>
this.tb1.DataContext = sd;
this.tb2.DataContext = sd;