[英]UWP/C# More efficient way of handling FileSavePicker?
我已经在按下按钮时运行了以下FileSavePicker
函数。 我想知道这是否是处理FileSave的可接受方法,或者在处理保存文件时是否应该采用更好的做法?
// Save File SubScriptHere:
private async void SaveButton_ClickAsync(object sender, RoutedEventArgs e)
{
FileSavePicker savePicker = new FileSavePicker();
savePicker.FileTypeChoices.Add("Text Document", new List<string> { ".txt" });
savePicker.FileTypeChoices.Add("CSV Document", new List<string> { ".csv" });
StorageFile file = await savePicker.PickSaveFileAsync();
}
它似乎很基本,并且运行得很好。 我只是想尽可能地养成良好的习惯,因为我还在学习。 我应该处理错误还是确认文件是否正确保存等? 对于我来说,似乎太准了,我不满意
我想知道这是否是处理FileSave的可接受方法...
是的,的确如此。 您需要记住要照顾文件,即将其保存在某个地方。 FileSavePicker
仅允许用户选择文件的文件名,扩展名和存储位置。 您可以使用FileIO.WriteTextAsync
方法实际保存它:
private async void SaveButton_ClickAsync(object sender, RoutedEventArgs e)
{
FileSavePicker savePicker = new FileSavePicker();
savePicker.FileTypeChoices.Add("Text Document", new List<string> { ".txt" });
savePicker.FileTypeChoices.Add("CSV Document", new List<string> { ".csv" });
StorageFile file = await savePicker.PickSaveFileAsync();
if (file != null)
await FileIO.WriteTextAsync(file, "contents...");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.