簡體   English   中英

UWP / C#處理FileSavePicker的更有效方法?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM