首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从WinUI控件的框架中的页中检索当前WinUI 3 MainWindow的窗口句柄

如何从WinUI控件的框架中的页中检索当前WinUI 3 MainWindow的窗口句柄
EN

Stack Overflow用户
提问于 2022-03-11 00:13:58
回答 1查看 2.2K关注 0票数 1

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

我得到了这个调试错误:

我知道问题与获得主窗口的HWND有关。

代码语言:javascript
复制
// 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)

我从上面的一行中得到了这个错误:

(我试过了,但没有用)

代码语言:javascript
复制
   var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(window); 

我从上面的一行中得到了这个错误:

我不知道正确的语法。

代码语言:javascript
复制
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.";
            }
            
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-11 15:24:10

更改m_window字段在App.xaml.cs中的修饰符:

代码语言:javascript
复制
internal Window m_window;

...or最好通过一个属性公开它:

代码语言:javascript
复制
public Window Window => m_window;

然后,您可以像这样从页面访问窗口:

代码语言:javascript
复制
var window = (Application.Current as App)?.m_window as MainWindow; 

代码语言:javascript
复制
var window = (Application.Current as App)?.Window as MainWindow; 
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71432263

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档