DynamicResource in Windows Store App apps?

后端 未结 3 539
小蘑菇
小蘑菇 2021-01-15 13:39

WPF\'s support for DynamicResource is great because this allows users to change the look and fell of a running application.

WinRT does not support DynamicResource th

3条回答
  •  醉酒成梦
    2021-01-15 14:34

    I'm currently trying to come up with a good design on this as well. I had something fairly "ready-to-go" before I discovered the lack of DynamicResources. Oops.

    The best approach I have come up with is to have a ThemedViewModel base that listens for changes to the settings VM and exposes resources that UI elements can bind to:

    public class ThemedViewModel : ViewModelBase
    {
        public Brush Foreground { get { return ViewModelSelector.Settings.Theme.Foreground; } }
    
        public ThemedViewModel()
        {
            ViewModelSelector.Settings.PropertyChanged += (sender,arg) =>
            {
                if(arg.PropertyName == "Theme")
                {
                    RaisePropertyChanged("Foreground");
                }
            }
        }
    }
    

    You would then derive any theme-based VMs from this, and any themed UI elements would bind to the exposed resources. It is fairly DRY, but really breaks the point of a VM in my mind. The VM shouldn't have UI resources in it. Having how it looks in the VM doesn't sit well with me. But every approach where the themes are constrained to the View section of my code seems to run into problems at bind-time.

提交回复
热议问题