UWP-动态加载不同的样式.xaml

UWP-动态加载不同的样式.xaml,uwp,Uwp,是否可以根据特定条件在运行时动态加载不同样式的ResourceDictionary 我希望有一个单一版本的应用程序,但根据具体设置有不同的颜色/品牌。 假设上述情况不可能实现,我最好能做到这一点吗?据我记忆所及,ResourceDictionary不能在运行时动态设置,而不是在UWP上。我认为唯一的办法是改变彩色画笔或整个风格 第一个选项:如果需要动态更改颜色,则需要在ResourceDictionary中创建笔刷 <SolidColorBrush x:Key="MyBrush">#

是否可以根据特定条件在运行时动态加载不同样式的ResourceDictionary

我希望有一个单一版本的应用程序,但根据具体设置有不同的颜色/品牌。
假设上述情况不可能实现,我最好能做到这一点吗?

据我记忆所及,ResourceDictionary不能在运行时动态设置,而不是在UWP上。我认为唯一的办法是改变彩色画笔或整个风格

第一个选项:如果需要动态更改颜色,则需要在ResourceDictionary中创建笔刷

<SolidColorBrush x:Key="MyBrush">#333344</SolidColorBrush>
第二个选项:如果您需要在运行时更改整个样式,假设您在资源字典中设置了MyStyle和MyOtherStyle,并希望将其应用于名为MyControl的某个控件:

 switch(anyValue)
 {
     case 1:
          var myStyle = Application.Current.Resources["MyStyle"] as Style;
          if(myStyle != null)
          {
              MyControl.Style = myStyle;                 
          }
          break;
     case 2:
          var myOtherStyle = Application.Current.Resources["MyOtherStyle"] as Style;
          if(myOtherStyle != null)
          {
              MyControl.Style = myOtherStyle;                  
          }
          break;
}

第三个选项:使用为控件设置的VisualStates更改样式。它需要为要更改的控件编写自己的VisualState,然后手动切换它们。从未尝试过,所以我不知道这种方法有多可靠。仅在性能假设的情况下,这似乎是最好的方法,但是手动保存每个控件的VisualState可能需要实现您自己的VisualStateManager,并且可能会导致在您需要的时候保存正确的VisualState出现问题。

非常感谢。这足以说明这是一个可靠的答案!
 switch(anyValue)
 {
     case 1:
          var myStyle = Application.Current.Resources["MyStyle"] as Style;
          if(myStyle != null)
          {
              MyControl.Style = myStyle;                 
          }
          break;
     case 2:
          var myOtherStyle = Application.Current.Resources["MyOtherStyle"] as Style;
          if(myOtherStyle != null)
          {
              MyControl.Style = myOtherStyle;                  
          }
          break;
}