DynamicResource in Windows Store App apps?

后端 未结 3 420
小蘑菇
小蘑菇 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:23

    You don't always need a dynamic resource. Change the value of a "static" resource like this:

    Resources.Remove("foo");
    Resources.Add("foo", "A new hope");
    

    This doesn't always work for declarative stuff. What you can do in these cases is provide a wrapper object; you can't replace the wrapper but you can update its properties. Alas there are cases for which this is not viable.

    0 讨论(0)
  • 2021-01-15 14:26

    If you use the MVVM pattern it should be fairly easy to recreate the views based on a switched theme without recreating the underlying data - depending on the way you marry your views and view-models.

    Just have multiple theme resource dictionaries and switch which of them you merge with your application resource dictionary, then recreate the view.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题