-
My window contains a root frame to switch pages: <Window
x:Class="MyApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MyApp"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Frame x:Name="rootFrame" />
</Window> For a page: <Page
x:Class="MyApp.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MyApp"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid>
<Button x:Name="readFileButton" Click="chooseFile">Select a file</Button>
</Grid>
</Page> Now I want to add a function for the button to open the FilePicker dialog, which need to pass the window handle to it: IAsyncAction MainPage::chooseFile() {
FileOpenPicker picker;
auto initializeWithWindow{ picker.as<IInitializeWithWindow>() };
// Here need to pass the window handle
initializeWithWindow->Initialize(windowHandle);
picker.FileTypeFilter().Append(L"*.jpg");
this->selectedFile = co_await picker.PickSingleFileAsync();
} The problem is using the follow code to retrieve the handle only works in window class: auto windowNative{ this->try_as<IWindowNative>() };
HWND windowHandle{ 0 };
windowNative->get_WindowHandle(&windowHandle); In the page class, the runtimeclass MainPage : Microsoft.UI.Xaml.Controls.Page {
MainPage();
HWND WindowHandle;
} But there is no |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
You can set winrt::Microsoft::UI::Xaml::Window window{ nullptr }; in App.xaml.h as public instead of private |
Beta Was this translation helpful? Give feedback.
You can set winrt::Microsoft::UI::Xaml::Window window{ nullptr }; in App.xaml.h as public instead of private
Then you can access it globally from Application::Current() (with try_as < App >() )