The steps are:
1. Adding the following functions to the Page.xaml.cs
private string m_filePath = "TestHistoryStocks.txt";
#region "Local Cache storage"
/// <summary>
/// Append the current cacheString to the local storage file, will remove all the duplicates to the original content
/// </summary>
/// <param name="cacheString"></param>
private void StoreToLocalCache(string cacheString)
{
string existingSymbols = ReadFromLocalCache();
string[] existingSymbolsSplit = existingSymbols.Split(m_separators, System.StringSplitOptions.RemoveEmptyEntries);
string[] cacheStringSplit = cacheString.Split(m_separators, System.StringSplitOptions.RemoveEmptyEntries);
foreach (string cacheSymbol in cacheStringSplit)
if (!existingSymbolsSplit.Contains(cacheSymbol))
if (string.IsNullOrEmpty(existingSymbols))
existingSymbols += cacheSymbol;
}
else
existingSymbols += "," + cacheSymbol;
try
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
// Use a StringBuilder to construct output.
StringBuilder sb = new StringBuilder();
// Create three directories in the root.
store.CreateDirectory("TestStockQuotes");
// Create a file in the root.
IsolatedStorageFileStream rootFile = store.CreateFile(m_filePath);
rootFile.Close();
if (store.FileExists(m_filePath))
using (StreamWriter sw =
new StreamWriter(store.OpenFile(m_filePath,
FileMode.Open, FileAccess.Write)))
sw.WriteLine(existingSymbols);
catch (IsolatedStorageException ex)
sb.AppendLine(ex.Message);
// remove the store
//store.Remove();
catch (IsolatedStorageException)
// TODO: Handle that store was unable to be accessed.
/// Read the text content from the localCache, will remove the end of line characters
/// <returns></returns>
private string ReadFromLocalCache()
string contents = "";
// Read the contents of the file: MyApp1\SubDir1\MyApp1A.txt
using (StreamReader reader =
new StreamReader(store.OpenFile(m_filePath,
FileMode.Open, FileAccess.Read)))
contents = reader.ReadToEnd();
return contents.Trim().TrimEnd(new char[] { '\r', '\n' });
return "";
/// Delete the local cache file
private void DeleteLocalCache()
// Delete a file.
store.DeleteFile(m_filePath);
store.Remove();
#endregion
2. Add functions and insert codes to call these functions:
In page.xaml:
<StackPanel Orientation="Horizontal" >
<Button Name="ButtonGetQuote" Background="Brown" Content="GetQuote" Click="ButtonGetQuote_Click" ></Button>
<Button Name="ButtonClearCache" Margin="10,0,0,0" Background="Brown" Content="Clear Stored Cache" Click="ButtonClearCache_Click"></Button>
</StackPanel>
In page.xaml.cs:
public Page()
InitializeComponent();
TextBoxQuoteInput.Text = ReadFromLocalCache();
if (!string.IsNullOrEmpty(TextBoxQuoteInput.Text))
RunQuery();
StartRefresh();
TextBoxQuoteInput.Focus();
private void ButtonClearCache_Click(object sender, RoutedEventArgs e)
DeleteLocalCache();
private void BindItemSource()
if (m_count >= m_maxCount)
int x = dataGridStock.SelectedIndex;
dataGridStock.ItemsSource = null;
dataGridStock.ItemsSource = m_AllStocks.Values;
dataGridStock.SelectedIndex = x;
//remove the extra columns in the table
while (dataGridStock.Columns.Count > 7)
dataGridStock.Columns.RemoveAt(7);
StoreToLocalCache(TextBoxQuoteInput.Text);
3. Ctrl-F5 a few times and quotes some symbols each time. We should see the previous quoted symbols get cached each time we load the program.
This is what the final version should looks like:
4. As ASP.NET MVP Ken pointed out after I first published the blog, we need to add IsReadOnly="True" for data:DataGrid to make not throwing exception when editing the grid. <data:DataGrid x:Name="dataGridStock" Margin="0,5,0,10" RowHeight="44" Height="500" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" AlternatingRowBackground="LemonChiffon" IsReadOnly="True" >. I'll update the attached file as well.