Xamarin表单:无法使用自定义控件填充列表视图

时间:2018-12-05 13:01:49

标签: xamarin.forms

我无法使用Xamarin表单中的自定义控件动态附加列表。请让我知道我在这里想念什么。

我可以为字符串属性创建槽,但不能为任何对象创建槽。

自定义控件内容视图:

<?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="MMSG.Mobile.CustomControls.DashboardItem">
<ContentView.Content>
    <StackLayout>

    <Label x:Name="displayText"></Label>

        <ListView x:Name="demoList">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell>
                                <StackLayout Orientation="Vertical" Margin="15,0,0,0">
                                    <Label Text="{Binding Name}"></Label>
                                </StackLayout>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
            </ListView>
    </StackLayout>


</ContentView.Content>

后面的自定义控制代码:

public partial class DashboardItem : ContentView
{
    public static readonly BindableProperty DisplayNameProperty = BindableProperty.Create(nameof(DisplayName), typeof(string), typeof(DashboardItem), default(string));
    public string DisplayName
    {
        get { return (string)GetValue(DisplayNameProperty); }
        set { SetValue(DisplayNameProperty, value); OnPropertyChanged(nameof(DisplayName)); }
    }

    public static readonly BindableProperty ListDisplayNameProperty = BindableProperty.Create(nameof(ListDisplayName), typeof(List<Demo>), typeof(DashboardItem));
    public List<Demo> ListDisplayName
    {
        get { return (List<Demo>)GetValue(ListDisplayNameProperty); }
        set { SetValue(ListDisplayNameProperty, value); OnPropertyChanged(nameof(ListDisplayName)); }
    }

    public DashboardItem()
    {
        InitializeComponent();
        displayText.SetBinding(Label.TextProperty, new Binding(nameof(DisplayName), source: this));
        demoList.SetBinding(ItemsView<Cell>.ItemsSourceProperty, new Binding(nameof(ListDisplayName), source: this));
    }
}

XAML页面,我在其中使用自定义控件:

<customControl:DashboardItem DisplayName="{Binding DashboardItm.Tile.Title}" ListDisplayName="{Binding Demos.Demos}">

ViewModel:

public class DashboardSummaryViewModel : BaseViewModel
{

    public DashboardItm DashboardItm { get; set; }

    public DemoList Demos { get; set; }

    public override void Init()
    {
        base.Init();
        //GetWalletSummary();
        DashboardItm = new DashboardItm();
        DashboardItm.Tile = new DashboardTile(){Title = "Sample Text"};

        Demos = new DemoList();
        Demos.Demos = new List<Demo>() { new Demo() { Name = "a" }, new Demo() { Name = "b" } };
    }
}

我能够在UI上显示DisplayName属性值。但是我无法使用customControl填充ListView。

请让我知道我在这里想念的东西。

0 个答案:

没有答案