WP8: Communication between Background Agent and App wire Storage File?

899 views Asked by At

I am trying to write a simple weather app as my first app. I have to keep the numbers of http requests as low as possible and so I am using a isolatedStorageSetting within the app to save the requested data and the date and time of the last request. Before the app start a request it looks in this file to ask when was the last request and start a new request if 120 minutes are gone. All this works perfectly within the app but now I have to implement an Scheduled Task to update the live tile and lock screen in the background. But before the background agent request the data it has to look in this file to ask for the last update and rewrite the data after request. So what I need is a file that can be used for reading and writing by the app and the background agent. I now that I need a mutex and wo on... but my questions are

  1. what is the right kind of file or database for this case? (isolatedStorgeSettings, isolatedStorgeFile or something else)

  2. where I have to generate this file? (within the MainPage.xaml.cs or do I need a Class Lib. Project)

  3. how is the syntax to read and write entries in this file from the app and the background agent?

Ok I have this example now just as test to understand the hole topic step by step...

  1. I have a Class Library "DataLib" wich contains this:

    namespace DataLib { public class DataLib {

    public static string DatenHolen(string DatenPacket)
    {
        IsolatedStorageFile WetterDatenDatei = IsolatedStorageFile.GetUserStoreForApplication();
    
        try
        {
            //Create == Create OR Overwrite
            IsolatedStorageFileStream myStream = new IsolatedStorageFileStream("datei1.txt", System.IO.FileMode.Create, WetterDatenDatei);
            StreamWriter writer = new StreamWriter(myStream);
            writer.Write("Inhalt der Datei");
            writer.Close();
        }
        catch (Exception)
        {
            MessageBox.Show("Fehler beim Schreiben der Datei");
        }
    
        try
        {
            IsolatedStorageFileStream myStream = new IsolatedStorageFileStream("datei1.txt", System.IO.FileMode.Open, WetterDatenDatei);
            StreamReader reader = new StreamReader(myStream);
            DatenPacket = reader.ReadToEnd();
            reader.Close();
        }
    
        catch (Exception)
        {
            MessageBox.Show("Fehler beim Auslesen der Datei");
        }
    
        return DatenPacket;
    }
    

    } }

  2. I have the app itself with MainPage.xaml.cs wich have a reference to DataLib and contains this:

    using DataLib;

...

txt_Test.Text = DataLib.DataLib.DatenHolen();

This line produce an error. I just wont to display the generated string within the textbox "txt_Test". Where is my misstake?

2

There are 2 answers

6
crea7or On BEST ANSWER
  1. Use isolatedStorage, but not the isolatedStorageSettings. Protect it with Mutex and there will be no problems.
  2. Generate it where you need it.
  3. Easiest way is XmlSerializer.

2: settings access:

public new static App Current
{
    get
    {
        return (App)Application.Current;
    }
}

static public MySettings mySettings = new MySettings();

Now you can access settting where you want by:

App.Current.mySettings.Save() // Load() ... etc.
2
Shawn Kendrot On

You can save settings in your app and have them accessible by your background agent by saving a file to IsolatedStorage. I use JSON.Net to save and read the file. I always save my settings in the .\Shared\ of my storage. This way I know I can always access it. I have created a class for storing and reading this information called FileStorage. Using this class you can save your settings quite easily. I will create a Save, and Load method in my settings so they can be read and updated with the current information.

public class AppSettings
{
    public bool SomeProp { get; set; }
    public double AnotherProp { get; set;}

    public void Save()
    {
        FileStorage.WriteSharedData("Settings.txt", this);
    }

    public static AppSettings Load()
    {
        return FileStorage.ReadSharedData<AppSettings>("Settings.txt");
    }
}

This class helps ensure that I can access the file and my settings without any issue.

I have had success when using IsolatedStorageSettings for my app and for my background agent. I only use IsolatedStorageSettings if memory is a concern as my other solution uses JSON.Net which consumes quite a bit of memory.