Xamarin表单列表视图显示空白项目

时间:2018-02-08 06:52:22

标签: listview data-binding xamarin.forms

我试图在自定义列表视图上显示数据。但是,它显示的内容中没有任何内容。由于空白项仍然显示在列表视图中,所以我想问题出在sessionID和用户名绑定上,但我无法弄清问题在哪里。一些答案说绑定数据必须是属性而不是字段,但我已经完成了。

HistoryPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 x:Class="ble.net.sampleapp.view.HistoryPage"
                 Title="{Binding Title}">
        <AbsoluteLayout>
            <Button StyleClass="Large"
                    AbsoluteLayout.LayoutBounds="0.5,0.98,0.3, 0.1"   
                    AbsoluteLayout.LayoutFlags="XProportional,YProportional,WidthProportional, HeightProportional"  
                    Margin="10,5"
                    Text="Refresh"
                    Command="{Binding Refresh}"/>
            <ListView x:Name="HistoryListView"
                      ItemsSource="{Binding HistoryList}"
                      ItemSelected="onItemSelected"
                    SeparatorColor="#aaaaaa"
                    VerticalOptions="FillAndExpand"
                    HorizontalOptions="FillAndExpand">


                <ListView.SeparatorVisibility>
                    <OnPlatform x:TypeArguments="SeparatorVisibility"
                                Android="Default"
                                iOS="Default"
                                WinPhone="Default" />
                </ListView.SeparatorVisibility>
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <Frame Margin="{StaticResource GridPadding}"
                                 Padding="{StaticResource GridPadding}">

                                <Grid RowSpacing="5">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="*" />
                                        <ColumnDefinition Width="*" />
                                    </Grid.ColumnDefinitions>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="*" />
                                        <RowDefinition Height="*" />
                                    </Grid.RowDefinitions>

                                    <Label Grid.Row="0" Grid.Column="0"
                                       Text="{Binding sessionID}"
                                       FontAttributes="Bold" />
                                    <Label Grid.Row="0" Grid.Column="1"
                                       Text="{Binding username}"
                                       FontAttributes="Bold" />
                                </Grid>
                            </Frame>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </AbsoluteLayout>
    </ContentPage>

HistoryViewModel

private ObservableCollection<Features> historyList = new ObservableCollection<Features>();
public ObservableCollection<Features> HistoryList
{
    get { return historyList; }
    set
    {
        historyList = value;
        RaiseCurrentPropertyChanged();
    }
}

//query data from SQL
HistoryList = await sql.GetFeatureItemsAsync(common.getUsername());

Feature.cs

namespace ble.net.sampleapp.func
{
    public class Features
    {
        [JsonProperty(PropertyName = "id")]
        public string sessionID { get; set; }

        [JsonProperty(PropertyName = "deviceId")]
        public string deviceID { get; set; }

        [JsonProperty(PropertyName = "username")]
        public string username { get; set; }

        [JsonProperty(PropertyName = "startTime")]
        public DateTimeOffset startTime { get; set; }

        [JsonProperty(PropertyName = "endTime")]
        public DateTimeOffset endTime { get; set; }
    }
}

问题出在哪里?如何在列表视图中正确显示sessionIDusername

1 个答案:

答案 0 :(得分:0)

您可能必须在Features类中实现INotifyPropertyChanged(INPC)接口。

抽象地说:可观察类的每个元素都必须处理INPC本身,以通知有关单个列表视图单元的更新的视图。

试试这个:

.dll

和INotifyPropertyChanged包装器:(取自msdn网站上的某个地方)

public class Features : BindableBase
{
    private string _sessionID;
    public string sessionID
    {
        get { return _sessionID; }
        set { SetProperty(ref _sessionID, value); }
    }
}

所以最后,让每个应该更新视图的模型继承可绑定的base并在属性的setter中调用SetProperty(...)。