【问题标题】:Cannot display data in ListView Xamarin Forms无法在 ListView Xamarin 表单中显示数据
【发布时间】:2021-05-22 10:05:54
【问题描述】:

我试图在我的 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>

我错过了什么??

提前致谢!

【问题讨论】:

  • 此问题可能由布局引起。要确认它,您可以将 ListView 设置为 ContentPage 的唯一元素(删除其他控件)。
  • 我建议将您的布局减少为仅带有 TextCell 的 ListView。让它发挥作用,然后逐渐增加复杂性,直到达到所需的布局。

标签: c# listview xamarin.forms gridview


【解决方案1】:

乍一看,您的 viewmodel 没有实现 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 变化。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-10
    • 2021-05-14
    • 2018-09-14
    • 1970-01-01
    • 2020-10-28
    • 2021-09-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多