簡體   English   中英

即使在導入 Win32 命名空間后,SaveFileDialog 在 UWP 中也不起作用

[英]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,請確保獲得以下應用程序和項目:

  • XAML 控件圖庫
    這是您獲得 OOTB 的所有控件的一站式商店,從這里開始!
  • Windows App Studio UWP 樣品
    允許您查看和調整 Windows App Studio UWP 控件和數據源庫的組件。 使用此應用程序,您可以瀏覽不同的控件,編輯屬性以實時查看更改,甚至可以復制 XAML、C# 和 JSON 代碼。 該應用程序的目的是突出 Windows App Studio 庫中的內容。
  • Windows 社區工具包示例應用程序
    這是指向已編譯示例應用程序的鏈接,這是一個新的、有時是實驗性控件的集合,您可以在 UWP 應用程序中使用這些控件。
  • 獲取 Windows 應用示例
    這是一個不斷增長的 GitHub 存儲庫庫,演示如何使 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.

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