我正在创建我的第一个 WinUI 3 桌面应用程序。我的主窗口上有一个导航视图。 我通过 NavigationView 中的框架导航到 9 个不同的页面。 其中一页用于打印报告。我需要让 FileSavePicker 从“reportPage”工作。我直接按照 learn.microsoft.com 的示例实现了以下内容。 (我还在下面的一个小型虚拟 winui-3 沙箱测试应用程序中添加了相同的 FileSavePicker 代码段,但我将代码放在 MainWindow 的代码隐藏中,并且它工作得很好。)我需要让 FileSavePicker 工作页面而不是主窗口。 感谢大家迄今为止的帮助。
我收到此调试错误:
我知道问题与获取主窗口的 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.";
}
}
}
}
更改 App.xaml.cs 中
m_window
字段的修饰符:
internal Window m_window;
...或者更好的是通过属性公开它:
public Window Window => m_window;
然后您可以从页面访问该窗口,如下所示:
var window = (Application.Current as App)?.m_window as MainWindow;
或
var window = (Application.Current as App)?.Window as MainWindow;
我发现了另一种方法,微软发布的 WinUI 3 Gallery 应用程序对此进行了描述。它位于示例中的
System
> FilePicker
下。
上面写着:
在 App.xaml.cs 中,您可以通过将 MainWindow 设置为静态来使其可访问。
这是他们提供的代码片段,对我有用。
public partial class App : Application
{
public static MainWindow MainWindow = new();
public App()
{
this.InitializeComponent();
}
protected override void OnLaunched(LaunchActivatedEventArgs args)
{
MainWindow.Activate();
}
}
然后在我的其他文件中,我可以简单地编写
App.MainWindow
来引用该窗口。