Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Docs] Update DynamicBindExtension Code in 5.4 Blog Post and Documentation #18493

Open
agneszitte opened this issue Oct 16, 2024 · 0 comments
Open
Assignees
Labels
difficulty/tbd Categorizes an issue for which the difficulty level needs to be defined. kind/documentation triage/untriaged Indicates an issue requires triaging or verification

Comments

@agneszitte
Copy link
Contributor

agneszitte commented Oct 16, 2024

On which pages?

What's wrong?

As per discussions with @Xiaoy312 and @jeromelaban, we need to update the DynamicBindExtension example code in the 5.4 blog post and related documentation to align with current WinUI specifications. The current implementation needs adjustments to avoid compilation errors.

Current Behavior:

The current example shows the following implementation, which results in XAML compilation errors:

public class DynamicBindExtension : MarkupExtension
{
    public DynamicBindExtension(string propertyName)
    {
        _propertyName = propertyName;
    }
    string _propertyName;

    public override object ProvideValue(IXamlServiceProvider serviceProvider)
    {
        var root =
((IRootObjectProvider)serviceProvider.GetService(typeof(IRootObjectProvider))).RootObject;
        var info = root.GetType().GetProperty(_propertyName);
        return info.GetValue(root);
    }
}

<TextBlock Text="{local:DynamicBind Tag}" />

Errors Encountered:
WinUI throws the following error:
XamlCompiler error WMC0100: XAML DynamicBindExtension type cannot be constructed.

Additionally, if the default constructor issue is resolved, WinUI will still raise:
XamlCompiler error WMC0615: Type 'local' used after '{' must be a Markup Extension. Error code 0x09d7.

This is because positional arguments (as in {local:DynamicBind Tag}) are not supported.

Proposed Solution:

The code should be updated to the following implementation to support property-based binding:

Updated C# Code:

public class DynamicBindExtension : MarkupExtension
{
    public string? PropertyName { get; set; } // Allow property-based binding

    public DynamicBindExtension() {} // Add default constructor

    public override object ProvideValue(IXamlServiceProvider serviceProvider)
    {
        var root = ((IRootObjectProvider)serviceProvider.GetService(typeof(IRootObjectProvider))).RootObject;
        var info = root.GetType().GetProperty(PropertyName);
        return info?.GetValue(root);
    }
}

Updated XAML Usage:
<TextBlock Text="{local:DynamicBind PropertyName=Tag}" />

Next Steps:

The 5.4 blog post example will need to be also updated accordingly. (cc @mtmattei for info)

References:

@agneszitte agneszitte added triage/untriaged Indicates an issue requires triaging or verification difficulty/tbd Categorizes an issue for which the difficulty level needs to be defined. kind/documentation labels Oct 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
difficulty/tbd Categorizes an issue for which the difficulty level needs to be defined. kind/documentation triage/untriaged Indicates an issue requires triaging or verification
Projects
None yet
Development

No branches or pull requests

2 participants