-
Notifications
You must be signed in to change notification settings - Fork 693
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question: Use something to pick files in a WinUI 3 Blank App #4100
Comments
They can work with workarounds: #2716 (comment) |
@huoyaoyuan, thank you. This only seems to work for WinUI in UWP projects and not for WinUI in Desktop projects. |
It works for me in WinUI desktop. |
Would you please share your .csproj file? ` |
The project file is unrelated. I'm just using default of preview3. |
Thank you. I am also using WinUI preview 3. New "Blank App, Packaged (WinUI in Desktop)": `using Microsoft.UI.Xaml; // To learn more about WinUI, the WinUI project structure, namespace TestProjectWinUIDesktop /// An empty window that can be used on its own or navigated to within a Frame. /// public sealed partial class MainWindow : Window { public MainWindow() { this.InitializeComponent(); }
}` Somehow the code feature in the editor is not formatting my code, sorry for that... |
You are using the default approach, which doesn't work. You need workaround from this comment: #2716 (comment) |
I understand, thank you! I thought, since the ticket #2716 is closed it would work without a workaround. |
Hi there, In Desktop, or Win32, it's required to specify which Window Handle (HWND) owns the File/Folder Picker. It's the current design. Here you can find a code snipped: public sealed partial class MainWindow : Window
{
public MainWindow()
{
this.InitializeComponent();
}
private async void myButton_Click(object sender, RoutedEventArgs e)
{
var filePicker = new FileOpenPicker();
//Get the Window's HWND
var hwnd = this.As<IWindowNative>().WindowHandle;
//Make folder Picker work in Win32
var initializeWithWindow = filePicker.As<IInitializeWithWindow>();
initializeWithWindow.Initialize(hwnd);
filePicker.FileTypeFilter.Add("*");
var folder = await filePicker.PickSingleFileAsync();
myText.Text = folder != null ? folder.Path : string.Empty;
}
[ComImport]
[Guid("3E68D4BD-7135-4D10-8018-9FB6D9F33FA1")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IInitializeWithWindow
{
void Initialize(IntPtr hwnd);
}
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("EECDBF0E-BAE9-4CB6-A68E-9598E1CB57BB")]
internal interface IWindowNative
{
IntPtr WindowHandle { get; }
}
} |
Closing the issue. I hope the sample is good enough. |
@marb2000 the sample works fine. @marb2000 general comments/questions:
|
I also ran into this issue, and while the workaround works it's not very nice experience and took some time to find this workaround. As I use pickers in a number of places I wrote an extension method to help with this (usage: |
@marb2000 The only reason I have to stay with UWP is that I want to run in AppContainer. Thanks to @nickrandolph I can run WinUI 3 in AppContainer but attempting to use hWnd with a file open picker leads to an Access Denied exception. Please, could Microsoft provide a way to initialise popup windows without an explicit usage of a hWnd? |
I created a WinUI 3 Blank App and I am trying to select a file from the file system. Is there something in Winui 3 to support me doing so, FilePicker and OpenFileDialog do not seem to work with Winui3.
The text was updated successfully, but these errors were encountered: