UWP中即使导入了Win32命名空间,SaveFileDialog仍然无法工作。

3
我正在尝试在UWP和XAML中使用C#中的SaveFileDialog创建保存按钮。
我尝试按照此贴所说的导入 Microsoft.Win32,但它不起作用。 SaveFileDialog() 函数告诉我:
“错误 CS0234:名称空间“Microsoft.Win32”中不存在类型或命名空间名称'SaveFileDialog'(您是否缺少程序集引用?)”
这是我的代码:
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
            dlg.FileName = "Document";
            dlg.DefaultExt = ".txt";
            dlg.Filter = "Text documents (.txt)|*.txt";

我该如何修复这个问题?


2
请添加源代码,以便我们查看问题所在。 - BenceL
哦,非常抱歉!我刚刚编辑了它! - altude
你为什么想要使用Win32 SaveFileDialog? - Chris Schaller
我不知道还能用什么,只是按照这个stackoverflow帖子和这个微软文档网页的方法。 - altude
1
重要的是您要遵循与您所开发的平台和框架相匹配的正确MS Docs分支,9年前的UWP已经有了很大的变化,针对当前的UWP实现,还要查找WinUI 2+的参考资料。同时,在9年前,SO只关注完成任务,无论代价多大,仅仅因为某人找到了答案,并不意味着他们问了正确的问题,或者您应该像他们那样去做。此外,他们正在询问WPF,与您无关。 - Chris Schaller
3个回答

4

不要跟随过时的建议,那是在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的新手,确保获取以下应用程序和项目:
  • XAML控件库
    这是一个可获取OOTB(开箱即用)所有控件的一站式商店,从这里开始!
  • Windows App Studio UWP示例
    允许您查看和调整Windows App Studio UWP控件和数据源库的组件。通过此应用程序,您可以浏览不同的控件,编辑属性以实时查看更改甚至复制XAML、C#和JSON代码。该应用程序的目的是突出显示Windows App Studio Libraries中的内容。
  • Windows社区工具包示例应用程序
    这是一个已编译示例应用程序的链接,其中包含一些新的,有时是实验性的控件,您可以在UWP应用程序中使用它们。
  • 获取Windows App示例
    这是一个日益增长的GitHub仓库的库,演示如何让UWP为您工作。

谢谢,这太完美了,你提供的微软文档页面非常好用。 - altude

0

你好,你需要在解决方案资源管理器中添加引用Windows.Forms,右键单击解决方案资源管理器中的“引用”,然后点击“添加引用”,并将System.Windows.Forms添加到你的项目中。当你添加了这个之后,就可以将System.Windows.Forms添加到你的命名空间中了。

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;

添加 System.Windows.Forms; 后出现了几个错误: 其中两个是 "Forms" 在命名空间 Windows.Forms 中不存在,另一个是我没有使用它。当我尝试使用 System.Windows.Forms.SaveFileDialog 时,出现了与上述相同的错误。 - altude
从项目选项卡中添加新的Windows窗体,如果代码成功运行且没有错误,则可以尝试使用“保存文件对话框”,然后您可以从WPF项目中删除无用的Form1。 - Ali Rezaie

0
根据SaveFileDialog Class文档,API适用于.NET Core 3.0和3.1。但是UWP目前仅支持.NET Core 2.0。这就是为什么您会遇到此问题。您发布的链接是一个WPF问题,可能不适合UWP。
@Chris Schaller的解决方案是正确的,您可以尝试使用FileSavePicker

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接