【发布时间】:2018-09-18 21:38:57
【问题描述】:
我正在尝试使用一些可绑定的属性在 xamarin 表单中创建自定义下拉菜单。我使用相对布局在标签下方有一个标签和一个列表视图,因此列表视图将始终位于标签下方,并且其 IsVisible 属性将被切换。
我创建了如下自定义视图:
Dropdown.xaml
<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="CustomViewXam.CustomViews.Dropdown">
<StackLayout>
<RelativeLayout>
<Label x:Name="selectedLabel" TextColor="Red" Text="xcx"
BackgroundColor="Silver" FontSize="15"
HeightRequest="50"
RelativeLayout.WidthConstraint =
"{ConstraintExpression
Type=RelativeToParent,
Property=Width,
Factor=0.5,
Constant=0}"/>
<ListView x:Name="listView" BackgroundColor="Black"
RelativeLayout.WidthConstraint =
"{ConstraintExpression
Type=RelativeToView,
ElementName=selectedLabel,
Property=Width,
Factor=1,
Constant=0}"
RelativeLayout.YConstraint=
"{ConstraintExpression
Type=RelativeToView,
ElementName=selectedLabel,
Property=Height,
Factor=1,
Constant=0}"
>
</ListView>
</RelativeLayout>
</StackLayout>
</ContentView>
Dropdown.xaml.cs
public partial class Dropdown : ContentView
{
public Dropdown()
{
InitializeComponent();
BindingContext = this;
}
public string TitleText
{
get { return base.GetValue(TitleTextProperty).ToString(); }
set { base.SetValue(TitleTextProperty, value); }
}
private static BindableProperty TitleTextProperty = BindableProperty.Create(
propertyName: "TitleText",
returnType: typeof(string),
declaringType: typeof(string),
defaultValue: "",
defaultBindingMode: BindingMode.TwoWay,
propertyChanged: TitleTextPropertyChanged);
private static void TitleTextPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
var control = (Dropdown)bindable;
control.selectedLabel.Text = newValue.ToString();
}
public static readonly BindableProperty ItemsSourceProperty =
BindableProperty.Create<Dropdown, IEnumerable<object>>(p => p.ItemsSource,
null, BindingMode.OneWay, null, (bindable, oldValue, newValue) => { ((Dropdown)bindable).LoadItems(newValue); });
public IEnumerable<object> ItemsSource
{
get { return (IEnumerable<object>)GetValue(ItemsSourceProperty); }
set { SetValue(ItemsSourceProperty, value); }
}
public void LoadItems(IEnumerable<object> tiles)
{
try
{
var list = tiles;
}
catch (Exception e)
{ // can throw exceptions if binding upon disposal
}
}
}
我在我的 xaml 页面中使用如下自定义视图
<local:Dropdown TitleText="dssdasd" ItemsSource="{Binding TitleList}" />
TitleList 是 ViewModel 中的 ObservableCollection
private ObservableCollection<string> _titleList;
public ObservableCollection<string> TitleList
{
get
{
return _titleList;
}
set
{
if (_titleList!= value)
{
_titleList= value;
NotifyPropertyChanged("TitleList");
}
}
}
问题:
文本在 UI 上可见并且文本正确显示,但下面的列表视图为空且数据未显示。自定义下拉列表中的 LoadItems 方法没有被调用,即使 TitleList 列表已更新。谁能指导我在上面的代码中做错了什么。
注意:我在我的视图中将 BindingContext 设置为我的视图模型。
【问题讨论】:
-
你应该创建一个 Picker 的自定义渲染器
标签: xamarin xamarin.forms xamarin.ios xamarin.android