我有一個 ContentPage,它參考了一個 ContentView。在這個內容視圖中,我有一個自定義的 Picker 控制元件,我系結了其他屬性,例如ItemsSource,ItemSelected但 ItemDisplayBinding 很痛苦。我可以在 Xamarin Forms Picker 類的代碼中看到,它不像其他的那樣是一個簡單的可系結屬性欄位。目前,當我單擊 Picker 時出現此錯誤。我知道特定的顯示系結屬性,因為選擇器無需我初始化就可以正常作業。
錯誤
Java.Lang.NullPointerException
Message=Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
內容頁
<controls:MyPickerView PickerTitle="Select time period"
PickerItemDisplayBinding="{Binding Display}"
PickerItemsSource="{Binding AppointmentRangeOptions}"/>
MyPickerView.xaml
<ContentView.Content>
<Frame >
<Picker Title = "{Binding PickerTitle}"
HorizontalOptions="FillAndExpand"
IsEnabled="{Binding PickerIsEnabled}"
ItemDisplayBinding="{Binding PickerItemDisplayBinding}"
SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
ItemsSource="{Binding PickerItemsSource, Mode=OneWay}"/>
</Frame>
</ContentView.Content>
MyPickerView.xaml.cs
public partial class MyPickerView : ContentView
{
public MyPickerView()
{
InitializeComponent();
Content.BindingContext = this;
}
public static readonly BindableProperty PickerIsEnabledProperty = BindableProperty.Create(
nameof(PickerIsEnabled),
typeof(bool),
typeof(MyPickerView),
null);
public bool PickerIsEnabled
{
get => (bool)GetValue(PickerIsEnabledProperty);
set => SetValue(PickerIsEnabledProperty, value);
}
public static readonly BindableProperty SelectedItemProperty = BindableProperty.Create(
nameof(SelectedItem),
typeof(object),
typeof(MyPickerView),
null);
public object SelectedItem
{
get => GetValue(SelectedItemProperty);
set => SetValue(SelectedItemProperty, value);
}
public static readonly BindableProperty PickerTitleProperty = BindableProperty.Create(
nameof(PickerTitle),
typeof(string),
typeof(MyPickerView),
null);
public string PickerTitle
{
get => (string)GetValue(PickerTitleProperty);
set => SetValue(PickerTitleProperty, value);
}
public static readonly BindableProperty PickerItemDisplayBindingProperty = BindableProperty.Create(
nameof(PickerItemDisplayBinding),
typeof(string),
typeof(MyPicker)
);
public string PickerItemDisplayBinding
{
get => (string)GetValue(PickerItemDisplayBindingProperty);
set => SetValue(PickerItemDisplayBindingProperty, value);
}
public static readonly BindableProperty PickerItemsSourceProperty = BindableProperty.Create(
nameof(PickerItemsSource),
typeof(IList),
typeof(MyPickerView));
public IList PickerItemsSource
{
get => (IList)GetValue(PickerItemsSourceProperty);
set => SetValue(PickerItemsSourceProperty, value);
}
}
uj5u.com熱心網友回復:
確實,這個屬性很痛苦。我用一個小技巧解決了它:
MyPickerView.xaml.cs
public BindingBase ItemDisplayBinding
{
get => MyPicker?.ItemDisplayBinding;
set => MyPicker.ItemDisplayBinding = value;
} // no need to define BindableProperty
MyPickerView.xaml
<ContentView.Content>
<Frame>
<Picker x:Name="MyPicker" />
</Frame>
</ContentView.Content>
內容頁
<controls:MyPickerView
PickerTitle="Select time period"
ItemDisplayBinding="{Binding Display}"
PickerItemsSource="{Binding AppointmentRangeOptions}"/>
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/456542.html
標籤:xml xamarin xamarin.forms xamarin.android xamarin.ios
上一篇:WinUI進度圈如何改變粗細?
