adding drink to favorite page on click in windows store

冷暖自知 提交于 2019-12-13 19:23:43

问题


I am making an app of drinks for windows store.

According to requirement user can select drink as favorite. So his favorite drinks should be shown in favorite page.

So how can I add these drinks to favorite page on button click as shown in image 1

Is it possible without using database..?

Any share of idea would be appreciated.

I am using xml file to save data on button click

I have managed to get the data from xml file in a grid on my favourite page but it is statically done by me as I had wrote xml file by myself. I want it to be wrote like that:

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

my current file is this:

 <?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>

回答1:


The solution you're looking for really depends on what it is that you're wanting to get out of the adding to favourites page.

If you just want to add it to the favourites page for the duration of the app, have a ViewModel which contains the collection of favourites that you can access from any page by storing it in an IOC container (possibly using MVVMLight).

If you're wanting to then save it, you can write the favourites out to a JSON file which you can store in the local storage for the application. You'll also want to load it back into your app next time it loads.

You can do your JSON save logic as below

    /// <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;
    }


来源:https://stackoverflow.com/questions/34094405/adding-drink-to-favorite-page-on-click-in-windows-store

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!