打开保存的文件时出错

时间:2014-03-09 23:37:42

标签: c# windows-phone-8 isolatedstorage

我根据从另一个类传递的文件名创建了一个打开保存文件的方法,但我不确定我是否在类之间正确调用或传递文件。

在此类中,文件名存储在列表中,当选择名称时,文件名将传递给另一个类,并调用我的OpenSavedFile方法。

以下两种方法,有人可以指出我正确的方向或告诉我哪里出错了吗?

将文件名传递给LocationDetails类的方法:

//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();

        }

我的方法应该根据从其他前一个类传递的名称打开文件,但是当执行第一个语句时,它会出现此错误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();
                }
            }
        }

这是课程休息后的堆栈跟踪:

>   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

3 个答案:

答案 0 :(得分:2)

在LocationDetailsList.xaml.cs中,将其设为

//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.xaml.cs中,输入:

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();
    }
}

从LocationDetails.xaml.cs

中删除旧的OpenSaveFile()方法

答案 1 :(得分:1)

LocationDetails.xaml的代码隐藏中,会有一个名为OnNavigatedTo(...)的覆盖方法。你应该在那里打开文件。

protected override void OnNavigatedTo(...)
{
    if (NavigationContext.QueryString.ContainsKey("note"))
    {
        LocationDetails myFile = new LocationDetails();
        myFile.OpenSavedFile();
    }
}

您的导航上下文将使用您之前选择的查询字符串填充。假设你做的一切正确:)

答案 2 :(得分:1)

将其添加到LocationDetails.xaml.cs

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);

        OpenSavedFile();
    }