XAML .Net Maui无法通过绑定到附加属性获取值

yhived7q  于 2022-12-25  发布在  .NET
关注(0)|答案(1)|浏览(239)

在.net Maui 7中,我一直在尝试将自定义可绑定属性绑定到附加属性值。我尝试了各种方法将附加属性值绑定到自定义可绑定属性值。在下面的xaml代码中,您将看到从Border继承的自定义视图和创建的新可绑定属性。

<views:CustomView
                    Margin="0,0,0,20" 
                    attached:AttachedProperty.Animate="False" 
                    ShouldAnimateOut="{Binding (attached:AttachedProperty.Animate), Source=                                     {RelativeSource Mode=Self}}"
                    StrokeThickness="0" 
                    x:Name="myCustomView" 
                    Padding="10,10" 
                    VerticalOptions="End" 
                    Background="#345173" 
                    HeightRequest="200">


                <views:CustomView.GestureRecognizers>
                    <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped"/>
                </views:CustomView.GestureRecognizers>

 </views:CustomView>`

UserCredentialsView的隐藏代码:

public class UserCredentialsView : Border
{
        public static readonly BindableProperty ShouldAnimateOutProperty =
                               BindableProperty.Create(nameof(ShouldAnimateOut), typeof(bool),  typeof(UserCredentialsView), false, BindingMode.TwoWay, propertyChanged: ShouldAnimateOutPropertyChanged);

        private static void ShouldAnimateOutPropertyChanged(BindableObject bindable, object oldValue, object newValue)
        {
            AttachedProperty.SetAnimate(bindable, false);
        }

        public bool ShouldAnimateOut
        {
            get => (bool)GetValue(ShouldAnimateOutProperty);
            set => SetValue(ShouldAnimateOutProperty, value);
        }
}

主页的代码隐藏:

public partial class MainPage : ContentPage
{

    public MainPage (SomeViewModel viewModel)
    {
        InitializeComponent();

        BindingContext = viewModel;

    }

    private void TapGestureRecognizer_Tapped(object sender, TappedEventArgs e)
    {
        var mControl = sender as Border;
        
        AttachedProperty.SetAnimate(mControl, !AttachedProperty.GetAnimate(mControl));
    }
}

附加属性定义:

public static readonly BindableProperty AnimateProperty = 
        BindableProperty.CreateAttached("Animate", typeof(bool), typeof(AttachedProperty), false, 
            BindingMode.TwoWay, propertyChanged: HandleChanged);

    private static void HandleChanged(BindableObject bindable, object oldValue, object newValue)
    {
        var mObject = bindable as UserCredentialsView;

        if (mObject is null)
            return;

        if ((bool)oldValue == (bool)newValue)
            return;

        if ((bool)newValue)
            DoSomething();

        else
            DoSomethingElse();
    }

    public static bool GetAnimate(BindableObject view) =>  (bool)view.GetValue(AnimateProperty);

    public static void SetAnimate(BindableObject view, bool value) => view.SetValue(AnimateProperty, value);

到目前为止我已经尝试了不同的“绑定”方式。
如:1)

ShouldAnimateOut="{Binding Path=(attached:AttachedProperty.Animate),Source= {RelativeSource Mode=Self}}"
ShouldAnimateOut="{Binding Path=(attached:AttachedProperty.Animate),Source= {x:Reference myCustomView}}"

绑定属性(ShouldAnimateOut)绑定到附加属性(Animate)的值。对附加属性的任何更改都会反映到ShouldAnimateOut属性。当属性发生更改时,会触发绑定属性的属性更改事件。但什么也不会发生。
我已尝试将ShouldAnimateOut绑定到IsEnabled属性。

ShouldAnimateOut="{Binding Path=IsEnabled, Source= {RelativeSource Mode=Self}}"
IsEnabled = "False"

如果我更改了IsEnabled属性,ShouldAnimateOut属性属性将触发更改事件。这就是我希望发生的。但是我无法从附加属性中获取值。
此外,我已经查看了Microsot .net maui文档的进一步细节,但没有运气。非常基本的信息,他们在他们的文档。
进一步检查:我还在github中查找了附加属性绑定可能存在的bug,但没有找到。
任何帮助都很感激。提前感谢...

ncgqoxb0

ncgqoxb01#

到目前为止,我们还不能绑定到Attached Properties,因为自定义控件中没有属性支持Attached Properties。Binding to Attached Properties in Xamarin Forms.
我也在Github Support binding to attached BP上发现了这个问题,你可以跟踪它。

相关问题