简体   繁体   English

在Silverlight中更改关键的烧烤风格

[英]Change key baised style in silverlight

I have two styles in two resource dictionaries. 我在两个资源词典中都有两种风格。 When application load then I have loaded one ( Dictionary1.xaml ) resource. 当应用程序加载时,我已经加载了一个( Dictionary1.xaml )资源。 After that when I click on button then I want to change style. 之后,当我单击按钮时,我想更改样式。 ie I want to load Dictionary2.xaml . 即我想加载Dictionary2.xaml I have changed but there is no effect on my button. 我进行了更改,但对我的按钮没有影响。

Dictionary1.xaml Dictionary1.xaml

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style TargetType="Button" x:Key="s1">
        <Setter Property="Background" Value="Red"/>  
        <Setter Property="Foreground" Value="Red"/> 
    </Style>
</ResourceDictionary>

Dictionary2.xaml Dictionary2.xaml

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style TargetType="Button" x:Key="s1">
        <Setter Property="Background" Value="Green</Setter>
        <Setter Property="Foreground" Value="Green"></Setter>/>
    </Style>
</ResourceDictionary>

When application load then 当应用程序加载时

Uri sUri = new Uri("/KeyCheck;component/Dictionary1.xaml", UriKind.Relative);
ResourceDictionary r = new ResourceDictionary();
r.Source = sUri;
Resources.MergedDictionaries.Add(r);

Apply style in page: Main.xaml 在页面中应用样式:Main.xaml

<Grid x:Name="LayoutRoot" Background="White">
    <Button Style="{StaticResource s1}" Content="Button" HorizontalAlignment="Left" Margin="195,133,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/> 
</Grid>

When click on button then: 当点击按钮时:

private void Button_Click(object sender, RoutedEventArgs e)
{
    Uri sUri = new Uri("/KeyCheck;component/Dictionary1.xaml", UriKind.Relative);
    ResourceDictionary r = new ResourceDictionary();
    r.Source = sUri;
    App.Current.Resources.MergedDictionaries.Remove(r);

    sUri = new Uri("/KeyCheck;component/Dictionary2.xaml", UriKind.Relative);
    r = new ResourceDictionary();
    r.Source = sUri;
    App.Current.Resources.MergedDictionaries.Add(r);
}

I was trying to do the same thing but couldn't get it to work. 我试图做同样的事情,但无法正常工作。 I got the dictionary to load but coulnd't get it to update to UI, tried everything (re binded & various update UI techniques). 我加载了字典,但无法将其更新到UI,尝试了所有操作(重新绑定和各种更新UI技术)。

In the end i ended up just changing the value of the static resource at runtime, depending on user selection : 最后,我最终只是在运行时根据用户选择更改了静态资源的值:

Application.Current.Resources("Background").Color = HexColor("#FF838383")

Hex color is a method used to convert hex codes to ARGB 十六进制颜色是一种用于将十六进制代码转换为ARGB的方法

I have solved this problem as described below: 我已经解决了如下问题:

  1. At first I have created a class like so: 首先,我创建了一个像这样的类:

     public class ProjectKeys : INotifyPropertyChanged { Style styKey = new Style(); public Style StyKey { get { return styKey; } set { styKey = value; NotifyPropertyChanged("StyKey"); } } protected void NotifyPropertyChanged(string propertyName) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } public event PropertyChangedEventHandler PropertyChanged; } 

    which contains my all key styles. 其中包含我所有的关键样式。

  2. After that I have created a class which contains ProjectKeys type property. 之后,我创建了一个包含ProjectKeys类型属性的类。 This class is inherited by other view model classes. 此类由其他视图模型类继承。

     public class VMBase : INotifyPropertyChanged { public VMBase() { } ProjectKeys styKey = App.AllStyleKeys; public ProjectKeys VMStyKey { get { return styKey; } set { styKey = value; NotifyPropertyChanged("VMStyKey"); } } protected void NotifyPropertyChanged(string propertyName) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } public event PropertyChangedEventHandler PropertyChanged; } 
  3. After that I have inherited above class in my view model 之后,我在视图模型中继承了上面的类

     Public class VMTest1: VMBase { } Public class VMTest2: VMBase { } 
  4. Now I have binded the above two vm in two Views: 现在,我在两个视图中绑定了以上两个虚拟机:

     ViewTest1() { //others... this.DataContext = new VMTest1(); } ViewTest1() { //others... this.DataContext = new VMTest2(); } 
  5. After that I have changed in xaml 之后,我改变了xaml

     <Button Style="{Binding Path= VMStyKey.StyKey}" Content="Button" HorizontalAlignment="Left" Margin="121,134,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click" > 
  6. The following code starts the application: 以下代码启动该应用程序:

     private static ProjectKeys _AllStyleKeys = new ProjectKeys(); public static ProjectKeys AllStyleKeys { get { return _AllStyleKeys; } set { _AllStyleKeys = value; } } public App() { Uri sUri = new Uri("/KeyCheck;component/Dictionary1.xaml", UriKind.Relative); ResourceDictionary r = new ResourceDictionary(); r.Source = sUri; Resources.MergedDictionaries.Add(r); AllStyleKeys.StyKey = (Style)App.Current.Resources["MyKey"]; } 

    7.After changing my style, opened pages, view pages and new pages get the same style. 7.更改样式后,打开的页面,查看页面和新页面将具有相同的样式。

     private void Button_Click(object sender, RoutedEventArgs e) { Uri sUri = new Uri("/KeyCheck;component/Dictionary1.xaml", UriKind.Relative); ResourceDictionary r = new ResourceDictionary(); r.Source = sUri; App.Current.Resources.MergedDictionaries.Remove(r); sUri = new Uri("/KeyCheck;component/Dictionary2.xaml", UriKind.Relative); r = new ResourceDictionary(); r.Source = sUri; App.Current.Resources.MergedDictionaries.Add(r); var Style = App.Current.Resources["MyKey"] as Style; App.AllStyleKeys.StyKey = Style; } 

Button风格的DynamicResource绑定可以解决问题。

Style="{DynamicResource s1}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM