【问题标题】:UWP DependencyObject Binding Windows.UI.Xaml.Markup.XamlParseExceptionUWP DependencyObject 绑定 Windows.UI.Xaml.Markup.XamlParseException
【发布时间】:2022-01-09 09:48:45
【问题描述】:

我有一个带有 XAML 页面的 Visual Studio 2019 UWP 项目,该页面使用依赖属性来绑定值。 在调试模式下一切正常,但在发布模式下却不行。我绑定VS relase 不喜欢的方式有什么问题?

下面的示例代码显示了一个绑定到 MyDependencyClass DependencyObject 类的 Windows.UI.Xaml.Controls.FontIcon。

<FontIcon FontFamily="{ThemeResource SymbolThemeFontFamily}" Glyph="{Binding ElementName=MyPageUI, Path=(local:myDependencyClass.myGlyph)}" />

错误是 Windows.UI.Xaml.Markup.XamlParseException: 'XAML parsing failed.',这是由于 FontIcon 元素中的此绑定所致。控件的类型无关紧要,同样的错误。

{Binding ElementName=MyPageUI, Path=(local:myDependencyClass.myGlyph)}

public abstract class MyDependencyClass : DependencyObject
{
    public static readonly DependencyProperty MyGlyphProperty;

    public static void SetMyGlyph(DependencyObject DepObject, string value)
    {
        DepObject.SetValue(MyGlyphProperty, value);
    }
    public static string GetMyGlyph(DependencyObject DepObject)
    {
        return (string)DepObject.GetValue(MyGlyphProperty);
    }

    static MyDependencyClass()
    {
        PropertyMetadata MyPropertyMetadata = new PropertyMetadata("\xE72E");
        MyGlyphProperty = DependencyProperty.RegisterAttached("MyGlyph",
                                                typeof(string),
                                                typeof(MyDependencyClass),
                                                MyPropertyMetadata);
}

【问题讨论】:

    标签: uwp winui dependencyobject


    【解决方案1】:

    在调试模式下一切正常,但在发布时却不行。

    我可以重现您的问题,并且 xaml 绑定代码有意义,请随时将您的问题发布到 Windows 反馈中心或在WinUI issue 框中发布。目前我们有一个解决方法,将Binding替换为x:Bind,并使用静态属性替换附加属性。

    Xaml

    <FontIcon FontFamily="{ThemeResource SymbolThemeFontFamily}" Glyph="{x:Bind local:TestClass.Glyph}" />
    

    代码

    public static class TestClass
    {
        public static string Glyph
        {
            get
            {
                return "\xE72E";
            }
        }
    
    }
    

    【讨论】:

    猜你喜欢
    • 2012-06-22
    • 1970-01-01
    • 2016-08-02
    • 2010-12-24
    • 2017-10-30
    • 1970-01-01
    • 2012-07-11
    • 1970-01-01
    • 2017-07-18
    相关资源
    最近更新 更多