Open weitzhandler opened 5 years ago
Note: your code currently DOES work in CoreCLR (Debug) build, but DOES NOT work in .Net Native (Release) build. The AOT system cannot create a System.Type
from xaml.
I would like this feature, and wrote almost same code in my app. But it seems needs xaml enhancement.
.NET Native requires that you explicitly list the types you want reflection on in the .rd.xml file, but otherwise the feature should in AOT I think.
@weitzhandler I think this should be considered blocked now, until the xaml platform is open sourced, which is announced for future in WinUI 3 roadmap. And also, the announcement of .NET 5 contains a full-featured AOT solution, to get rid of differences between Debug and Release in UWP.
Currently I just check the type name, instead of the System.Type
object, as there are only 2 or 3 choises.
Another data point to include in this conversation (not meant to detract from the proposal, just add some extra info)... a while back I took a look at how DataTemplateSelector was being used in 1st party UI across the system and one thing that stood out was the different pattern used in C++ versus C# code. Managed code overwhelmingly would have a base type for the data and various derived types. Selecting which DataTemplate to use was based on the type information. Unmanaged code, however, would have only a few types and would select the template to use based on something like the value of a property.
Sadly this workaround even breaks for winui.
And, now that ResourceDictionary
is of Dict<object, object>
, why can't we put a general object as key, no matter it's Type
or other stuff?
The most annoying point of using Selector is that both DataTemplate
definition and consumption are bound to xaml. And there's no reason to select xaml keys which are compile-time static, depending on some value in code.
Proposal: Enable implicit
DataTemplate
in UWPIn WPF, you can add
DataTemplate
resources without a key, so at runtime, they get resolved according to theirDataType
property. Sadly, looks like this functionality isn't implemented in UWP. This feature has been proposed centuries ago. Maybe now that WinUI is on, it's about time to implement this. LikeDataTemplateSelector
, but per type.Scope
Meanwhile, here's my workaround:
Usage: