编程技术网

关注微信公众号,定时推送前沿、专业、深度的编程技术资料。

 找回密码
 立即注册

QQ登录

只需一步,快速开始

极客时间

x:load =“ {x:band blah,mode = oneway}”

作者: Alpha Privativ 2022-5-12 16:15:09 显示全部楼层 |阅读模式

x:Load="{x:Bind Blah, Mode=OneWay}"

Describe the bug

x:Bind works in control template since 1809. There is a bug however which prevents x:Load from being used with x:Bind. Note that x:Load="False" work, only x:Load={x:Bind Blah, Mode=OneWay} doesn't.

Steps to reproduce the bug
    Create a blank app, create the following CustomControlTemplate.xaml and CustomControlTemplate.cs (using user control template -> rename is probably the fastest)
<Style x:Key=&quot;DefaultCustomControlStyke&quot; TargetType=&quot;local:CustomControl&quot;> <Setter Property=&quot;Template&quot;> <Setter.Value> <ControlTemplate TargetType=&quot;local:CustomControl&quot;> <TextBlock x:Name=&quot;textBlock&quot; Text=&quot;{x:Bind Greeting, Mode=OneWay}&quot; x:Load=&quot;{x:Bind IsVisible, Mode=OneWay}&quot; /> </ControlTemplate> </Setter.Value> </Setter> // cs public sealed partial class CustomControlTemplate : ResourceDictionary { public CustomControlTemplate() { this.InitializeComponent(); } }">
// xaml
<ResourceDictionary
    x:
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:XLoadBindingNotWorkingInTemplate"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
    <Style TargetType="local:CustomControl" BasedOn="{StaticResource DefaultCustomControlStyke}"/>
    <Style x:Key="DefaultCustomControlStyke" TargetType="local:CustomControl">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="local:CustomControl">
                    <TextBlock x:Name="textBlock" Text="{x:Bind Greeting, Mode=OneWay}" x:Load="{x:Bind IsVisible, Mode=OneWay}" />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>
// cs
public sealed partial class CustomControlTemplate : ResourceDictionary
    {
        public CustomControlTemplate()
        {
            this.InitializeComponent();
        }
    }
    Add CustomControl that use the style
public sealed class CustomControl : Control, INotifyPropertyChanged
    {
        public CustomControl()
        {
            this.DefaultStyleKey = typeof(CustomControl);
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public bool IsVisible { get; private set; }

        public string Greeting { get { return "Hello world"; } }
        public void MakeVisible()
        {
            IsVisible = true;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsVisible)));
        }
    }
    Click run in VS. Observe doesn't compile with xaml compiler error. Navigate to obj and find CustomControlTemplate.g.cs, notice 2 compiler errors where obj2 is used without being declared
Expected behavior

x:Load="{x:Bind Blah, Mode=OneWay}" works.

Screenshots

image

NuGet package version

No response

Windows app type UWP Win32 Device form factor

没有 React

Windows version

没有 React

Additional context
    This is a WUX issue, but it is likely affecting WinUI 3 as well. Also reproducible with C++. The generated code should use dataRoot.GetTemplateChild instead of obj2.FindName. Minimal repro: https://github.com/roxk/XLoadBindingNotWorkingInTemplateCs
该问题来自:microsoft/microsoft-ui-xaml/issues/7086 , 试试查看该项目更多issue.

问题解答

ansorre 2022-5-12 16:48:14 显示全部楼层

As a workaround, write x:Load="False" and manually call GetTemplateChild(elementName).

腾讯云服务器 阿里云服务器
腾讯云服务器 腾讯云服务器
关注微信
^