Xamarin 在派生类中设置绑定属性不会触发在其派生的类中更改的属性
我创建了这个类:Xamarin 在派生类中设置绑定属性不会触发在其派生的类中更改的属性,xamarin,xamarin.forms,Xamarin,Xamarin.forms,我创建了这个类: public partial class ScrollHeadingView : ContentPage { Xamarin.Forms.ScrollView scrollView; public static readonly BindableProperty IsScrollableProperty = BindableProperty.Create(nameof(IsScrollable), typeof(bool),
public partial class ScrollHeadingView : ContentPage
{
Xamarin.Forms.ScrollView scrollView;
public static readonly BindableProperty IsScrollableProperty =
BindableProperty.Create(nameof(IsScrollable), typeof(bool),
typeof(ScrollHeadingView), false, propertyChanged: OnIsScrollablePropertyChanged);
public bool IsScrollable
{
get { return (bool)GetValue(IsScrollableProperty); }
set { SetValue(IsScrollableProperty, value); }
}
private static void OnIsScrollablePropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
var view = (ScrollHeadingView)bindable;
if ((bool)newValue)
{
view.scrollView.Orientation = ScrollOrientation.Vertical;
view.scrollView.VerticalScrollBarVisibility = ScrollBarVisibility.Always;
}
else
{
view.scrollView.Orientation = ScrollOrientation.Neither;
view.scrollView.VerticalScrollBarVisibility = ScrollBarVisibility.Never;
}
}
当我使用这样的类时,不会调用OnIsScrollablePropertyChanged。有人知道这是为什么吗
<?xml version="1.0" encoding="UTF-8" ?>
<t:ScrollHeadingView
x:Class="Test.DecksTabPage"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
IsScrollable = "False" >
public partial class DecksTabPage : ScrollHeadingView
{
public DecksTabViewModel vm;
public DecksTabPage()
{
BindingContext = vm = new DecksTabViewModel();
InitializeComponent();
}
protected override void OnAppearing()
{
base.OnAppearing();
vm.OnAppearing();
}
}
如果将
IsScrollable
属性设置为false
,则不会调用OnIsScrollablePropertyChanged
,因为属性的默认值为false
,因此没有更改。为了证明这一点,您可以尝试将属性设置为true
,您应该看到断点现在已命中
这里最好的方法是为false
条件设置默认值。类似于构造函数中的内容:
public ScrollHeadingView()
{
view.scrollView.Orientation = ScrollOrientation.Neither;
view.scrollView.VerticalScrollBarVisibility = ScrollBarVisibility.Never;
}
您可以在xaml中发布代码,以便我们检查问题。只是为了再次检查,您是否尝试过设置
IsScrollable=“True”
?你的断点命中了吗?我仍在调查,但似乎有可能。我只是想解决一些其他的问题,然后我会回去详细研究。谢谢
public ScrollHeadingView()
{
view.scrollView.Orientation = ScrollOrientation.Neither;
view.scrollView.VerticalScrollBarVisibility = ScrollBarVisibility.Never;
}