无法在 ListView Xamarin Forms 中显示数据

Cannot display data in ListView Xamarin Forms

我正在尝试在我的 XamarinApp 的 ListView 中显示数据,我的问题是我无法显示数据。我进行了所有绑定,我不认为我在绑定列表视图时犯了错误..

有人可以帮忙吗?

这是我的 UrlBindingModel 的样子:

 public class UrlBindingModel 
{
    public IList<Urls> UrlsList { get; set; }

    public UrlBindingModel() // In this constructor we add a few items
    {
        try
        {
            UrlsList = new List<Urls>
            {
                new Urls() { Name = "TestName", Url = "localhost" },
                new Urls() { Name = "TestName", Url = "localhost" },
                new Urls() { Name = "Test", Url = "Test" },
                new Urls() { Name = "Sas", Url = "Sas" }
            };
        }

        catch (Exception ex)
        {

        }

    }

    public class Urls
    {

        public string Name { get; set; }
        public string Url { get; set; }

    }


}

这是我的 Page.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"
             xmlns:models="clr-namespace:TestFormApp.Models;assembly=TestFormApp"
             xmlns:views="clr-namespace:TestFormApp.Views;assembly=TestFormApp"
             x:Class="TestFormApp.Pages.SettingsPage"
             Title="Settings">

    <ContentPage.BindingContext>
        <views:UrlBindingModel></views:UrlBindingModel>
    </ContentPage.BindingContext>

    <ContentPage.Content>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="40" />
                <RowDefinition Height="40" />
                <RowDefinition Height="40" />
                <RowDefinition Height="10" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <Label Text="Urls" Grid.Row="0" FontAttributes="Bold" FontSize="Large" HorizontalOptions="CenterAndExpand"></Label>
            <StackLayout Grid.Row="1">
                <Button x:Name="btnUrlPopUp" Text="Add Url" Clicked="BtnUrlPopUp_OnClicked"></Button>
            </StackLayout>
            <Grid Grid.Row="2" VerticalOptions="CenterAndExpand" Margin="10" Padding="10">
                <Grid.RowDefinitions>
                    <RowDefinition Height="30" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="3*" />
                    <ColumnDefinition Width="2*" />
                    <ColumnDefinition Width="2.5*" />
                    <ColumnDefinition Width="1*" />
                    <ColumnDefinition Width="1.5*" />
                </Grid.ColumnDefinitions>
                <Label Text="Name" Grid.Column="0" Grid.Row="0" Style="{DynamicResource headerTablet}" />
                <Label Text=" Host Url" Grid.Column="1" Grid.Row="0" Style="{DynamicResource headerTablet}" />
                <Label Text="Edit" Grid.Column="2" Grid.Row="0" Style="{DynamicResource headerTablet}" />
                <Label Text="Delete" Grid.Column="3" Grid.Row="0" Style="{DynamicResource headerTablet}" />
                <!--The box view creates a line under labels-->
                <BoxView Grid.Row="3" HorizontalOptions="FillAndExpand"
                         VerticalOptions="Center" HeightRequest="2" BackgroundColor="DarkGray"></BoxView>
                <ListView IsPullToRefreshEnabled="True" x:Name="lstUrlList" Grid.Row="4" ItemsSource="{Binding UrlsList}" Margin="4">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell>
                                <Grid VerticalOptions="CenterAndExpand" BackgroundColor="White" ColumnSpacing="0">
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="40" />
                                    </Grid.RowDefinitions>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="3*" />
                                        <ColumnDefinition Width="2*" />
                                        <ColumnDefinition Width="3*" />
                                        <ColumnDefinition Width="1*" />
                                        <ColumnDefinition Width="1*" />
                                    </Grid.ColumnDefinitions>

                                    <Label Text="{Binding Name, Mode=TwoWay}" Grid.Column="0" Grid.Row="0" Style="{DynamicResource detailTablet}" />

                                    <Label Text="{Binding Url, Mode=TwoWay}" Grid.Column="1" Grid.Row="0" Style="{DynamicResource detailTablet}" />

                                </Grid>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
            </Grid>
        </Grid>
    </ContentPage.Content>
</ContentPage>

我错过了什么??

提前致谢!

乍一看,您的视图模型没有实现 INotifyPropertyChanged,因此无法通知您的 xaml 任何 属性,包括 itemssource 的更改。

1 从 BindableObject 继承您的视图模型

public class UrlBindingModel : BindableObject

2 将道具更改为

        private IList<Urls> _urlsList;
        public IList<Urls> UrlsList
        {
            get { return _urlsList; }
            set
            {
                if (_IsProperty != value)
                {
                    _IsProperty = value;
                    OnPropertyChanged();
                }
            }
        }

请注意,任何 属性 将在运行时更改且不调用 OnPropertyChanged();当它发生变化时,不会将其变化通知给您的 UI.