对象未在ViewModel类中设置,控件直接从数据访问类移动到View

本文关键字:数据 访问 移动 View 控件 ViewModel 设置 对象 | 更新日期: 2023-09-27 17:58:32

我想从AboutUs表中获取第一条记录并显示为内容标签。

我已经在MVVM模式中创建了4个类。

第一个是模型类AboutUs.cs

[Table("tblAboutUs")]
    public class AboutUs
    {
        [PrimaryKey, AutoIncrement, NotNull]
        public int IDP { get; set; }
        public string Content { get; set; }
    }

其次是数据访问类

SQLiteAboutUs.s

public class SQLiteAboutUs
    {
        private static readonly AsyncLock Mutex = new AsyncLock();
        private SQLiteAsyncConnection dbConn;
        public int StatusCode { get; set; }
        public SQLiteAboutUs(ISQLitePlatform sqlitePlatform, string dbPath)
        {
            if (dbConn == null)
            {
                var connectionFunc = new Func<SQLiteConnectionWithLock>(() =>
                    new SQLiteConnectionWithLock
                    (
                        sqlitePlatform,
                        new SQLiteConnectionString(dbPath, storeDateTimeAsTicks: false)
                    ));
                dbConn = new SQLiteAsyncConnection(connectionFunc);
                dbConn.CreateTableAsync<Model.AboutUs>();
            }
        }
        public SQLiteAboutUs()
        {
        }
        public async Task Save(Model.AboutUs content)
        {
            using (await Mutex.LockAsync().ConfigureAwait(false))
            {
                StatusCode = 0;

            await dbConn.InsertAsync(new Model.AboutUs { Content = content.Content });
            StatusCode = 1;
        }
        //For Get first Row from Table
        public async Task<Model.AboutUs> GetAllData()
        {
            return await dbConn.Table<Model.AboutUs>().Where(x => x.IDP == 1).FirstOrDefaultAsync();
        }
    }

三级ViewModel类

AboutUsViewModel.cs

public class AboutUsViewModel
    {
        readonly SQLiteAboutUs _db;
        public string AboutUsContent { get; set; }
        //public string AboutUs;  
        public AboutUsViewModel()
        {
            _db = new SQLiteAboutUs();
        }
        public async void FirstRecord()
        {
            Model.AboutUs obj = await _db.GetAllData();
            this.AboutUsContent = obj.Content;
        }
    }

第四个是我的xaml页面的代码隐藏文件

AboutUs.xaml.cs

public partial class AboutUs : ContentPage
    {
        readonly AboutUsViewModel aboutUsViewModel;
        public AboutUs()
        {
            InitializeComponent();
            aboutUsViewModel = new AboutUsViewModel();
            aboutUsViewModel.FirstRecord();
            lblContent.Text = aboutUsViewModel.AboutUsContent;
        }
    }

我有调试代码,但问题是在FirstRecord方法对象中的AboutUsViewModel.cs类中无法设置,这就是为什么也没有设置AboutUsContent字符串属性的原因。

我不明白为什么我的调试器直接从SQLiteAboutUs.cs中的GetAllData()方法跳到视图的代码隐藏文件中的label.text

对象未在ViewModel类中设置,控件直接从数据访问类移动到View

欢迎来到异步性的奇妙世界。我鼓励你仔细阅读等待是如何工作的:如何以及何时使用"async"answers"await"

这不是阻塞呼叫。这样就创建了AboutUs视图。它创建ViewModel AboutUsViewModel。它称

aboutUsViewModel.FirstRecord();

但不要等待调用完成(不要忘记您将FirstRecord函数标记为async…)

所以它称

Model.AboutUs obj = await _db.GetAllData();

并且由于等待运算符的原因,直接返回给调用者。这就是为什么它直接跳到

lblContent.Text = aboutUsViewModel.AboutUsContent;

你想要的是类似的东西

await aboutUsViewModel.FirstRecord();

等待呼叫完成后再转到下一行。但你当然不能这样做,因为你在构造函数中,不可能有异步构造函数。无论如何,在构造函数中调用数据库(或者实际上任何可能失败的数据库)都是一种糟糕的做法。

我建议您在构造函数中只让InitializeComponent(),然后使用类似于视图的OnDataLoaded()事件的东西来执行带有等待的异步调用。

希望能有所帮助。