Xamarin异步列表无法显示

时间:2017-06-12 12:59:04

标签: c# visual-studio listview asynchronous xamarin

我在类范围内提供了一些字符串。我在Dis()方法中初始化它们。我从本地数据库中获取这些值的值,如下所示 - var ra = await App.Database.GetProfileAsync(email, password);

问题是,当我运行应用程序时,我无法看到列表中的列表项。没有这样的错误,但我认为我的应用程序在初始化字符串之前正在执行。

以下是整个活动代码(.cs) -

using LoginSystem.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using System.Collections.Generic;

namespace LoginSystem.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class UserProfile : ContentPage
    {
        string email;
        string password;
        string username;
        string gender;
        string address;
        string postal;
        string province;
        string country;


        public UserProfile(string e, string p)
        {
            email = e;
            password = p;
            Dis();
            //InitializeComponent();

            //this.BindingContext = new User_Profiles();

            var title = new Label
            {
                Text = "My Profile",
                FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label)),
                HorizontalOptions = LayoutOptions.CenterAndExpand,
            };

            List<string> Details = new List<string>
            {
                username,
                email,
                gender,
                address,
                postal,
                province,
                country
            };

            ListView articlesView = new ListView
            {
                ItemsSource = Details
            };

            Content = new StackLayout
            {
                BackgroundColor = Xamarin.Forms.Color.CadetBlue,
                Padding = 30,
                Spacing = 10,
                Children = { title,articlesView }
            };

        }

        async public void Dis()
        {
                var ra = await App.Database.GetProfileAsync(email, password);
                await DisplayAlert("Country", ra.Country + "!", "OK");
                username = ra.Username;
                email = ra.Username;
                gender = ra.Gender;
                address = ra.Address;
                postal = ra.Postal;
                province = ra.Province;
                country = ra.Country;

        }


    }
}

1 个答案:

答案 0 :(得分:0)

很难说哪个是问题,因为我认为您的代码中存在很多问题

1-你应该使用MVVM ...看看Introduction to MVVM

2- email,passowrd,username ...应该是{get; set;}

的属性

3-您应该实施INPC,可能使用Fody

4-您应该将ObservableCollection绑定到ListView,而不是List

这里有一些帮助

Data Binding