Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 即使在导入Win32命名空间后,SaveFileDialog也无法在UWP中工作_C#_Uwp_Savefiledialog - Fatal编程技术网

C# 即使在导入Win32命名空间后,SaveFileDialog也无法在UWP中工作

C# 即使在导入Win32命名空间后,SaveFileDialog也无法在UWP中工作,c#,uwp,savefiledialog,C#,Uwp,Savefiledialog,我正试图用带有UWP和XAML的C#中的SaveFileDialog创建一个save按钮 我试着像上面说的那样导入Microsoft.Win32,但不起作用。SaveFileDialog()函数告诉我: 错误CS0234命名空间“Microsoft.Win32”中不存在类型或命名空间名称“SaveFileDialog”(是否缺少程序集引用?) 这是我的代码: Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDial

我正试图用带有UWP和XAML的C#中的SaveFileDialog创建一个save按钮

我试着像上面说的那样导入
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";

如何解决此问题?

您好,您需要添加引用窗口。soultion资源管理器中的表单在解决方案资源管理器中的引用上单击鼠标右键,然后单击“添加引用”并将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 
          
            }

不要遵循UWP在预览时提供的非常古老的建议,您应该仔细阅读当前的MS Docs文章,这些文章有许多常见应用程序范例的非常简单的示例

一般来说,如果您现在开始开发一个新的应用程序,并且选择了UWP,那么您应该尽量避免直接引用Win32或旧的.Net运行时。虽然您可以使其正常工作,但向后兼容性旨在支持开发人员将遗留代码转移到.Net Core,对于许多控件,已经有了本机Xaml或WinUI实现

请看一下如何使用to

//创建对话框,此块相当于OPs原始代码。
var savePicker=new Windows.Storage.Pickers.FileSavePicker();
savePicker.SuggestedStartLocation=
Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary;
//用户可以将文件保存为的文件类型下拉列表
savePicker.FileTypeChoices.Add(“纯文本”,新列表(){.txt});
//如果用户未键入文件名或未选择要替换的文件,则为默认文件名
savePicker.SuggestedFileName=“文档”;
//显示用户指定目标文件的对话框
//如果单击“保存”,对话框将返回对文件的引用
//如果用户取消对话框,则返回null
Windows.Storage.StorageFile file=wait savePicker.PickSaveFileAsync();
如果(文件!=null)
{
//阻止更新文件的远程版本,直到
//我们完成更改并调用CompleteUpdatesAsync。
DeferUpdates(文件);
//写入文件
等待Windows.Storage.FileIO.WriteTextAsync(文件,文件名);
//让Windows知道我们已完成更改文件,以便
//另一个应用程序可以更新文件的远程版本。
//完成更新可能需要Windows请求用户输入。
Windows.Storage.Provider.FileUpdateStatus状态=
等待Windows.Storage.CachedFileManager.CompleteUpdatesAsync(文件);
如果(状态==Windows.Storage.Provider.FileUpdateStatus.Complete)
{
System.Diagnostics.Debug.WriteLine(“文件”+File.Name+“已保存”);
}
其他的
{
System.Diagnostics.Debug.WriteLine(“文件”+文件名+“无法保存”);
}
}
其他的
{
System.Diagnostics.Debug.WriteLine(“用户取消了保存对话框”);
}

如果您刚开始使用UWP,请确保获得以下应用程序和项目:


  • 这是您获得的所有控制的一站式商店OOTB,从这里开始

  • 允许您查看和调整Windows App Studio UWP控件和数据源库的组件。使用此应用程序,您可以浏览不同的控件,编辑属性以实时查看更改,甚至可以复制XAML、C#和JSON代码。该应用程序的目的是突出显示Windows app Studio库中的内容

  • 这是到已编译示例应用程序的链接,这是一个新控件的集合,有时是实验控件,可以在UWP应用程序中使用

  • 这是一个不断增长的GitHub repo库,它演示了如何让UWP为您工作
根据的文档,它提到API适用于.NET Core 3.0和3.1。但是UWP目前只支持.NETCore2.0。这就是为什么你会遇到这种意想不到的情况。您发布的链接是WPF问题,可能不适合UWP


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

请添加源代码,以便我们了解问题所在。哦,我很抱歉!我刚刚编辑过!为什么要使用Win32 SaveFileDialog?我不知道还可以使用什么,请遵循MS文档中与您开发的平台和框架相匹配的正确分支,这一点很重要。9年前,UWP是一个非常不同的野兽。对于当前的UWP实现,请同时查找WinUI 2+引用。同样在9年前,也就是说,无论花费多少,只要有人找到了答案,这并不意味着他们问了正确的问题,或者你应该做他们所做的事情。他们还询问了WPF,因此与您完全没有关系给了我几个错误:其中两个错误是“
表单
”在命名空间
窗口中不存在。表单
,另一个错误是我没有使用它。当我尝试
System.Windows.Forms.SaveFileDialog
时,我得到了与上面相同的错误。从“项目”选项卡添加新的Windows窗体,如果代码成功无误,请使用“保存文件”对话框重试谢谢,这很完美,您链接的ms文档页面工作得很好。
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;
// 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.");
}