microsoft / microsoft-ui-xaml

WinUI: a modern UI framework with a rich set of controls and styles to build dynamic and high-performing Windows applications.
MIT License
6.38k stars 683 forks source link

Proposal: XamlApplication: Application implementing IXamlMetadataPorvider interface #8475

Open sotanakamura opened 1 year ago

sotanakamura commented 1 year ago

Proposal: XamlApplication: Application implementing IXamlMetadataPorvider interface

Summary

XamlApplication is a Application implementing IXamlMetadataProvider interface and loading WinUI theme. This API provides an easy way to create an application integrating XAML.

runtimeclass XamlApplication : Microsoft.UI.Xaml.Application, Microsoft.UI.Xaml.Markup.IXamlMetadataProvider

Rationale

Scope

Capability Priority
This proposal will allow developers to make a code simpler Could

Important Notes

This API was proposed by @Scottj1s in Meet C++/WinRT 2.0: Faster and smarter in the open - BRK4009.
The sample code implemented by Kenny Kerr.

wjk commented 1 year ago

As it is, the application does implement IXamlMetadataProvider. This is defined in the generated C++ file for the app class. Furthermore, as I understand it, IXamlMetadataProvider must be able to return full type information, including callbacks to get or set properties by name, for every type used in XAML in your application. If it doesn’t, those types will not be usable from XAML as then WinUI will have no idea how to configure the object used to match the values in the XAML. If you do as you describe, then you may end up being unable to use any types defined in your project from XAML, including code-behinds; the only types you could successfully use are the ones supported by XamlControlsXamlMetaDataProvider. Of course there is no documentation for any of this.

IXamlMetadataProvider and the objects it returns are how the C++-based WinUI 3 core library is able to manipulate objects implemented in any programming language. (C++ types call through to other C++ code, C# types go through a CCW and are handled in managed code, and so on.) You could use these APIs to create your own type metadata system, which unfortunately is intended to be used only when you are porting a new language to WinRT. It is assumed that C# and C++ developers would always use the built-in system, and that the built-in system would be sufficient for all cases.

In short, your proposed change would actually cause widespread breakage (as far as I can tell). Sorry about that!

sotanakamura commented 1 year ago

Thank you for providing a lot of information. Actually, I want to create WinUI 3 apps without XAML and use IXamlMetadataProvider only for instantiating XamlControlsResources. If we can instantiate XamlControlsResources without XAML integration, it would be nice.

DarranRowe commented 1 year ago

While I understand what you are going for here, I think the better option would be for Microsoft to document how it works and provide a sample. Interestingly, the Xaml Islands application that you referenced is close to what you would need for this to work normally. Here is a repository that shows this at work. This should be extensible to a certain extent, where if you have a custom control that also provides a metadata provider then these controls can also be used.

sotanakamura commented 1 year ago

Thank you for providing the implementation. I updated the issue info with your code.

sotanakamura commented 1 year ago

I updated the sample code with Kenny Kerr's implementation.