Use a theme for a single page

Apr 19, 2013 at 8:09 AM
Hi everyone.

I created a custom theme that I want only for a single view. Is this possible ? The view is Home.xaml, the first one.

For the moment, I loaded the theme at the launch in App.xaml, but I don't know how to switch to the default real theme (light or dark, the only choixes in Settings), and switch back to the custom theme when I go back to the Home.xaml view (after visiting Settings, the default theme is "synced").

Thanks for this amazing toolkit,
IDE.
Coordinator
Apr 20, 2013 at 12:17 PM
You can change the theme in code by setting the AppearanceManager.Current.ThemeSource. The default dark and light theme source are defined as consts on the AppearanceManager class.

Changing the theme per page is possible by implementing IContent and set the theme in the OnNavigatedTo and restore it in the OnNavigatedFrom.
public class MyPage : IContent
{
  private Uri previousThemeSource;

  public void OnNavigatedTo(NavigationEventArgs e)
  {
    // first store the current theme
    this.previousThemeSource = AppearanceManager.Current.ThemeSource;

    // set the dark theme (or any theme you like)
    AppearanceManager.Current.ThemeSource = AppearanceManager.DarkThemeSource;
  }
  public void OnNavigatedFrom(NavigationEventArgs e)
  {
    // restore the previous theme
    AppearanceManager.Current.ThemeSource = this.previousThemeSource;
  }
  public void OnNavigatingFrom(NavigatingCancelEventArgs e)
  {
  }
  public void OnFragmentNavigation(FragmentNavigationEventArgs e)
  {
  }
}
Apr 20, 2013 at 12:30 PM
I just tried, but I'm not sure if I used it correctly. It keeps telling me "Interface member 'void FirstFloor.ModernUI.IContent.OnFragmentNavigation(FragmentNavigationEventArgs)' is not implemented" (replace OnFragmentNavigation by the 3 others, I have 4 errors). Here's my code :
/// <summary>
    /// Interaction logic for Home.xaml
    /// </summary>
    public partial class Home : IContent
    {
        public Home()
        {
            InitializeComponent();
        }
        private Uri previousThemeSource;

        public void OnNavigatedTo(NavigationEventArgs e)
        {
            // first store the current theme
            this.previousThemeSource = AppearanceManager.Current.ThemeSource;

            // set the dark theme (or any theme you like)
            AppearanceManager.Current.ThemeSource = AppearanceManager.DarkThemeSource;
        }
        public void OnNavigatedFrom(NavigationEventArgs e)
        {
            // restore the previous theme
            AppearanceManager.Current.ThemeSource = this.previousThemeSource;
        }
        public void OnNavigatingFrom(NavigatingCancelEventArgs e)
        {
        }
        public void OnFragmentNavigation(FragmentNavigationEventArgs e)
        {
        }
    }
Coordinator
Apr 20, 2013 at 12:35 PM
Make sure you remove the using System.Windows.Navigation; and replace it with using FirstFloor.ModernUI.Windows.Navigation;
Apr 20, 2013 at 12:39 PM
Yes I'm using them (not sure if Visual Studio added them, or ReSharper).
Coordinator
Apr 20, 2013 at 12:42 PM
Edited Apr 20, 2013 at 12:42 PM
And it still won't compile? That's odd, works fine here. NavigationEventArgs is defined in both System.Windows.Navigation and FirstFloor.ModernUI.Windows.Navigation, please double check that System.Windows.Navigation is not used.
Apr 20, 2013 at 12:51 PM
Edited Apr 20, 2013 at 12:52 PM
Yep, double checked. Let's see that from the beginning : I just created a new Modern UI project, and here's my code for Home.xaml.cs :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using FirstFloor.ModernUI.Presentation;
using FirstFloor.ModernUI.Windows;
using FirstFloor.ModernUI.Windows.Navigation;

namespace TestCustomTheme.Pages
{
    /// <summary>
    /// Interaction logic for Home.xaml
    /// </summary>
    public partial class Home : IContent
    {
        public Home()
        {
            InitializeComponent();
        }
        private Uri previousThemeSource;

        public void OnNavigatedTo(NavigationEventArgs e)
        {
            // first store the current theme
            this.previousThemeSource = AppearanceManager.Current.ThemeSource;

            // set the dark theme (or any theme you like)
            AppearanceManager.Current.ThemeSource = AppearanceManager.DarkThemeSource;
        }
        public void OnNavigatedFrom(NavigationEventArgs e)
        {
            // restore the previous theme
            AppearanceManager.Current.ThemeSource = this.previousThemeSource;
        }
        public void OnNavigatingFrom(NavigatingCancelEventArgs e)
        {
        }
        public void OnFragmentNavigation(FragmentNavigationEventArgs e)
        {
        }
    }
}
Still have the same errors.
Apr 20, 2013 at 12:54 PM
Edited Apr 20, 2013 at 12:54 PM
It works! Thanks to ReSharper, I replaced
public void OnNavigatedTo(NavigationEventArgs e)
by
public void OnNavigatedTo(FirstFloor.ModernUI.Windows.Navigation.NavigationEventArgs e)
And it seems to be working on my test project!

So thanks ReSharper and of course thank you for your amazing toolkit!