【问题标题】:adding drink to favorite page on click in windows store在 Windows 商店中单击时将饮料添加到最喜欢的页面
【发布时间】:2015-12-06 22:11:44
【问题描述】:

我正在为 windows 商店制作饮料应用程序。

用户可以根据需要选择喜欢的饮料。 所以他最喜欢的饮料应该显示在最喜欢的页面中。

那么如何在点击按钮时将这些饮料添加到收藏页面,如 图片 1

所示

是否可以不使用数据库..?

任何想法都将不胜感激。

我正在使用 xml 文件来保存按钮单击时的数据

我已经设法在我的最喜欢的页面上的网格中从 xml 文件中获取数据 但它是由我静态完成的,因为我自己编写了 xml 文件。 我希望它是这样写的:

<drink>
    <drinkImage>ck.png</drinkImage>
    <drinkTitle>COKE</drinkTitle>
    <drinkDescription>(1793-1844)</drinkDescription>
  </drink>

我当前的文件是这样的:

 <?xml version="1.0" encoding="utf-8" ?>
    <drinks>
      <drink>
        <drinkImage>pepsi.png</drinkImage>
        <drinkTitle>PEPSI</drinkTitle>
        <drinkDescription>(1793-1844)</drinkDescription>
      </drink>
**<here I Want above xml on add to my favourite button click>**
    </drinks>

【问题讨论】:

  • 您始终可以将数据存储在 json 或 xml 文件中
  • 你的意思是文件处理..

标签: windows-store-apps windows-store


【解决方案1】:

您正在寻找的解决方案实际上取决于您希望从添加到收藏夹页面中得到什么。

如果您只想在应用程序运行期间将其添加到收藏夹页面,请拥有一个包含收藏夹集合的 ViewModel,您可以通过将其存储在 IOC 容器中从任何页面访问这些收藏夹(可能使用 MVVMLight )。

如果您想保存它,您可以将收藏夹写入一个 JSON 文件,该文件可以存储在应用程序的本地存储中。您还需要在下次加载时将其重新加载到您的应用程序中。

您可以按如下方式执行 JSON 保存逻辑

    /// <summary>
    /// Save an object of a given type as JSON to a file in the storage folder with the specified name.
    /// </summary>
    /// <typeparam name="T">The type of object</typeparam>
    /// <param name="folder">Folder to store the file in</param>
    /// <param name="data">The object to save to the file</param>
    /// <param name="encoding">The encoding to save as</param>
    /// <param name="fileName">The name given to the saved file</param>
    /// <returns>Returns the created file.</returns>
    public async Task<StorageFile> SaveAsJsonToStorageFolder<T>(StorageFolder folder, T data, Encoding encoding, string fileName)
    {
        if (folder == null)
            throw new ArgumentNullException("folder");
        if (data == null)
            throw new ArgumentNullException("data");
        if (fileName == null)
            throw new ArgumentNullException("fileName");

        string json = JsonConvert.SerializeObject(data, new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.All });
        byte[] bytes = encoding.GetBytes(json);

        return await this.SaveBytesToStorageFolder(folder, bytes, fileName);
    }

    /// <summary>
    /// Saves a byte array to a file in the storage folder with the specified name.
    /// </summary>
    /// <param name="folder">Folder to store the file in</param>
    /// <param name="bytes">Bytes to save to file</param>
    /// <param name="fileName">Name to assign to the file</param>
    /// <returns>Returns the created file.</returns>
    public async Task<StorageFile> SaveBytesToStorageFolder(StorageFolder folder, byte[] bytes, string fileName)
    {
        if (folder == null)
            throw new ArgumentNullException("folder");

        if (bytes == null)
            throw new ArgumentNullException("bytes");

        if (string.IsNullOrWhiteSpace(fileName))
            throw new ArgumentNullException("fileName");

        StorageFile file = await folder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
        await FileIO.WriteBytesAsync(file, bytes);

        return file;
    }

【讨论】:

  • 好吧,我现在正在使用 xml 文件来保存@KENTUCKER 提到的数据,但上面提到了一点问题。
  • @mohammadharis,使用我的 JSON 示例,您只需在当前饮料模型中添加一个喜欢的布尔值并调用该保存函数。
猜你喜欢
  • 1970-01-01
  • 2022-01-15
  • 2023-03-25
  • 1970-01-01
  • 2011-12-19
  • 1970-01-01
  • 1970-01-01
  • 2011-08-06
  • 1970-01-01
相关资源
最近更新 更多