Async Init and property changed to MvvmCross

I have asynchronous calls from the Init model in a view. The problem is that sometimes the asynchronous call returns to OnCreate, and the property in the user interface is not updated. Is there a suitable asynchronous / pending model for this case when we need to initialize asynchronous data?

pseudo code:

    // ViewModel
    public async Task Init(string id)
    {
        Url = await LoadUrlAsync(id);
    }


    // View
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        SetContentView(Resource.Layout.ui_xml);
        ViewModel.PropertyChanged += ViewModel_PropertyChanged;
    }

    void ViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        _webView.LoadUrl(ViewModel.Url);
    }
+4
source share
1 answer

I would probably do something similar in the method OnCreate, since you could add additional properties to it in the future.

private bool _loaded;

protected override void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);
    SetContentView(Resource.Layout.ui_xml);
    ViewModel.WeakSubscribe(() => ViewModel.Url, (s,e) => 
    {
        if (!_loaded)
            _webView.LoadUrl(ViewModel.Url);
    });

    if (ViewModel.Url != null) //Check if the async Init has finished already
    {
        _webView.LoadUrl(ViewModel.Url);
        _loaded = true;
    }
}
+2
source

All Articles