adding drink to favorite page on click in windows store
Asked Answered
C

1

1

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.

The page where add to favourite buuton is provided

enter image description here

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>
Charlesettacharleston answered 4/12, 2015 at 17:49 Comment(2)
You can always store the data in a json or xml fileLabionasal
you mean file handling ..Charlesettacharleston
M
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;
    }
Mucus answered 5/12, 2015 at 21:47 Comment(2)
well I am now using xml file to save data as mentioned by@KENTUCKER but having a bit problem have mentioned above.Charlesettacharleston
@mohammadharis, using my JSON example, you'd just add a favourited Boolean value to your current drink model and call that save function.Mucus

© 2022 - 2024 — McMap. All rights reserved.