如何从 MainWindow 上 NavigationView 控件的框架中的页面检索当前 WinUI 3 MainWindow 的 window 句柄
How to retrieve the window handle of the current WinUI 3 MainWindow from a page in a frame in a NavigationView control on the MainWindow
我正在创建我的第一个 WinUI 3 桌面应用程序。我的 MainWindow 上有一个 NavigationView。
我有 9 个不同的页面,我通过 NavigationView 中的框架导航到这些页面。
其中一页用于打印报告。我需要让 FileSavePicker 从 'reportPage' 开始工作。我已经实施了以下内容,直接遵循 docs.microsoft.com 中的示例。 (我还在下面的一个小型虚拟 winui-3 沙盒测试应用程序中添加了相同的 FileSavePicker 代码段,但我将代码放在 MainWindow 的代码隐藏中并且运行良好。)我需要让 FileSavePicker 从页面而不是 MainWindow。
感谢大家迄今为止的帮助。
我收到这个调试错误:
我知道问题与获取 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.";
}
}
}
}
更改 App.xaml.cs 中 m_window
字段的修饰符:
internal Window m_window;
...或者更好的是通过 属性:
公开它
public Window Window => m_window;
然后您可以像这样从页面访问window:
var window = (Application.Current as App)?.m_window as MainWindow;
或
var window = (Application.Current as App)?.Window as MainWindow;
我正在创建我的第一个 WinUI 3 桌面应用程序。我的 MainWindow 上有一个 NavigationView。 我有 9 个不同的页面,我通过 NavigationView 中的框架导航到这些页面。 其中一页用于打印报告。我需要让 FileSavePicker 从 'reportPage' 开始工作。我已经实施了以下内容,直接遵循 docs.microsoft.com 中的示例。 (我还在下面的一个小型虚拟 winui-3 沙盒测试应用程序中添加了相同的 FileSavePicker 代码段,但我将代码放在 MainWindow 的代码隐藏中并且运行良好。)我需要让 FileSavePicker 从页面而不是 MainWindow。 感谢大家迄今为止的帮助。
我收到这个调试错误:
我知道问题与获取 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.";
}
}
}
}
更改 App.xaml.cs 中 m_window
字段的修饰符:
internal Window m_window;
...或者更好的是通过 属性:
公开它public Window Window => m_window;
然后您可以像这样从页面访问window:
var window = (Application.Current as App)?.m_window as MainWindow;
或
var window = (Application.Current as App)?.Window as MainWindow;