Improved APIs for Picker/Dialog classes that implement IInitializeWithWindow #1887
Replies: 4 comments 2 replies
-
Related to #1063. |
Beta Was this translation helpful? Give feedback.
-
While you're improving FolderPicker, please consider relaxing the SuggestedStartLocation from the PickerLocationId enumeration to any folder location. What made sense in the sandbox doesn't work so well in the Win32 world. |
Beta Was this translation helpful? Give feedback.
-
I don't see much point in duplicating system file pickers as they're UI framework agnostic and currently WindowsAppSDK also force-installs WinUI 3, making it unusable if WinUI 3 isn't required unless you're willing to include it without using it. While the system APIs could be updated themselves, it won't be backward compatible and the Initialize method already exists in the object, just not accessible from the projected world. Language projections could make the Initialize method directly avoid without needing to query for a classic COM interface or use the interop wrapper. Ideally, I want to the pickers and other UI objects to work directly just like in UWP, it can obtain the window ID of the currently focused window in the process. |
Beta Was this translation helpful? Give feedback.
-
The API is good here but much more pressing than the current API not being clean is that it doesn't work at all in AppContainer, meaning that pickers cannot be used! microsoft/microsoft-ui-xaml#9557 Does the new approach potentially fix this @angelazhangmsft ? |
Beta Was this translation helpful? Give feedback.
-
The team is looking at improving the API pattern for several picker/dialog classes that currently require interop code with IInitializeWithWindow. We would love to hear your feedback on the proposal below!
Summary
Several WinRT classes (e.g. FileOpenPicker, FolderPicker, MessageDialog) require a CoreWindow to call certain methods, typically to display a UI. However, CoreWindow is not supported in desktop apps so these WinRT classes cannot be used in desktop apps by default (using the same API usage pattern as in UWP). Some of the classes implement the IInitializeWithWindow interface, which enables using these APIs in desktop apps but requires interop with IInitializeWithWindow, which is not developer-friendly or discoverable.
See issues: microsoft/microsoft-ui-xaml#2716, #1063
Current API pattern
C# (Current API pattern)
C++ (Current API pattern)
Proposed Solution
Introduce new WinAppSDK 1.x APIs that support WindowId parameter
These new APIs would be available with the Windows App SDK and wrap underlying Windows APIs. For example, a
Microsoft.Windows.Storage.Pickers.FolderPicker
that wrapsWindows.Storage.Pickers.FolderPicker
but adds a required WindowId-based constructor.There are existing CSWinRT-generated APIs in the
WinRT.Interop
namespace that make WindowId from various types easy to get, such as from a Xaml Window and AppWindow. These can be used to call the picker/dialog API with the correct WindowId value. We can retrieve a Microsoft.UI.WindowId object from the AppWindow APIs, passing in whichever framework is being used (WPF, Winforms, HWND, etc), and construct the picker/dialog object with the WindowId parameter.The following APIs fall under this pattern:
C# (Proposed API pattern)
Note: Long term,
Microsoft.UI.WindowId
would be a property of the Window object, which would simplify the code to obtain the WindowId (eliminating the requirement to obtain an hwnd). This would simplify the picker initialization code to something like:Alternative Solutions
Introduce new WinAppSDK 1.x APIs that support parameter-less constructors
Ideally, we would support parameter-less constructors in addition to supporting explicit WindowId (the above Proposed Solution).
C# (Proposed API pattern)
Possible approaches and considerations:
Open Questions
Window.Storage.Pickers.FolderPicker
andMicrosoft.Windows.Storage.Pickers.FolderPicker
), or should we change these class names? Duplicating the class names may result in confusion with Intellisense since both classes will show up.Beta Was this translation helpful? Give feedback.
All reactions