c# - 打开保存的文件时出错

标签 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

最佳答案

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

关于c# - 打开保存的文件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22289740/

相关文章:

xml - WP7 在 IsolatedStorage 中读写 Xml

c# - 从项目文件夹动态加载图像 - Windows Phone 7

.Net 用户范围应用程序设置文件路径问题

c# - 通过托管代码 (.NET)/命令行启用 Windows 事件日志

c# - 在 C# 中从 Socket 接收数据的正确方法是什么?

c#-4.0 - 将图像发布到 Windows Phone 8 中的 Web 服务器

c# - 如何在 LongListSelector 中显示/隐藏(如果可能,使用动画)复选框

c# - ASP.NET core 在 IIS 中运行启动错误日志记录

c# - 如何在 C# 中定义全息坐标系?

c# - 使用 WPD 将文件复制到 Windows Phone C#