简体   繁体   中英

Binding to Xml elements in Windows Phone 8.1

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<resources>
    <string name="ABOUT">ÜBER</string>
    <string name="ABOUT_MESSAGE">Willkommen</string>
</resources>

I have this XML file with strings in my solution. Then in my project in the XAML I have:

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

How to bind the Text property of TextBlock to name="ABOUT" value. Where do I have to put the XML, and what kind of reference I need to add in the Xaml namespaces to find it?

Also, how to do this from the C# code-behind?

There is a built in XML Serialization you can use.

using System.Xml.Serialization;

You can only Bind to a class Property

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

Then combine the two

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();
}

Then set your XAML up

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

Then set your DataContext (I don't know how your Views are setup, so I assume nothing)

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

Seems to me you need to "Getting Started Page"

Getting started with developing for Windows Phone 8

Channel 9's Windows Phone Page

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM