WinRT StorageFile 读写操作xml

using System;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Windows.Data.Xml.Dom;
using Windows.Storage;

namespace Microsoft.Assistant.MobileAssistantRT.Utilities
{
    public class LocalFolderHelper
    {
        private const string STORE_CONFIG_NAME = "Config.xml";
        private const string STORE_CONFIG_FOLDERNAME = "Data";
        private static StorageFolder localFolder = ApplicationData.Current.LocalFolder;

        /// <summary>
        /// Create the storeConfig.xml if it need.
        /// </summary>
        /// <typeparam name="T">the Generics type</typeparam>
        /// <param name="t">the type to save</param>
        public async static Task CreateStoreConifgXML<T>(T t)
        {
            StorageFolder storageFolder = await localFolder.CreateFolderAsync(STORE_CONFIG_FOLDERNAME, CreationCollisionOption.OpenIfExists);
            StorageFile storageFile = await storageFolder.CreateFileAsync(STORE_CONFIG_NAME, CreationCollisionOption.ReplaceExisting);
            var properties = t.GetType().GetTypeInfo().DeclaredProperties;
            XmlDocument dom = new XmlDocument();
            XmlElement xmlRootNode;

            xmlRootNode = dom.CreateElement("Configuration");
            dom.AppendChild(xmlRootNode);

            XmlElement x1 = dom.CreateElement(t.GetType().Name);

            foreach (var property in properties)
            {
                XmlElement xmlElement = dom.CreateElement(property.Name);

                if (property.GetValue(t) != null)
                {
                    xmlElement.InnerText = property.GetValue(t).ToString();
                }
                x1.AppendChild(xmlElement);
            }
            xmlRootNode.AppendChild(x1);

            await dom.SaveToFileAsync(storageFile);
        }

        /// <summary>
        /// Check whether the storeConfig.xml exists. 
        /// path: C:\Users\UserName\AppData\Local\Packages\packageName\LocalState\Data\Config.xml
        /// </summary>
        /// <returns>return null if not exists.</returns>
        public async static Task<StorageFile> CheckStoreConfigExist()
        {
            try
            {
                StorageFolder folderFound = await localFolder.GetFolderAsync(STORE_CONFIG_FOLDERNAME);
                StorageFile fileFound = await folderFound.GetFileAsync(STORE_CONFIG_NAME);

                return fileFound;
            }
            catch (Exception e)
            {
                return null;
            }
        }

        /// <summary>
        /// Read config from xml
        /// </summary>
        /// <typeparam name="T">The type to return</typeparam>
        /// <param name="storageFile">the data source</param>
        /// <param name="t">the object</param>
        /// <returns></returns>
        public async static Task<T> GetDataFromConifgXML<T>(StorageFile storageFile, T t)
        {
            if (storageFile == null)
            {
                return default(T);
            }

            var stream = await storageFile.OpenAsync(FileAccessMode.Read);
            XmlDocument xmlDoc = await XmlDocument.LoadFromFileAsync(storageFile);
            XmlNodeList nodes = xmlDoc.SelectNodes(@"/Configuration/" + t.GetType().Name);
            IXmlNode node = nodes.FirstOrDefault();
            if (node == null)
            {
                return default(T);
            }
            var properties = t.GetType().GetTypeInfo().DeclaredProperties;

            if (node != null && node.HasChildNodes())
            {
                var list = node.ChildNodes;
                foreach (var item in list)
                {
                    PropertyInfo prop = properties.Where((i) => i.Name.Equals(item.LocalName)).FirstOrDefault();
                    if (prop != null)
                    {
                        int newInt;
                        Guid newGuid = Guid.Empty;
                        if (prop.PropertyType.Name.Equals("Int32") && Int32.TryParse(item.InnerText, out newInt))
                        {
                            prop.SetValue(t, newInt);
                        }
                        // some property maybe nullable.
                        else if (prop.PropertyType.FullName.Contains("Guid") && Guid.TryParse(item.InnerText, out newGuid))
                        {
                            prop.SetValue(t, newGuid);
                        }
                        else
                        {
                            prop.SetValue(t, item.InnerText);
                        }
                    }
                }

            }

            return (T)t;
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 Windows 10 Toast 通知中添加一个监听按钮被点击的事件,可以使用 Python winrt 库和 XML 构建器创建一个包含自定义按钮的通知。下面是一个简单的示例代码: ```python import winrt.windows.ui.notifications as notifications import winrt.windows.data.xml.dom as dom # 创建 ToastNotificationManager 对象 toaster = notifications.ToastNotificationManager # 创建 ToastNotification 对象 toast = toaster.get_template_content(notifications.ToastTemplateType.ToastText02) # 获取 Toast 的 XML 节点 xml = dom.XmlDocument() xml.load_xml(toast.get_xml().get_xml()) # 添加一个按钮 actions = xml.getElementsByTagName("actions")[0] action = xml.createElement("action") action.setAttribute("content", "点击这里") action.setAttribute("arguments", "buttonClicked") action.setAttribute("activationType", "background") actions.appendChild(action) # 创建 ToastNotification 对象并显示 toast.add_node(xml) notifier = notifications.ToastNotificationManager.create_toast_notifier() notifier.show(toast) ``` 在上面的代码中,我们使用 `ToastNotificationManager` 对象创建了一个基本的 Toast 通知模板。然后,我们使用 `XmlDocument` 对象加载了该模板的 XML,以便能够修改它。我们通过 `getElementsByTagName` 方法获取到了 `actions` 节点,然后创建了一个新的 `action` 节点来表示监听按钮被点击的事件。我们设置了按钮的 `content`、`arguments` 和 `activationType` 属性。最后,我们将修改后的 XML 内容添加回 `ToastNotification` 对象中,并使用 `ToastNotificationManager` 创建了一个 `ToastNotifier` 对象并显示了通知。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值