[英]SaveFileDialog is not working in UWP even after import Win32 namespace
我正在嘗試使用 C# 中的 SaveFileDialog 和 UWP 和 XAML 創建一個保存按鈕。
我試圖像這篇文章所說的那樣導入Microsoft.Win32
,但它不起作用。 SaveFileDialog()
function 告訴我:
錯誤 CS0234 命名空間“Microsoft.Win32”中不存在類型或命名空間名稱“SaveFileDialog”(您是否缺少程序集引用?)
這是我的代碼:
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
dlg.FileName = "Document";
dlg.DefaultExt = ".txt";
dlg.Filter = "Text documents (.txt)|*.txt";
我怎樣才能解決這個問題?
不要遵循在 UWP 處於預覽階段時提供的非常古老的建議,您應該按照自己的方式閱讀當前的 MS Docs 文章,這些文章為許多常見的應用程序范例提供了很好的簡單示例。
作為一般規則,如果您今天開始開發新的應用程序,並且您選擇了 UWP,那么您應該盡量避免直接引用 Win32 或舊的 .Net 運行時。 雖然您可以使其工作,但向后兼容性旨在支持開發人員將遺留代碼轉移到 .Net Core,對於許多控件,已經有原生 Xaml 或 WinUI 實現。
看看使用FileSavePicker來
// Create the dialog, this block is equivalent to OPs original code.
var savePicker = new Windows.Storage.Pickers.FileSavePicker();
savePicker.SuggestedStartLocation =
Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary;
// Dropdown of file types the user can save the file as
savePicker.FileTypeChoices.Add("Plain Text", new List<string>() { ".txt" });
// Default file name if the user does not type one in or select a file to replace
savePicker.SuggestedFileName = "Document";
// Show the dialog for the user to specify the target file
// The dialog will return a reference to the file if they click on "save"
// If the user cancels the dialog, null will be returned
Windows.Storage.StorageFile file = await savePicker.PickSaveFileAsync();
if (file != null)
{
// Prevent updates to the remote version of the file until
// we finish making changes and call CompleteUpdatesAsync.
Windows.Storage.CachedFileManager.DeferUpdates(file);
// write to file
await Windows.Storage.FileIO.WriteTextAsync(file, file.Name);
// Let Windows know that we're finished changing the file so
// the other app can update the remote version of the file.
// Completing updates may require Windows to ask for user input.
Windows.Storage.Provider.FileUpdateStatus status =
await Windows.Storage.CachedFileManager.CompleteUpdatesAsync(file);
if (status == Windows.Storage.Provider.FileUpdateStatus.Complete)
{
System.Diagnostics.Debug.WriteLine("File " + file.Name + " was saved.");
}
else
{
System.Diagnostics.Debug.WriteLine("File " + file.Name + " couldn't be saved.");
}
}
else
{
System.Diagnostics.Debug.WriteLine("User cancelled the save dialog.");
}
如果您剛開始使用 UWP,請確保獲得以下應用程序和項目:
Hi You need to Add Refrence Windows.Forms from soultion Explorer Right Click on Refrences in Solution Explorer and Click Add Refrence And add System.Windows.Forms To your Project When You Added This You Can Add System.Windows.Forms to Your Name Spaces
using System.Windows.Forms;
在此之后你可以使用 SaveFileDialog
我使用這種方式我的代碼:
SaveFileDialog save = new SaveFileDialog();
Nullable<bool> result = save.ShowDialog();
if (result==true)
{
//Your Code here
}
我在 WPF 項目中使用的命名空間代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Net;
using System.Net.NetworkInformation;
using System.Text.RegularExpressions;
using System.IO;
using Microsoft.Win32;
using System.Windows.Forms;
using System.Threading;
根據SaveFileDialog Class的文檔,它提到 API 適用於 .NET Core 3.0 和 3.1。 但是 UWP 目前只支持 .NET Core 2.0。 這就是為什么你會遇到這種意外。 您發布的鏈接是 WPF 問題,它可能不適合 UWP。
@Chris Schaller 解決方案是正確的,您可以嘗試使用FileSavePicker 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.