unoplatform / uno

Open-source platform for building cross-platform native Mobile, Web, Desktop and Embedded apps quickly. Create rich, C#/XAML, single-codebase apps from any IDE. Hot Reload included! 90m+ NuGet Downloads!!
https://platform.uno
Apache License 2.0
8.92k stars 723 forks source link

C# Markup WinAppSDK Performance Improvements #18665

Open mikernet opened 3 hours ago

mikernet commented 3 hours ago

What would you like to be added

Due to the way WinRT works, building up the UI programmatically is considerably less performant than when it is done with the XAML compiler. This is slightly problematic for C# markup running on WinAppSDK as currently written.

Luckily, there are high-performance XamlDirect APIs intended specifically to enable this scenario (that link has a lot of useful info in it, so have a look there), but C# Markup is currently not using them:

XamlDirect is purpose built for middleware that predominantly use imperative APIs to create UI instead of markup. With XamlDirect APIs, you can achieve performance parity with the XAML parser even when creating UI imperatively in code.

XamlDirect APIs can be used side-by-side with traditional APIs and take advantage of the pay for play performance improvements.

Ideally, C# markup would use XamlDirect to build the whole object graph from start to end, but as it is currently designed, that might be challenging to do everywhere (i.e. because you call new Button() ctors directly to create buttons, for example). That said, there are still many places where the benefits of XamlDirect could be utilized to improve performance. One example is in all the property setter methods:

#if !HAS_UNO
private static readonly IXamlDirect _xamlDirect = XamlDirect.GetDefault();
#endif

public static T Content<T>(this T element, object content) where T : ContentControl
{
#if HAS_UNO
    element.Content = content;
    return element;
#else
    var xdo = _xamlDirect.GetXamlDirectObject(element);
    _xamlDirect.SetObjectProperty(xdo, XamlPropertyIndex.ContentControl_Content, content);
#endif
}

Why is this needed

To bring C# Markup closer to parity with compiled XAML performance.

For which platform

Windows (WinAppSDK)

Anything else we need to know?

No response

mikernet commented 3 hours ago

I would be willing to do some of this work if desired, but I can't find where you have C# markup code. Is it private?