打开保存的文件时出错

本文关键字:出错 文件 保存 | 更新日期: 2023-09-27 17:49:15

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

在这个类中,文件名存储在一个列表中,当一个名称被选中时,文件名传递给另一个类,并调用我的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

打开保存的文件时出错

在locationdetailslist . example .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 . example .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()方法

LocationDetails.xaml的代码后面,将有一个方法,您可以覆盖称为OnNavigatedTo(...)。你应该在那里打开文件。

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

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

将此添加到您的locationdetails . example .cs

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