Setting or modifying ThemeResource in code

后端 未结 5 709
野趣味
野趣味 2021-02-06 04:14

My questions is very specific to ThemeResources in a Windows 10 Store App. Unfortunately several things available in \"classic\" WPF are different or not available here.

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-06 04:50

    Once I've also faced the same problem and I also haven't found a way to programatically change ThemeResource so that it will change along with phone's theme. Nevertheless there is a way to achieve what you want, but it's cumbersome and may need a lot of work when you want to implement this to many controls.

    The basic idea is to use VisualStates to change from/to ThemeResource - the states are defined in xaml so this will work with ThemeResources. Then in code you can invoke the change back to phone's theme value. Here below is the sample button changing to theme's/user's color.

    
        

    and code behind:

    public class ExtendedButton : Button
    {
        public SolidColorBrush UserBackground
        {
            get { return (SolidColorBrush)GetValue(UserBackgroundProperty); }
            set { SetValue(UserBackgroundProperty, value); }
        }
    
        public static readonly DependencyProperty UserBackgroundProperty =
            DependencyProperty.Register("UserBackground", typeof(SolidColorBrush), typeof(ExtendedButton),
                new PropertyMetadata(new SolidColorBrush(Colors.Red), (s, e) =>
                { if ((s as ExtendedButton).IsUserTheme) (s as ExtendedButton).Background = e.NewValue as SolidColorBrush; }));
    
        // we need some property to indicate if to use user's theme or phone's
        public bool IsUserTheme
        {
            get { return (bool)GetValue(IsUserThemeProperty); }
            set { SetValue(IsUserThemeProperty, value); }
        }
    
        public static readonly DependencyProperty IsUserThemeProperty =
            DependencyProperty.Register("IsUserTheme", typeof(bool), typeof(ExtendedButton), new PropertyMetadata(false, (s, e) =>
            {
                if ((bool)e.NewValue)
                {
                    VisualStateManager.GoToState((s as ExtendedButton), "UserColor", false);
                    (s as ExtendedButton).Background = (s as ExtendedButton).UserBackground;                
                }
                else VisualStateManager.GoToState((s as ExtendedButton), "ThemeColor", false);
            }));
    }
    
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            Random random = new Random();
            UserBtn.Click += (s, e) => UserBtn.IsUserTheme = !UserBtn.IsUserTheme; ;
            ColorBtn.Click += (s, e) => UserBtn.UserBackground = new SolidColorBrush(Color.FromArgb(0xFF, (byte)random.Next(255), (byte)random.Next(255), (byte)random.Next(255)));
        }
    }
    

    It's a long way above just to change one color, but should work and maybe will give you an idea. Those are also DependencyProperties so you can use binding if needed.

提交回复
热议问题