dotnet / dotnet/wpf

API Request: Allow developers to inject a XAML factory for creating objects

Open
#4,022 4 comments 8 reactions 0 assignees View on GitHub
API suggestion Feature Request
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

As we know, the WPF will call the `XamlTypeInvoker.CreateInstance` to create the object in the XAML engine.

https://github.com/dotnet/wpf/blob/bbd9e44d87b4edd8880f4c09c6db36a2f3e23ba8/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Schema/XamlTypeInvoker.cs#L133-L145

All the type which we define will use reflection to create. I do not think this is a high-performance behavior.

Should we add XAML object creation factory that allow developers to inject a XAML factory for creating objects?

```csharp
// Before the XAML engine starts, add initialization logic
XamlObjectCreationFactory.RegisterCreator(() => new MainWindow());
```

In the future, we can use [SourceGenerator](https://devblogs.microsoft.com/dotnet/introducing-c-source-generators/) to automate the writing of this part of the code

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.