title: 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
body: <p>I am creating my first WinUI 3 desktop application. I have a NavigationView on my MainWindow.
  I have 9 different pages that I navigate to via the frame in the NavigationView.
  One of the pages is for printing reports. I need to get the FileSavePicker to work from the 'reportPage'.  I have implemented the below, directly following the examples from the docs.microsoft.com.  (I also added the same FileSavePicker code segment below to a small dummy winui-3 sand-box test app, but instead I put the code on the MainWindow's code-behind and it worked perfectly.) I need to get the FileSavePicker to work from a page instead of a MainWindow.
  Thank you all for your help so far.</p>
  <p>I get this debug error:</p>
  <p><a href="https://i.stack.imgur.com/JQFUF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JQFUF.png" alt="enter image description here" /></a></p>
  <p>I know the problem has to do with getting the MainWindow's HWND.</p>
  <pre><code>// 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)
  </code></pre>
  <p>I get this error from the above line:
  <a href="https://i.stack.imgur.com/7qxrS.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7qxrS.png" alt="enter image description here" /></a></p>
  <p>(I tried this, but it did not work)</p>
  <pre><code>   var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(window); 
  </code></pre>
  <p>I get this error from the line above:</p>
  <p><a href="https://i.stack.imgur.com/Hqzm7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Hqzm7.png" alt="enter image description here" /></a></p>
  <p>I don't know the correct syntax.</p>
  <pre><code>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
  {
      /// &lt;summary&gt;
      /// An empty page that can be used on its own or navigated to within a Frame.
      /// &lt;/summary&gt;
      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(&quot;Plain Text&quot;, new List&lt;string&gt;() { &quot;.txt&quot; });
              // Default file name if the user does not type one in or select a file to replace
              savePicker.SuggestedFileName = &quot;New Document&quot;;
  
              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 = &quot;File &quot; + file.Name + &quot; was saved.&quot;;
                  }
                  else
                  {
                      ReportStatus.Text = &quot;File &quot; + file.Name + &quot; couldn't be saved.&quot;;
                  }
              }
              else
              {
                  ReportStatus.Text = &quot;Operation cancelled.&quot;;
              }
              
          }
      }
  }
  </code></pre>