microsoft / xaml-designer-extensibility

Extensibility sample code for the Visual Studio XAML Designer
MIT License
67 stars 29 forks source link

Visual Studio design-time APIs don't allow setting ModelProperty with the 'System.Type' property type #31

Open miniBivlidi opened 4 years ago

miniBivlidi commented 4 years ago

Environment: Microsoft Visual Studio Professional 2019 Preview Version 16.8.0 Preview 3.1

Issue: Since information about runtime types is not available at design time, it's impossible to set a model property with the 'System.Type' type.

I tried setting a property value to TypeIdentifier:

var myTypeIdentifier = new TypeIdentifier("CustomControlLibrary.WpfCore.MyEnum");
ModelItem.Properties["MyType"].SetValue(myTypeIdentifier);

I also tried to set it to TypeDefinition:

var myTypeIdentifier = new TypeIdentifier("CustomControlLibrary.WpfCore.MyEnum");
var myTypeDefinition = MetadataFactory.ResolveType(ModelItem.Context, myTypeIdentifier );
ModelItem.Properties["MyType"].SetValue(myTypeDefinition);

However, none of these attempts was successful.

When I create TypeExtension using ModelFactory and set the TypeName property, the markup extension appears in XAML, but with a wrong type name. It doesn't contain an xmlns prefix:

var myTypeIdentifier = new TypeIdentifier("CustomControlLibrary.WpfCore.MyEnum");
var myTypeDefinition = MetadataFactory.ResolveType(ModelItem.Context, myTypeIdentifier );
var typeExtensionItem = ModelFactory.CreateItem(ModelItem.Context, new TypeIdentifier("System.Windows.Markup.TypeExtension"));
typeExtensionItem.Properties["TypeName"].SetValue(myTypeDefinition.FullName);
ModelItem.Properties["MyType"].SetValue(typeExtensionItem);

Generated XAML:

<lib:MyControl MyType="{x:Type TypeName=CustomControlLibrary.WpfCore.MyEnum}" >

It seems that design time has no public APIs to get an xmlns type prefix or add it to the root element.

Thanks.