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

Using a custom DependencyProperty extension in XAML causes XamlParseException on property assignment #9677

Open
Poyo-SSB opened this issue May 27, 2024 · 1 comment
Labels
bug Something isn't working team-Markup Issue for the Markup team

Comments

@Poyo-SSB
Copy link

Poyo-SSB commented May 27, 2024

Describe the bug

I'm extending NavigationView with a SettingsItemInfoBadge property (as WinUI does not natively provide this). Trying to use it in XAML causes a XamlParseException with the message "The text associated with this error code could not be found." and "Failed to assign to property 'Example.Utility.Extensions.NavigationViewExtensions.SettingsItemInfoBadge'."

Steps to reproduce the bug

NavigationViewExtensions.cs:

using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;

namespace Example.Utility.Extensions;

internal static class NavigationViewExtensions {
    public static readonly DependencyProperty SettingsItemInfoBadgeProperty = DependencyProperty.RegisterAttached("SettingsItemInfoBadge", typeof(InfoBadge), typeof(NavigationViewExtensions), new PropertyMetadata(new InfoBadge(), OnSettingsItemInfoBadgeChanged));

    public static void SetSettingsItemInfoBadge(UIElement element, InfoBadge value) {
        element.SetValue(SettingsItemInfoBadgeProperty, value);
    }

    public static InfoBadge GetSettingsItemInfoBadge(UIElement element) {
        return (InfoBadge)element.GetValue(SettingsItemInfoBadgeProperty);
    }

    private static void OnSettingsItemInfoBadgeChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args) {
        if (sender is not NavigationView navigationView) {
            return;
        }

        if (navigationView.SettingsItem is NavigationViewItem settingsItem) {
            settingsItem.InfoBadge = (InfoBadge)args.NewValue;
        }
    }
}

MainWindow.xaml:

<?xml version="1.0" encoding="utf-8"?>
<Window
    x:Class="Example.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:extensions="using:Example.Utility.Extensions"
    mc:Ignorable="d">

    <NavigationView IsSettingsVisible="True">
        <extensions:NavigationViewExtensions.SettingsItemInfoBadge>
            <InfoBadge/>
        </extensions:NavigationViewExtensions.SettingsItemInfoBadge>
    </NavigationView>
</Window>

Note: using SetSettingsItemInfoBadge() in the code-behind works perfectly fine. OnSettingsItemInfoBadgeChanged() can be empty and still yield the error. In XAML, if <extensions:NavigationViewExtensions.SettingsItemInfoBadge> is empty (i.e. no <InfoBadge> is provided), no error occurs.

Expected behavior

No error should occur or the error provided should be more descriptive.

NuGet package version

WinUI 3 - Windows App SDK 1.5.3: 1.5.240428000

Windows version

Windows 10 (22H2): Build 19045.4291

@Poyo-SSB Poyo-SSB added the bug Something isn't working label May 27, 2024
@microsoft-github-policy-service microsoft-github-policy-service bot added the needs-triage Issue needs to be triaged by the area owners label May 27, 2024
Copy link

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one. Thank you!

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

@Poyo-SSB Poyo-SSB changed the title Using a custom DependencyProperty extension causes XamlParseException on property assignment Using a custom DependencyProperty extension in XAML causes XamlParseException on property assignment May 27, 2024
@codendone codendone added team-Markup Issue for the Markup team and removed needs-triage Issue needs to be triaged by the area owners labels Jun 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working team-Markup Issue for the Markup team
Projects
None yet
Development

No branches or pull requests

2 participants