【问题标题】:SaveFileDialog is not working in UWP even after import Win32 namespace即使在导入 Win32 命名空间后,SaveFileDialog 在 UWP 中也不起作用
【发布时间】:2021-05-17 08:12:20
【问题描述】:

我正在尝试使用 UWP 和 XAML 在 C# 中使用 SaveFileDialog 创建一个保存按钮。

我尝试像this post 所说的那样导入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";

我该如何解决这个问题?

【问题讨论】:

  • 请添加源代码,以便我们查看问题所在。
  • 哦,对不起!我刚刚编辑了它!
  • 为什么要使用 Win32 SaveFileDialog?
  • 不知道还能用什么,就跟着this stackoverflow postthis microsoft docs page
  • 遵循与您正在开发的平台和框架相匹配的 MS Docs 的正确分支很重要,9 年前 UWP 是一个非常不同的野兽,对于 UWP 的当前实现,还要寻找WinUI 2+ 参考。同样在 9 年前,SO 就是要完成它,无论付出什么代价,仅仅因为有人找到了答案,这并不意味着他们提出了正确的问题,或者你应该做他们所做的事情。他们也在询问 WPF,所以与你完全无关。

标签: c# uwp savefiledialog


【解决方案1】:

您好,您需要从灵魂资源管理器中添加引用 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; 给了我几个错误:其中两个是命名空间 Windows.Forms 中不存在“Forms”,另一个是我没有使用它.当我尝试System.Windows.Forms.SaveFileDialog 时,我得到与上面相同的错误。
  • 从项目选项卡添加新的 Windows 窗体,如果代码成功运行且没有错误,则使用保存文件对话框重试?您可以从 WPF 项目中删除无用的 Form1
【解决方案2】:

您不应遵循 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 Controls Gallery
    这是您获得 OOTB 的所有控件的一站式商店,从这里开始!
  • Windows App Studio UWP Samples
    允许您查看和调整 Windows App Studio UWP 控件和数据源库的组件。使用此应用程序,您可以浏览不同的控件、编辑属性以实时查看更改,甚至可以复制 XAML、C# 和 JSON 代码。该应用的目的是突出显示 Windows App Studio 库中的内容。
  • Windows Community Toolkit Sample App
    这是指向已编译示例应用的链接,这是您可以在 UWP 应用中使用的新的有时是实验性控件的集合。
  • Get Windows App Samples
    这是一个不断壮大的 GitHub 存储库,展示了如何让 UWP 为您服务。

【讨论】:

  • 谢谢,这太完美了,您链接的 ms 文档页面效果很好。
【解决方案3】:

根据SaveFileDialog Class的文档,提到该API适用于.NET Core 3.0和3.1。但 UWP 目前只支持 .NET Core 2.0。这就是为什么你会遇到这种意外。您发布的链接是 WPF 问题,可能不适合 UWP。

@Chris Schaller 的解决方案是正确的,您可以尝试使用 FileSavePicker

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-16
    • 2015-02-22
    • 2015-11-14
    • 2021-07-02
    • 2020-02-14
    相关资源
    最近更新 更多