[UWP]将XML文件属性转换为VariableSizedWrapGrid中的XAML按钮XAML& C# [英] [UWP]Converting XML File Attribute to XAML Button's in a VariableSizedWrapGrid XAML & C#

查看:130
本文介绍了[UWP]将XML文件属性转换为VariableSizedWrapGrid中的XAML按钮XAML& C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我在相当长的几年里没有做太多的xaml,所以我在回到事物中时遇到了一些麻烦。


无论如何,我想要实现的是......我有一个XML文件,我把它放在Assets / Files文件夹中,称为"albums.xml"


看起来像这样......

 <相册> 
< albumName>

< title> 相册名称< / title>
<年>> 2018 < / year>
< source> Assets / Images / imagepath.jpg < / source>
< tracks>
< track> 歌曲名称< / track>
< track> 歌曲名称< / track>
< track> 歌曲名称< / track> ;
< track> 歌曲名称< / track>
< track> 歌曲姓名< / track>
< track> 歌曲名称< / track>
< ;曲目> 歌曲名称< / track>
< / tracks>
< / albumName>
<相册>

在我的xaml页面中,我有一个网格,其中包含一个我想要的VariableSizedWrapGrid包含所有"专辑"在xml文件中。我真的不确定如何将xml文件绑定到Wrap Grid中,这样如果我从文件中添加或删除
,它就会更新。


真的希望你可以帮助我,已经坚持了几个晚上。 


干杯伙伴。 

解决方案






首先是您需要将xml文件序列化为C#类。您可以使用

XmlSerializer类
来做到这一点。创建目标类并使用XmlSerializer.Deserialize()函数转换xml文件。



喜欢这个:





 [Serializable()] 
公共类albumName
{
[System.Xml.Serialization.XmlElement(" title")]
public string title {get;组; }

[System.Xml.Serialization.XmlElement(" year")]
public string year {get;组; }

[System.Xml.Serialization.XmlElement(" source")]
public string source {get;组; }


[XmlArray(" tracks")]
[XmlArrayItem(" track",typeof(String))]
public String [] track {得到;组; }
}

[Serializable()]
[System.Xml.Serialization.XmlRoot(" Albums")]
public class Albums
{
[System.Xml.Serialization.XmlElement(" albumName")]
public albumName albumName {get;组; }
}







这个:   &NBSP; &NBSP;   

 public Albums albums {get;组; 


StorageFolder appInstalledFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
StorageFolder assets = await appInstalledFolder.GetFolderAsync(" Assets");
var file = await assets.GetFileAsync(" albums.xml");



XmlSerializer serializer = new XmlSerializer(typeof(Albums));

StreamReader reader = new StreamReader(file.Path);

albums =(专辑)serializer.Deserialize(reader);
reader.Close();











如果你想将数据绑定到xaml,你将需要设置datacontext



喜欢这个:

 this.DataContext = albums; 





和xaml





< TextBlock x:Name =" Source" Text =" {Binding albumName.source}" /> ; 
< ListView x:Name =" TrackList" ItemsSource =" {Binding albumName.track}">
< ListView.ItemTemplate>
< DataTemplate>
< TextBlock Text =" {Binding}" />
< / DataTemplate>
< /ListView.ItemTemplate>
< / ListView>







如果你需要进行双向绑定(当你在UI页面中更改数据时,源会更新),你需要实现INotifyPropertyChanged接口。你可以
参考
数据绑定深度





一旦你想删除或添加一些项目的xml文件,你可以使用System.Xml.Linq。请参考这些链接:



将元素,属性和节点
添加到XML树(C#)



从XML树中删除元素,属性,
和节点(C#)





最好的问候,



罗伊


Hi,

I haven't done much xaml in a fair few years, so I am having a bit of trouble getting back into the swing of things.

Anyway, what I am trying to achieve is... I have a XML file that I have put in Assets/Files folder and is called "albums.xml"

Looks like this...

<Albums>
   <albumName>
      <title>Album Name</title>
      <year>2018</year>
      <source>Assets/Images/imagepath.jpg</source>
      <tracks>
        <track>Song Name</track>
        <track>Song Name</track>
        <track>Song Name</track>
        <track>Song Name</track>
        <track>Song Name</track>
        <track>Song Name</track>
        <track>Song Name</track>
      </tracks>
   </albumName>
<Albums>

In my xaml page I have a grid that contains a VariableSizedWrapGrid that I would like to contain all the "albums" that are in the xml file. I'm really not sure how I can bind the xml file into the Wrap Grid so that if I add or remove something from the file it will update.

Really hope you can help me, been stuck on it for a couple of night's now. 

Cheers guys. 

解决方案

Hi,

The first thing is that you need to serialize the xml file to a C# class. You could use XmlSerializer Class to do that. Create a target class and use XmlSerializer.Deserialize() function to convert the xml file.

Like this:

    [Serializable()]
    public class albumName
    {
        [System.Xml.Serialization.XmlElement("title")]
        public string title { get; set; }

        [System.Xml.Serialization.XmlElement("year")]
        public string year { get; set; }

        [System.Xml.Serialization.XmlElement("source")]
        public string source { get; set; }


        [XmlArray("tracks")]
        [XmlArrayItem("track", typeof(String))]
        public String[] track { get; set; }
    }

    [Serializable()]
    [System.Xml.Serialization.XmlRoot("Albums")]
    public class Albums
    {
        [System.Xml.Serialization.XmlElement("albumName")]
        public albumName albumName { get; set; }
    }

And this:        

            public Albums albums { get; set; }


            StorageFolder appInstalledFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
            StorageFolder assets = await appInstalledFolder.GetFolderAsync("Assets");
            var file = await assets.GetFileAsync("albums.xml");



            XmlSerializer serializer = new XmlSerializer(typeof(Albums));

            StreamReader reader = new StreamReader(file.Path);

            albums = (Albums)serializer.Deserialize(reader);
            reader.Close();


If you want to bind the data to xaml, you will need to set the datacontext

Like this:

this.DataContext = albums;

and xaml

<TextBlock x:Name="Source" Text="{Binding albumName.source}"/>
            <ListView x:Name="TrackList" ItemsSource="{Binding albumName.track}">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding}"/>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>


If you need to made a two way binding (when you change the data in UI page, the source will update), you will need to implement the INotifyPropertyChanged interface. You could refer Data binding in depth

Once you want to delete or add some item the xml file, you could use System.Xml.Linq. Please refer these links:

Adding Elements, Attributes, and Nodes to an XML Tree (C#)

Removing Elements, Attributes, and Nodes from an XML Tree (C#)

Best regards,

Roy


这篇关于[UWP]将XML文件属性转换为VariableSizedWrapGrid中的XAML按钮XAML&amp; C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆