创建事件是使XF OnAppearing成为异步方法的有效方法吗? [英] Could creating an event be a valid way to make a XF OnAppearing into an async method?

查看:82
本文介绍了创建事件是使XF OnAppearing成为异步方法的有效方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近看到了有关如何将应用程序OnStart转换为异步OnStart的建议:

I recently saw this suggestion on how to make an app OnStart into an async OnStart:

    protected override void OnStart()
    {
        this.started += onStarted;      //Subscribe to event
        started(this, EventArgs.Empty); //Raise event
    }

    protected async void onStarted(object sender, EventArgs args)
    {
        try
        {
            await // do things
        }
        catch (Exception ex)
        {
            var ignore = ex;
        }
        this.started -= onStarted;
    }

任何人都可以看到与此相关的任何可能的问题,如果不能,那么可以将类似的内容应用于OnAppearing,如果需要的话,是否需要进行任何更改.

Can anyone see any possible issues with this and if not then could something similar be applied to the OnAppearing and if so would there be any changes needed.

推荐答案

OnAppearing只是Page类上的void方法

OnAppearing is simply a void method on Page class

Xamarin.Forms.Page

//...

protected virtual void OnAppearing()
{
}

//...

来源

这是Page生命周期的一部分.

that is called as part of the Page's life cycle.

//...

[EditorBrowsable(EditorBrowsableState.Never)]
public void SendAppearing()
{
    if (_hasAppeared)
        return;

    _hasAppeared = true;

    if (IsBusy)
    {
        if (IsPlatformEnabled)
            MessagingCenter.Send(this, BusySetSignalName, true);
        else
            _pendingActions.Add(() => MessagingCenter.Send(this, BusySetSignalName, true));
    }

    OnAppearing(); //<---
    Appearing?.Invoke(this, EventArgs.Empty); //NOTE HOW ACTUAL EVENT IS RAISED AFTER

    var pageContainer = this as IPageContainer<Page>;
    pageContainer?.CurrentPage?.SendAppearing();

    FindApplication(this)?.OnPageAppearing(this);
}

//...

来源

不要将它们误认为事件处理程序,这是允许使用async void的一个例外.

They are not to be mistaken for event handlers, that are the one exception allowed to use async void.

参考您在OnStart示例中显示的方法也可以应用于OnAppearing

The approach shown in your OnStart example can also be applied to OnAppearing

例如

public partial class SomePage : ContentPage {
    public SomelPage() {
        InitializeComponent();
        appearing += onAppearing;
    }

    protected override void OnAppearing() {
        appearing(this, EventArgs.Empty);
        appearing -= onAppearing;
    }

    event EventHandler appearing = delegate { };

    private async void onAppearing(object sender, EventArgs args) {
        try {
            var locator = CrossGeolocator.Current;
            var position = await locator.GetPositionAsync();

            var places = await SomeService.getPlacesOfInterest(position.Latitude, position.Longitude);
            placesListView.ItemsSource = places;
        } catch( Exception ex) {
            //handler error (Log?)
        }
    }
}

或者您可以直接预订实际 Appearing事件

Or you could subscribe to the actual Appearing event directly

//...

public event EventHandler Appearing;

//...

来源

并放弃覆盖OnAppearing()方法

public partial class SomePage : ContentPage {
    public SomelPage() {
        InitializeComponent();
        Appearing += onAppearing;
    }

    private async void onAppearing(object sender, EventArgs args) {
        try {
            var locator = CrossGeolocator.Current;
            var position = await locator.GetPositionAsync();

            var places = await SomeService.getPlacesOfInterest(position.Latitude, position.Longitude);
            placesListView.ItemsSource = places;
        } catch( Exception ex) {
            //handler error (Log?)
        }
    }
}

这篇关于创建事件是使XF OnAppearing成为异步方法的有效方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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