C# 沙马林。恢复后的不同行为应用程序

C# 沙马林。恢复后的不同行为应用程序,c#,xamarin,xamarin.android,onresume,C#,Xamarin,Xamarin.android,Onresume,在学习xamarin的第一个项目中,我制作了一个简单的应用程序,用户可以在其中创建一个便条,并添加警报时间来安排本地通知。 应用程序从后台恢复时出现问题 切中要害 注意型号: public class Note { [PrimaryKey, AutoIncrement] public int Id { get; set; } [MaxLength(255)] public string Title { get; set; } [MaxLength(255)

在学习xamarin的第一个项目中,我制作了一个简单的应用程序,用户可以在其中创建一个便条,并添加警报时间来安排本地通知。 应用程序从后台恢复时出现问题

切中要害

注意型号:

public class Note
{
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }
    [MaxLength(255)]
    public string Title { get; set; }
    [MaxLength(255)]
    public string Content { get; set; }

    public DateTime TimeCreate { get; set; }
    public DateTime AlarmTime { get; set; }
    public bool AlarmTimeActive { get; set; }
}
在主页上有注释列表。每个便笺都有一个开关按钮,用户可以在其中打开/关闭时间警报。 若用户试图打开警报,功能检查是否已过计划时间。如果消失,则将开关保持在关闭位置并显示应用程序信息。在另一种情况下,函数将数据库中的值更新为“true”

XAML

<local:ButtonActiveSwitcher Toggled="Switch_Toggled" IsToggled="{Binding AlarmTimeActive}" Active="{Binding .}" />
这是我的问题

例如:一个便笺上有
AlarmTimeActive
“true”和用户点击的“Home”按钮,应用程序进入后台。稍后,当计划警报时间结束时,用户点击应用程序切换按钮下列表中的应用程序,将应用程序置于前台。由于某些原因,应用程序首先显示警报“时间已逝”,然后(我认为)执行函数
OnAppearing()
。最后,在主页上我有一个更新记录的注释列表,但为什么应用程序首先显示此警报

但是这个问题在另外三种情况下没有出现。
用户关闭应用程序切换列表中的应用程序,并再次打开应用程序列表中的点击图标。
用户点击后退按钮退出应用。
用户通过点击通知来恢复应用程序

那么,为什么用户从应用程序切换程序列表中恢复应用程序时,会显示此警报,但在其他情况下不会显示

我希望我的描述清楚。
请向我解释为什么会发生这种情况以及如何修复它。

除了事件处理程序之外,请尽量避免
异步无效<代码>OnAppearing
不是事件处理程序。但是它是在实际的
事件出现之前调用的,这使您有机会使用实际的事件处理程序订阅它,从而允许您正确使用async/wait

protected override void OnAppearing() {
    this.Appearing += Page_Appearing;                    
    base.OnAppearing();
}

private async void Page_Appearing(object sender, EventArgs e) {
    //...call async code here
    await DataBaseService.checkNoteAlarmTimeActive();
    var notes = await loadNotes();
    listNotes.ItemsSource = notes;
    //unsubscribing from the event (optional but advised)
    this.Appearing -= Page_Appearing;
}

/// <summary>
/// Generate list of notes basic on current sorting option and active switcher
/// </summary>     
private Task<IEnumerable<Note>> loadNotes()
{
    return _sorting.sortNotes(_sortOption, _activeSwitcherState);          
}

构造函数中调用的函数也应该重构到事件处理程序中。

抱歉,但没有帮助。同样的效果。我也重构了构造函数。
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class MainPage : ContentPage
{
    private Sorting _sorting;
    private int _sortOption;
    private bool _activeSwitcherState;
    private DateTime _nooLoopTime;


    public MainPage(int noteid)
    {
        DataBaseService.CreateTables();            
        this._sorting = new Sorting();                     

        InitializeComponent();
        this.AddPickerItems();
        if (noteid != 0)
        {
            this.loadNoteView(noteid);
        }
    }

    protected async override void OnAppearing()
    {          
        await DataBaseService.checkNoteAlarmTimeActive();
        this.loadNotes();                       
        base.OnAppearing();
    }

    /// <summary>
    /// Generate list of notes basic on current sorting option and active switcher
    /// </summary>     
    private async void loadNotes()
    {
        listNotes.ItemsSource = await _sorting.sortNotes(_sortOption, _activeSwitcherState);          
    }
}
protected override void OnAppearing() {
    this.Appearing += Page_Appearing;                    
    base.OnAppearing();
}

private async void Page_Appearing(object sender, EventArgs e) {
    //...call async code here
    await DataBaseService.checkNoteAlarmTimeActive();
    var notes = await loadNotes();
    listNotes.ItemsSource = notes;
    //unsubscribing from the event (optional but advised)
    this.Appearing -= Page_Appearing;
}

/// <summary>
/// Generate list of notes basic on current sorting option and active switcher
/// </summary>     
private Task<IEnumerable<Note>> loadNotes()
{
    return _sorting.sortNotes(_sortOption, _activeSwitcherState);          
}
if (noteid != 0)
{
    this.loadNoteView(noteid);
}