从设置弹出菜单更新2个不同的类? [英] Update 2 different classes from a settings flyout?

查看:88
本文介绍了从设置弹出菜单更新2个不同的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据MSDN指南,我们需要将应用程序的所有设置都放入SettingsPane中,然后应用该设置时,应用程序应该更新所有页面.

As per MSDN guidelines we need to put all the app's settings into the SettingsPane and then the app should update all pages when the settings is applied.

在我的应用程序中,我需要有一个重置选项,该选项将应用程序恢复为默认设置.按下重置按钮后,我需要更新2个页面,Calendar.xaml和HistoryStatistics.xaml.该应用程序的所有数据都放在一个名为CycleManager的单例类中.我已经使用了Callisto工具包中的SettingsFlyout控件.

In my app I need to have a reset option which brings the app to the default settings. There are 2 pages, Calendar.xaml and HistoryStatistics.xaml that i need to update when the reset button is pressed. All the data of the app is put in a singleton class called CycleManager. I have used a SettingsFlyout control from the Callisto Toolkit.

App.Xaml

在App.xaml中注册设置

Registered the settings in the App.xaml

SettingsPane.GetForCurrentView().CommandsRequested += OnCommandsRequested;

并在OnCommandsRequested函数中,创建了重置处理程序

and in OnCommandsRequested function, created the reset handler

        var reset = new SettingsCommand("reset", "Reset", (handler) =>
        {
            var settings = new SettingsFlyout();
            settings.Content = new ResetUserControl();
            settings.HeaderBrush = new SolidColorBrush(_background);
            settings.Background = new SolidColorBrush(_background);
            settings.HeaderText = "Reset";
            settings.IsOpen = true;
        });

        args.Request.ApplicationCommands.Add(reset);

CycleManager.cs

在CycleManager类中,有一个m_Reset变量,其setter和getter以及一个名为ResetClicked的事件处理程序

In the CycleManager class, there is a m_Reset variable,its setter and getter and an event handler called ResetClicked

    public event EventHandler ResetClicked;

    public bool Reset
    {
        get
        {
            return m_reset;
        }
        set
        {
            m_reset = value;
            if (ResetClicked != null)
                ResetClicked(this, EventArgs.Empty);
        }
    }

接下来是我在第一堂课calendar.xaml中关联此处理程序的部分

Next is the part where i have associated this handler in my first class calendar.xaml

Calendar.xaml

在类的构造函数中,我声明事件处理程序

In the constructor of the class I declare the event handler

CycleManager pCycMan = CycleManager.Instance;
pCycMan.ResetClicked += this.ResetClicked;

后面是事件处理程序的定义

followed by the definition of the event handler

    private async void ResetClicked(object sender, EventArgs e)
    {
        CycleManager pCycMan = CycleManager.Instance;
        if (pCycMan.Reset == true)
        {
            try
            {                   
                await Windows.Storage.ApplicationData.Current.ClearAsync(Windows.Storage.ApplicationDataLocality.Local);
                pCycMan.InitializeValues();
            }
            catch (Exception)
            {
            }
        }

        CreateCalendar();// UI is loaded
    }  

在HistoryStatistics.xaml的构造函数中,我完成了与上述操作相同的操作

In the constructor of the HistoryStatistics.xaml I have done the same thing as above

HistoryStatistics.xaml

public HistoryStatistics()
    {
        CycleManager pCycMan = CycleManager.Instance;
        pCycMan.ResetClicked += this.ResetClicked;
    }

并定义

    private void ResetClicked(object sender, EventArgs e)
    {
        CycleManager pCycMan = CycleManager.Instance;
        if (pCycMan.Reset == true)
        {
            await Windows.Storage.ApplicationData.Current.ClearAsync(Windows.Storage.ApplicationDataLocality.Local);
            pCycMan.InitializeValues();
            LoadListView();// loads the UI
            DisplayStatistics();//loads the UI for the page
        }
    } 

现在有问题了

  1. 这是正确的方法吗?

  1. Is this the right approach?

在第二页的第一页(HistoryStatistcs)中按下重置"时,将首先调用在第一页(Calendar.xaml.cs)中声明的单击重置函数,然后在HistoryStatistics中调用该函数.两者都被异步执行! :( 这是正确的行为吗?

When Reset is pressed in the first from the second page(HistoryStatistcs), the reset clicked function declared in the first page(Calendar.xaml.cs) is called first and then the one in HistoryStatistics. And both gets executed async! :( Is this a right behaviour?

这个问题很长.希望每个人都了解情况和问题.

This question is quite long. Hope everybody understood the scenario and question.

推荐答案

您所概述的行为没有任何问题.两个页面订阅一个事件,并且事件使用多类型委托,这意味着它们都将被解雇.

There is nothing wrong with the behaviour you outlined. Two pages subscribe to an event and event uses multi cast delegate which means they will both get fired.

我认为您需要在这里进行更简单的操作.每个xaml页面都应在OnNavigatedTo上预订该事件,并应在OnNavigatedFrom上退订.

I think you need a simpler behaviour here. Each xaml page should subscribe to that event on OnNavigatedTo and should unsubscribe in OnNavigatedFrom.

这样,实际上只有两者之一执行清除.

That way only one of the two actually executes the cleanup.

这篇关于从设置弹出菜单更新2个不同的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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