在SQLite中存储对象列表

时间:2017-05-28 20:11:31

标签: c# sql xamarin sqlite xamarin.forms

在这里学习SQLite ..

在我的应用程序中,我有以下Team类。 Bill,User和Payment是其他对象类。 大多数东西在应用程序中工作,但我无法检索用户,帐单或付款列表。没有抛出异常,但ListViews显示为空白。我这样做了吗?请注意,主页上显示的团队列表没有问题。

public class Team
{
    [PrimaryKey, AutoIncrement]
    public int TeamId { get; set; }
    public string Name { get; set; }
    public List<User> Members = new List<User>();
    public List<Bill> Bills = new List<Bill>();
    public List<Payment> Payments = new List<Payment>();

    public Club()
    {         
    }
    public Club(string name)
    {
        Name = name;
    }
}

要显示的代码在这里:

    private Team _team;
    private SQLiteAsyncConnection _connection;


    public TeamPage(Team team)
    {

        InitializeComponent();
        _team = team;
        try
        {
            Title = _team.Name;
            TeamBills.ItemsSource = _team.Bills;
            TeamPayments.ItemsSource = _team.Payments;
            UserList.ItemsSource = _team.Members.ToList();
            teamBills.HeightRequest = team.Bills.Count * 50;
            teamPayments.HeightRequest = team.Payments.Count * 50;
        }
        catch (Exception)
        {
            DisplayAlert("Error", "something happnened in list displays","ok");
        }

        _connection = DependencyService.Get<ISQLiteDB>().GetConnection();
    }

传递团队对象的主页代码:

async void MyTeams_ItemTapped(object sender, ItemTappedEventArgs e)
    {
        Team selectedTeam;

        if (e.Item as Team == null)
            await DisplayAlert("Team error", "team selected is null!?", "ok");
        else
        {
           selectedTeam = e.Item as Team;
           await Navigation.PushAsync(new TeamPage(selectedTeam));
           ((ListView)sender).SelectedItem = null;
        }
    }

添加_team(这是来自不同的页面):

async void Finished_Clicked(object sender, EventArgs e)
    {
        _newTeam.Members.AddRange(NewMembers);
        _connection = DependencyService.Get<ISQLiteDB>().GetConnection();
        await _connection.InsertAsync(_newTeam);
        MessagingCenter.Send(this, "NewTeamAdded", _newTeam);
        await Navigation.PushAsync(new MyTeamPage());
    }

0 个答案:

没有答案