【问题标题】:Xamarin Async List Cant DisplayXamarin 异步列表无法显示
【发布时间】:2017-11-13 23:16:14
【问题描述】:

我在类范围内提供了一些字符串。我在 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;

        }


    }
}

【问题讨论】:

    标签: c# visual-studio listview asynchronous xamarin


    【解决方案1】:

    很难说哪个是问题,因为我认为你的代码有很多问题

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

    2- 电子邮件、密码、用户名...应该是带有{get; set;} 的属性

    3- 你应该实现 INPC,也许是 Fody

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

    这里有一些帮助

    Data Binding

    【讨论】:

      猜你喜欢
      • 2020-03-14
      • 2019-02-10
      • 2019-01-08
      • 1970-01-01
      • 1970-01-01
      • 2017-08-19
      • 2021-05-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多