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



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

<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}}"
    

    好吧,Bindable属性(ShouldAnimateOut)绑定到附加属性(Animate)值。动画附加属性的任何变化都会反映到ShouldAnimateOut属性,当属性发生变化时应该触发Bindable属性的property changed事件。但是什么也没发生。

    我已经尝试绑定ShouldAnimateOut到IsEnabled属性。

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

    如果我改变IsEnabled属性ShouldAnimateOut属性属性改变事件触发。这就是我想要的。但是我无法从附加属性中获取值。

    此外,我已经查看了microsot.net maui文档以获取更多细节,但没有运气。他们的文档中有非常基本的信息。

    进一步检查:我还在github上查找附加属性绑定的可能错误,但我找不到任何。

    任何帮助,非常感谢。提前感谢……

    到目前为止,我们无法绑定到一个附加属性,因为在自定义控件中没有属性来备份附加属性。这里是类似的问题在StackOverflow:绑定到附加属性在Xamarin表单。

    我还发现了Github支持绑定到附加BP的问题,您可以遵循它。

    供您参考,在本例中我可以绑定到附加属性。它显示了错误" error XLS0415可附加属性'MyVisualState'在类型中找不到"在VS错误窗格中,但仍然编译。https://github.com/dotnet/maui/issues/15464

    相关内容

    • 没有找到相关文章

    最新更新