gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 20:50:48 25 4
gpt4 key购买 nike

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

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.

这是下面的两种方法,有人可以用这个为我指明正确的方向,或者告诉我哪里出错了吗?

将文件名传递给 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com