简体   繁体   中英

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

I'm developing a Windows 8 store application in c#/xaml. The windows store guidelines say that when a user changes a settings, the application should reflect that change immediately. 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:

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:

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. 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. 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.

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. 2) The config class exposes a property

public static event Action RegionChanged = delegate { };

3) In setter of the Region property fire the event.

if (RegionChanged != null)
    RegionChanged();

4) Now in MainPage.xaml.cs or the main app entry point.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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