1

我有一个带有 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);
}
4

1 回答 1

1

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

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

Xaml

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

代码

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

}
于 2021-12-03T06:49:48.197 回答