【问题标题】:How to display data from the SQLite database in the Home `GridView` layout如何在 Home `GridView` 布局中显示来自 SQLite 数据库的数据
【发布时间】:2023-04-09 15:37:03
【问题描述】:

如何在 Home GridView 中显示来自 SQlite 数据库的数据,在 DisplayDetails() 方法中使用 ItemsSource 传递数据时,错误说 Grid 不包含 ItemsSource 的定义并且没有可访问的扩展方法,接受“网格”类型的参数。 请查找Home.xaml 网格布局和Home.xaml.cs 类了解更多详情

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="soccerapp.Home" BackgroundColor="White" Title="Home">
    <ContentPage.Content>
        <Grid x:Name="gridDisplay" Padding="10">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>

            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <!-- Use Grid.Column to specify column -->

            <Label Grid.Row="0" Grid.Column="0" Text="{Binding FullName}" BackgroundColor="#92f459"/>
            <Label x:Name="HomeLabel"  Text="Home Page is here" TextColor="White"
                VerticalOptions="CenterAndExpand" 
                HorizontalOptions="CenterAndExpand" FontSize="Small"></Label>
        </Grid>
    </ContentPage.Content>
</ContentPage>

下面是Home.xaml.cs

namespace soccerapp
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class Home : ContentPage
    {
        public SQLiteConnection conn = null;
        public PlayerDetails playermodel;
        public Home(string parameter1)
        {
            InitializeComponent();
            HomeLabel.Text = parameter1;
            conn = DependencyService.Get<Isqlite>().GetConnection();
            conn.CreateTable<PlayerDetails>();
            DisplayDetails();
        }

     public void DisplayDetails()
        {

            var details = (from x in conn.Table<PlayerDetails>() select x).ToList();
            gridDisplay.ItemsSource= details; 
        }


    }

我已经添加了PlayerDetails 模型类:

namespace soccerapp
{
    public class PlayerDetails
    {
        [PrimaryKey, AutoIncrement]
        public int id { get; set; }
        public string FullName { get; set; }
        public string Mobile { get; set; }
        public string SoccerPosition { get; set; }
        public string Email { get; set; }
        public string Password { get; set; }
        public string ConfirmPassword { get; set; }

    }


}

【问题讨论】:

标签: xamarin xamarin.forms


【解决方案1】:

您为什么要尝试将list&lt;&gt; 绑定到Grid?将gridDisplay.BindingContext 设置为模型应该可以工作。

public void DisplayDetails()
{

    var details = (from x in conn.Table<PlayerDetails>() select x).ToList();
    PlayerDetails playermodel = details[0];
    gridDisplay.BindingContext = playermodel;
}

更新:

模型中的代码:

public class PlayerDetails : INotifyPropertyChanged
{

    string fullname;

    public PlayerDetails( ) {

    }

    public string FullName
    {
        set
        {
            if (fullname != value)
            {
                fullname = value;

                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("FullName"));
                }
            }
        }
        get
        {
            return fullname;
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

ViewModels 一般实现 INotifyPropertyChanged 接口, 这意味着该类在任何时候触发一个 PropertyChanged 事件 其属性的变化。 Xamarin.Forms 中的数据绑定机制 将处理程序附加到此 PropertyChanged 事件,以便通知它 当属性更改并使用新的更新目标时 价值。

更新 2:

我检查了您的项目,发现问题出在您的xaml 代码中:

<ContentPage.Content>
    <Grid x:Name="gridDisplay" Padding="10">

        ...

    </Grid>
</ContentPage.Content>
<Label x:Name="HomeLabel"  Text="Home Page is here" TextColor="White"  Grid.Row="0" Grid.Column="1" 
            VerticalOptions="CenterAndExpand" 
            HorizontalOptions="CenterAndExpand" FontSize="Small"></Label>

您在ContentPage.Content 之外放置一个标签并设置VerticalOptions HorizontalOptions 等于CenterAndExpand,因此它将覆盖所有内容页面并且您在Grid 中看不到任何内容。

您设置了textcolor = white,这样您就不会在白色背景颜色下看到标签的文本。

因此,解决方案是删除此标签或将此标签移动到内容页面中。您可以将此标签放入 Grid 或其他地方。

【讨论】:

  • 是的,我会尽快回复您。
  • @JackHua...错误已经消失,但现在主屏幕上没有显示记录。
  • @soccerway 什么是记录?你只在你的 xaml 中绑定了一个 FullName 并且在你的 playermodel 中是 FullName
  • @soccerway 你可以看看这个关于 INotifyPropertyChanged 的​​document。当然,如果您有其他属性并正确设置绑定,它们将显示在屏幕上。我还更新了关于 INotifyPropertyChanged 的​​答案。
  • @soccerway 是系统接口,不用自己写。使用 System.ComponentModel。在我的回答中,我已经为你写了一个全名的模型。
猜你喜欢
  • 2013-02-22
  • 1970-01-01
  • 1970-01-01
  • 2015-09-20
  • 1970-01-01
  • 1970-01-01
  • 2011-08-03
  • 2020-11-07
  • 2017-09-20
相关资源
最近更新 更多