简体   繁体   English

如何在Windows 8设置面板(C#)中订阅事件

[英]How to subscribe to an event in a windows 8 settings panel (c#)

I'm developing a Windows 8 store application in c#/xaml. 我正在用c#/ xaml开发Windows 8商店应用程序。 The windows store guidelines say that when a user changes a settings, the application should reflect that change immediately. Windows存储准则指出,当用户更改设置时,应用程序应立即反映该更改。 I need help figuring out how to make this happen. 我需要帮助弄清楚如何做到这一点。

Here are some details about my setup. 以下是有关我的设置的一些详细信息。

I've created a custom control called OptionsView: 我创建了一个名为OptionsView的自定义控件:

public partial class OptionsView : UserControl
{
    public OptionsView()
    {
        this.InitializeComponent();
    }

    private void cmbEarliestYear_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        ApplicationDataContainer roamingSettings = Windows.Storage.ApplicationData.Current.RoamingSettings;
        roamingSettings.Containers["appOptions"].Values["earliestYear"] = cmbEarliestYear.SelectedValue.ToString();
    }
}

In my App.xaml.cs class, I'm using a SettingsFlyout from the Callisto library to display the custom options control when the user clicks on the Options link: 在我的App.xaml.cs类中,当用户单击“选项”链接时,我使用Callisto库中的SettingsFlyout来显示自定义选项控件:

protected override void OnWindowCreated(WindowCreatedEventArgs args)
{
    base.OnWindowCreated(args);
    SettingsPane.GetForCurrentView().CommandsRequested += onCommandsRequested;
}

void onCommandsRequested(SettingsPane settingsPane, SettingsPaneCommandsRequestedEventArgs eventArgs)
{
    UICommandInvokedHandler optionsHandler = new UICommandInvokedHandler(onOptionsClick);
    SettingsCommand optionsCommand = new SettingsCommand("options", "Options", optionsHandler);
    eventArgs.Request.ApplicationCommands.Add(optionsCommand);

}

void onOptionsClick(IUICommand command)
{
    SettingsFlyout settings = new SettingsFlyout();
    settings.FlyoutWidth = SettingsFlyout.SettingsFlyoutWidth.Narrow;
    settings.HeaderText = "Options";
    settings.Content = new OptionsView();
    settings.IsOpen = true;
}

I have a page in my application called CreateTripPage. 我的应用程序中有一个名为CreateTripPage的页面。 There's a combobox on that page that allows the user to change the year of the trip. 该页面上有一个组合框,允许用户更改旅行年份。 The earliest year in that combobox needs to change based on the value set by the user in Options. 该组合框中的最早年份需要根据用户在“选项”中设置的值进行更改。 So, when the user changes the value of cmbEarliestYear in the OptionsView while the CreateTripPage is open, I need an event to fire. 因此,当CreateTripPage打开时,用户在OptionsView中更改cmbEarliestYear的值时,我需要触发一个事件。 I can't figure out how to fire/subscribe to the needed event. 我不知道如何触发/订阅所需的事件。

Any help would be appreciated. 任何帮助,将不胜感激。

Here is a simple example of similar behaviour I implemented. 这是我实现的类似行为的简单示例。 Cineworld app can be used to view details about cinemas / movies / in UK and Ireland. Cineworld应用程序可用于查看英国和爱尔兰的电影院/电影的详细信息。

Options page within Settings Pane, allows region to be selected / modified. “设置”窗格中的“选项”页面允许选择/修改区域。 This meant that my app needs to cater for region modification while it is running. 这意味着我的应用程序在运行时需要适应区域修改。

What I tend to do is this: 我倾向于这样做的是:

1) Have a config class that defines properties and persists those values. 1)有一个配置类,用于定义属性并保留这些值。 2) The config class exposes a property 2)config类公开一个属性

public static event Action RegionChanged = delegate { };

3) In setter of the Region property fire the event. 3)在Region属性的setter中触发事件。

if (RegionChanged != null)
    RegionChanged();

4) Now in MainPage.xaml.cs or the main app entry point. 4)现在在MainPage.xaml.cs或主应用程序入口点。

protected async override void OnNavigatedTo(NavigationEventArgs e)
{
    Config.RegionChanged -= Config_RegionChanged;
    Config.RegionChanged += Config_RegionChanged;

    // do whatever else you need to do (initial data load)

    base.OnNavigatedTo(e);
}

async void Config_RegionChanged()
{
    bLoaded = false;
    this.GoHome(this, new RoutedEventArgs());
}

That's it really. 就是这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM