获取ListView的内容

时间:2015-03-28 02:05:04

标签: listview xamarin.forms

我设置了Xamarin Forms ListView

public class AddressView : ContentPage
{
    public AddressView()
    {
        this.Title = "Native address book";
        CreateView();
    }

    async void CreateView()
    {
        IAddress addresses = DependencyService.Get<IAddress>();
        var addressList = addresses.ContactDetails();
        if (addressList.Count == 0)
        {
            await DisplayAlert("No contacts", "Your phone has no contacts stored on it", "OK");
            return;
        }

        if (Device.OS != TargetPlatform.iOS)
            BackgroundColor = Color.White;
        else
            Padding = new Thickness(0, 20, 0, 0);

        var myList = new ListView()
        {
            ItemsSource = addressList,
            ItemTemplate = new DataTemplate(typeof(MyLayout))
        };
        myList.ItemSelected += MyList_ItemSelected;

        Content = myList;
    }

    void MyList_ItemSelected (object sender, SelectedItemChangedEventArgs e)
    {
        var item = e.SelectedItem as ViewCell;
    }
}

public class MyLayout : ViewCell
{
    public MyLayout()
    {
        var label = new Label()
        {
            Text = "name",
            Font = Font.SystemFontOfSize(NamedSize.Default),
            TextColor = Color.Blue
        };

        var numberLabel = new Label()
        {
            Text = "number",
            Font = Font.SystemFontOfSize(NamedSize.Small),
            TextColor = Color.Black
        };

        this.BindingContextChanged += (object sender, EventArgs e) =>
        {
            var item = (KeyValuePair<string,string>)BindingContext;
            label.SetBinding(Label.TextProperty, new Binding("Key"));
            numberLabel.SetBinding(Label.TextProperty, new Binding("Value"));
        };

        View = new StackLayout()
        {
            Orientation = StackOrientation.Vertical,
            VerticalOptions = LayoutOptions.StartAndExpand,
            Padding = new Thickness(12, 8),
            Children = { label, numberLabel }
        };
    }
}

这从本机平台抓取地址簿(并且工作正常)。我想要做的是从DataTemplate中的ViewCell中的标签中读取两个Text属性。

有没有办法迭代ViewCell中的子节点来查找单元格内标签的值?

1 个答案:

答案 0 :(得分:0)

我认为问题出在MyList_ItemSelected事件处理程序中,因为您正在传递单元格的绑定上下文(在本例中,e.SelectedItem是字典中的KeyValuePair),而不是单元格本身? / p>

我无法直接在事件处理程序中找到引用Cell的方法。

但是,通过将Dictionary的值更改为类而不是简单字符串,可以向Cell添加WeakReference:

public class Address
{
    public string Text {get;set;}
    public WeakReference<MyLayout> Layout { get;set;}

    public Address (string text)
    {
        Text = text;
    }
}

public interface IAddress
{
    Dictionary<string,Address> ContactDetails ();
}

public MyLayout()
{
    // ... Removed for brevity

    this.BindingContextChanged += (object sender, EventArgs e) =>
    {
        var item = (KeyValuePair<string,Address>)BindingContext;
        label.SetBinding(Label.TextProperty, new Binding("Key"));
        numberLabel.SetBinding(Label.TextProperty, new Binding("Value.Text"));
        item.Value.Layout = new WeakReference<MyLayout>(this);
    };

    // ... Removed for brevity
}

因此,现在事件处理程序中的值将引用ViewCell。请注意,我使用WeakReference来避免强引用周期。

void MyList_ItemSelected (object sender, SelectedItemChangedEventArgs e)
{
    var kvp = (KeyValuePair<string,Address>)e.SelectedItem;
    var item = kvp.Value;
    MyLayout cell;
    item.Layout.TryGetTarget (out cell);

    // Now that you have a reference to the ViewCell, you can access the 
    // View properties.
}