Xamarin异步ViewDidAppear在ViewDidLoad期间调用 [英] Xamarin Async ViewDidAppear called during ViewDidLoad

查看:84
本文介绍了Xamarin异步ViewDidAppear在ViewDidLoad期间调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在ViewDidLoad上初始化视图模型.我需要在ViewModel初始化代码中调用一些异步方法,因此我已将异步代码从构造函数中移到了

I'm trying to initialize a view model on ViewDidLoad. I need to call some async methods in the ViewModel initialization code, so I've moved the async code out of the constructor into an async factory method.

我已经在我的UIViewController子类中将ViewDidLoadViewWillAppear标记为async void,但是由于某些原因,在执行第4行时,ViewWillAppear开始了,第11行抛出了NullReferenceException,因为ViewModel尚未初始化.

I've marked the ViewDidLoad and ViewWillAppear as async void in my UIViewController subclass, but for some reason while line 4 is executing the ViewWillAppear is kicked off and line 11 throws a NullReferenceException because the ViewModel isn't initialized yet.

我怀疑Xamarin不能等待ViewDidLoad完成,因为它是async void,但是我不得不在这里使用async void,因为它覆盖了方法.

My suspicion is that Xamarin can't wait for ViewDidLoad to complete because it's async void, but I have to use an async void here because it's overriding a method.

MyCustomUiViewController.cs

MyCustomUiViewController.cs

1  public override async void ViewDidLoad()
2  {
3      base.ViewDidLoad();
4      ViewModel = await ViewModel.CreateAsync();
5      OtherStuff();
6  }
7 
8  public override async void ViewWillAppear(bool animated)
9  {
10     base.ViewWillAppear(animated);
11     ViewModel.SomeMethod(); // <-- NullReferenceException
12     AttachViewModelToViewBindings();
13 }

如果有更好的实例化异步ViewModel的模式,我愿意改变体系结构.

I'm open to changing the architecture if there is a better pattern for instantiating an async ViewModel.

推荐答案

这是我们使用的通用模式(提取为

Here's the generalized pattern that we used (extracted into this gist). This lets you create a controller that inherits from AsyncInitializationController and then overrides, for example, ViewDidLoadAsync. The code is structured so that each subsequent lifecycle method waits for the previous ones to complete.

虽然我们不需要异步ViewDidDisappear,但我敢肯定,您也可以将其应用于此模式.

While we didn't have a need for an async ViewDidDisappear, I'm sure you could work that into this pattern as well.

using System;
using System.Threading.Tasks;
using UIKit;

namespace Seanfisher.Gists
{
    public abstract class AsyncInitializationController : UIViewController
    {
        Task _viewDidLoadAsyncTask = Task.CompletedTask;
        public virtual Task ViewDidLoadAsync()
        {
            return _viewDidLoadAsyncTask;
        }

        public sealed override async void ViewDidLoad()
        {
            try
            {
                base.ViewDidLoad();
                _viewDidLoadAsyncTask = ViewDidLoadAsync();
                await _viewDidLoadAsyncTask;
            }
            catch (Exception e)
            {
                // Handle
            }
        }

        Task _viewWillAppearAsyncTask = Task.CompletedTask;
        public virtual Task ViewWillAppearAsync()
        {
            return _viewWillAppearAsyncTask;
        }

        public sealed override async void ViewWillAppear(bool animated)
        {
            try
            {
                await _viewDidLoadAsyncTask;
                base.ViewWillAppear(animated);
                _viewWillAppearAsyncTask = ViewWillAppearAsync();
                await _viewWillAppearAsyncTask;
            }
            catch (Exception e)
            {
                // Handle
            }
        }

        Task _viewDidAppearAsyncTask = Task.CompletedTask;
        public virtual Task ViewDidAppearAsync()
        {
            return _viewDidAppearAsyncTask;
        }
        public sealed override async void ViewDidAppear(bool animated)
        {
            try
            {
                await _viewDidLoadAsyncTask;
                await _viewWillAppearAsyncTask;

                base.ViewDidAppear(animated);
                _viewDidAppearAsyncTask = ViewDidAppearAsync();
                await _viewDidAppearAsyncTask;
            }
            catch (Exception e)
            {
                // Handle
            }
        }
    }
}

这篇关于Xamarin异步ViewDidAppear在ViewDidLoad期间调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆