我正在创建我的第一个WinUI 3桌面应用程序。我的NavigationView在我的MainWindow上。我通过NavigationView中的框架导航到9个不同的页面。其中一页用于打印报告。我需要让FileSavePicker从“reportPage”开始工作。我直接按照learn.microsoft.com的示例实现了下面的内容。(我还在下面添加了同样的FileSavePicker代码段到一个小型虚拟winui-3沙箱测试应用程序中,但我将代码放在MainWindow的代码背后,它运行得很好。)我需要让FileSavePicker从一个页面而不是一个MainWindow来工作。到目前为止,谢谢大家的帮助。
我得到了这个调试错误:

我知道问题与获得主窗口的HWND有关。
// Retrieve the window handle (HWND) of the current WinUI 3 window.
var window = (MainWindow)Application.Current.MainWindow; (I tried this, but it did not work)我从上面的一行中得到了这个错误:

(我试过了,但没有用)
var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(window); 我从上面的一行中得到了这个错误:

我不知道正确的语法。
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using System;
using System.Collections.Generic;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.Storage.Provider;
namespace MetricReporting.Pages
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class pageReports : Page
{
public pageReports()
{
this.InitializeComponent();
}
private void ButtonBoltReport_Click(object sender, RoutedEventArgs e)
{
DisplayBOLTSaveDialog();
}
private async void DisplayBOLTSaveDialog()
{
FileSavePicker savePicker = new FileSavePicker();
// Retrieve the window handle (HWND) of the current WinUI 3 window.
var window = (MainWindow)Application.Current.MainWindow;
var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
// Initialize the folder picker with the window handle (HWND).
WinRT.Interop.InitializeWithWindow.Initialize(savePicker, hWnd);
savePicker.SuggestedStartLocation = 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 = "New Document";
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.
CachedFileManager.DeferUpdates(file);
// write to file
await 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.
FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(file);
if (status == FileUpdateStatus.Complete)
{
ReportStatus.Text = "File " + file.Name + " was saved.";
}
else
{
ReportStatus.Text = "File " + file.Name + " couldn't be saved.";
}
}
else
{
ReportStatus.Text = "Operation cancelled.";
}
}
}
}发布于 2022-03-11 15:24:10
更改m_window字段在App.xaml.cs中的修饰符:
internal Window m_window;...or最好通过一个属性公开它:
public Window Window => m_window;然后,您可以像这样从页面访问窗口:
var window = (Application.Current as App)?.m_window as MainWindow; 或
var window = (Application.Current as App)?.Window as MainWindow; https://stackoverflow.com/questions/71432263
复制相似问题