kamranayub / CaliburnBindableAppBar

Bindable App Bar support for Caliburn.Micro and WP7.1+
MIT License
16 stars 10 forks source link

How to get access to real ApplicationBar to use ThemeManager #11

Closed rmtuckerphx closed 11 years ago

rmtuckerphx commented 11 years ago

I want to use the ThemeManager (see http://www.jeff.wilcox.name/2012/01/phonethememanager/) to style the ApplicationBar.

How do I get access to the ApplicationBar so that I can call:

ThemeManager.MatchOverriddenTheme(appBar);

kamranayub commented 11 years ago

You can do this using the OnLoaded event of the control. This is how I do it in my own app:

In XAML

<bindableAppBar:BindableAppBar IsVisible="True" Loaded="FrameworkElement_OnLoaded">

In code-behind

private void FrameworkElement_OnLoaded(object sender, RoutedEventArgs e) {
    var appbar = sender as BindableAppBar;

    // Light theme
    appbar.MatchOverriddenTheme();
    appbar.StateChanged += (o, args) => appbar.MatchOverriddenTheme();
    appbar.Invalidated += (o, args) => appbar.MatchOverriddenTheme();
}

To promote reusability, feel free to inherit from my appbar class and add this code to the constructor.

public class ThemeableBindableAppBar : BindableAppBar {

    public ThemeableBindableAppBar() {
        this.Loaded += OnLoaded;
    }

    private void OnLoaded(object sender, RoutedEventArgs e) {
        // Light theme
        this.MatchOverriddenTheme();
        this.StateChanged += (o, args) => this.MatchOverriddenTheme();
        this.Invalidated += (o, args) => this.MatchOverriddenTheme();
    }
}