Windows 应用商店中的文本编辑器应用






2.33/5 (2投票s)
这是一个适用于 Windows 8 的文本编辑器应用。
引言
这是一个适用于 Windows 8 的纯文本编辑器。 尽管它是一个相对简单的应用,但我涵盖了 Windows 8 平台的一些重要应用开发概念。 其中一些概念如下:
- 使用文件选择器保存和打开文件
- 保存和恢复应用程序状态
- 将文本数据写入磁盘上的文件以及从磁盘上的文件中读取文本数据
背景
文件选择器对于允许用户在硬盘上定位文件非常有用。 有两种类型的文件选择器。 FileSavePicker
类提供了通过指定默认位置、建议的文件名和要保存的文件类型来保存磁盘上文件的功能。 同样, FileOpenPicker
类提供了通过指定默认位置和要保存的文件类型来从磁盘打开文件的功能。
应用程序数据容器可用于在应用容器中保存应用的状态。 应用程序数据容器的优点是即使应用终止,它也可以用于自动保存和恢复应用程序状态。
FileIO
类可用于保存磁盘上文件的内容以及从磁盘上读取文件的内容。 它提供了 WriteTextAsync()
方法将文件内容写入磁盘,以及 ReadTextAsync()
方法从磁盘读取文件内容。
使用代码
编辑器应用由一个多行文本框组成,其内容需要被写入和读取。 以下 XAML 代码用于创建多行文本框
<TextBox Name="txtEditor" Canvas.Left="250" Canvas.Top="200" Width="700" Height="400"
AcceptsReturn="True" TextWrapping="Wrap" FontSize="20" ScrollViewer.VerticalScrollBarVisibility="Auto"
TextChanged="txtEditor_TextChanged_1"/>
在上面的代码中, AcceptsReturn
属性设置为 "True" 以使 TextBox 成为多行, TextWrapping
属性设置为 "Wrap" 以启用文本换行。
以下是文本编辑器应用的完整 xaml 代码
TextChanged
事件用于在 TextBox 的内容每次更改时将 TextBox 内容保存到应用程序容器中。 以下是 TextChanged
事件处理程序的代码
private void txtEditor_TextChanged_1(object sender, TextChangedEventArgs e)
{
ApplicationDataContainer container = ApplicationData.Current.LocalSettings;
container.Values["CONTENT"] = txtEditor.Text;
}
上面的代码使用键“CONTENT”将 TextBox 的内容保存在应用程序容器中。
以下 MainPage 的 OnNavigatedTo
事件中的代码用于从应用程序容器恢复 TextBox 内容
protected override void OnNavigatedTo(NavigationEventArgs e)
{
ApplicationDataContainer container = ApplicationData.Current.LocalSettings;
if (container.Values.ContainsKey("CONTENT"))
{
txtEditor.Text = container.Values["CONTENT"] as string;
}
}
在每次应用重新启动时,上面的代码会检查应用程序容器中是否存在键“CONTENT”,然后恢复应用程序状态并将其显示在 TextBox 中。
以下是 Save 按钮的点击事件中的代码,用于将 TextBox 内容永久保存在用户指定的文件中。
private async void btnSave_Click_1(object sender, RoutedEventArgs e)
{
FileSavePicker picker = new FileSavePicker();
picker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
picker.SuggestedFileName = "New File";
picker.FileTypeChoices.Add("Text Files", new List() { ".txt" });
picker.FileTypeChoices.Add("HTML Files", new List() { ".html" });
StorageFile file = await picker.PickSaveFileAsync();
if (file != null)
{
await FileIO.WriteTextAsync(file, txtEditor.Text);
}
}
上面的代码将 TextBox 的内容保存在一个文件中,该文件的位置、名称和类型由用户指定。 FileSavePicker
类的对象用于指定文件的位置、名称和类型。 FileSavePicker
类的 PickSaveFileAsync
方法返回一个 StorageFile
类的实例,该实例表示用户指定的文件。 FileIO
类的 WriteTextAsync()
方法将 TextBox 的内容保存到文件中。 此方法接受两个参数,第一个是 Storage 文件对象,第二个是要保存的文本。
以下是 Open 按钮的点击事件中的代码,用于从用户选择的文件中检索 TextBox 内容。
private async void btnOpen_Click_1(object sender, RoutedEventArgs e)
{
FileOpenPicker picker = new FileOpenPicker();
picker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
picker.FileTypeFilter.Add(".txt");
picker.FileTypeFilter.Add(".html");
StorageFile file = await picker.PickSingleFileAsync();
if (file != null)
{
txtEditor.Text = await FileIO.ReadTextAsync(file);
}
}
上面的代码读取文件的内容,该文件的位置、名称和类型由用户指定,并在 TextBox 中显示内容。 FileOpenPicker
类的对象用于指定文件的位置和类型。 FileOpenPicker
类的 PickSingleFileAsync()
方法返回一个 StorageFile
类的实例,该实例表示用户选择的文件。 FileIO
类的 ReadTextAsync()
方法读取文件的内容。 此方法接受一个类型为 StorageFile
的参数,并以字符串格式返回文件的内容。
关注点
我希望这篇文章对理解 Windows 商店应用中的基本文件处理概念有所帮助。