Reputation: 6227
I created a method to open a saved file based on the name of the file passed from another class, but I'm not sure if I'm calling or passing the file correctly between classes.
In this class the file names are stored in a list, when a name is selected the file name is passed to the other class and my OpenSavedFile
method is called.
These are the two methods below,can someone point me in the right direction with this or tell me where I'm going wrong with it?
Method that passes file name to LocationDetails class:
//selects saved note name based on slected index in list
private void NotesListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count > 0)
NavigationService.Navigate(new Uri(string.Format("/LocationDetails.xaml?note={0}", e.AddedItems[0]), UriKind.Relative));
LocationDetails myFile = new LocationDetails();
myFile.OpenSavedFile();
}
My method that is supposed to open the file based on the name passed from the other previous class,but when the first statement is executed it gives this error System.AccessViolationException
:
public void OpenSavedFile()
{
//breaks when stepping into below statement `System.AccessViolationException`
string filename = this.NavigationContext.QueryString["note"];
if (!string.IsNullOrEmpty(filename))
{
using (var store = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication())
using (var stream = new IsolatedStorageFileStream(filename, FileMode.Open, FileAccess.ReadWrite, store))
{
StreamReader reader = new StreamReader(stream);
this.NoteTextBox.Text = reader.ReadToEnd();
this.FilenameTextBox.Text = filename; reader.Close();
}
}
}
This is the stack trace after the class breaks:
> CarFind.DLL!CarFind.LocationDetails.OpenSavedFileFromList() Line 25 C#
CarFind.DLL!CarFind.LocationDetailsList.NotesListBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e) Line 38 C#
System.Windows.ni.dll!System.Windows.Controls.Primitives.Selector.OnSelectionChanged(System.Windows.Controls.SelectionChangedEventArgs e) Unknown
System.Windows.ni.dll!System.Windows.Controls.Primitives.Selector.InvokeSelectionChanged(System.Collections.Generic.List<object> unselectedItems, System.Collections.Generic.List<object> selectedItems) Unknown
System.Windows.ni.dll!System.Windows.Controls.Primitives.Selector.SelectionChanger.End() Unknown
System.Windows.ni.dll!System.Windows.Controls.Primitives.Selector.SelectionChanger.SelectJustThisItem(int oldIndex, int newIndex) Unknown
System.Windows.ni.dll!System.Windows.Controls.ListBox.MakeSingleSelection(int index) Unknown
System.Windows.ni.dll!System.Windows.Controls.ListBox.HandleItemSelection(System.Windows.Controls.ListBoxItem item, bool isMouseSelection) Unknown
System.Windows.ni.dll!System.Windows.Controls.ListBox.OnListBoxItemClicked(System.Windows.Controls.ListBoxItem item) Unknown
System.Windows.ni.dll!System.Windows.Controls.ListBoxItem.OnManipulationCompleted(System.Windows.Input.ManipulationCompletedEventArgs e) Unknown
System.Windows.ni.dll!System.Windows.Controls.Control.OnManipulationCompleted(System.Windows.Controls.Control ctrl, System.EventArgs e) Unknown
System.Windows.ni.dll!MS.Internal.JoltHelper.FireEvent(System.IntPtr unmanagedObj, System.IntPtr unmanagedObjArgs, int argsTypeIndex, int actualArgsTypeIndex, string eventName) Unknown
Upvotes: 2
Views: 117
Reputation: 2285
In LocationDetailsList.xaml.cs, make it
//selects saved note name based on slected index in list
private void NotesListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count > 0)
NavigationService.Navigate(new Uri(string.Format("/LocationDetails.xaml?note={0}", e.AddedItems[0]), UriKind.Relative));
}
In LocationDetails.xaml.cs, put:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if(NavigationContext.QueryString == null)
{
//QueryString is null
}
else if (NavigationContext.QueryString.ContainsKey("note"))
{
_openSavedFile(NavigationContext.QueryString["note"]);
}
else
{
//QueryString does not contain a "note" parameter and QueryString is not null
}
base.OnNavigatedTo(e);
}
private _openSavedFile(string filename)
{
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
using (var stream = new IsolatedStorageFileStream(filename, FileMode.Open, FileAccess.ReadWrite, store))
{
StreamReader reader = new StreamReader(stream);
this.NoteTextBox.Text = reader.ReadToEnd();
this.FilenameTextBox.Text = filename;
reader.Close();
}
}
Remove your old OpenSaveFile() method from LocationDetails.xaml.cs
Upvotes: 2
Reputation: 367
Add this into your LocationDetails.xaml.cs
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
OpenSavedFile();
}
Upvotes: 1
Reputation: 6091
In the code-behind for LocationDetails.xaml
, there will be a method you can override called OnNavigatedTo(...)
. You should open your file there.
protected override void OnNavigatedTo(...)
{
if (NavigationContext.QueryString.ContainsKey("note"))
{
LocationDetails myFile = new LocationDetails();
myFile.OpenSavedFile();
}
}
You navigation context will be populated with the query string you previously selected. Assuming you did everything else right :)
Upvotes: 1