C# 可以在VisualStateManager中修改静态资源吗?
我有许多字体大小、画笔等的全局常量。示例:C# 可以在VisualStateManager中修改静态资源吗?,c#,uwp,C#,Uwp,我有许多字体大小、画笔等的全局常量。示例: <x:Double x:Key="SmallWindowWidth">0</x:Double> <x:Double x:Key="CompactWindowWidth">600</x:Double> <x:Double x:Key="MediumWindowWidth">720</x:Double> <x:Double x:Key="WideWindowWidth">1
<x:Double x:Key="SmallWindowWidth">0</x:Double>
<x:Double x:Key="CompactWindowWidth">600</x:Double>
<x:Double x:Key="MediumWindowWidth">720</x:Double>
<x:Double x:Key="WideWindowWidth">1024</x:Double>
<x:Double x:Key="SmallTitleFontSize">22</x:Double>
<x:Double x:Key="NormalFontSize">16</x:Double>
0
600
720
1024
22
16
当窗口宽度变小时,我想减小一些文本的大小。当然,我可以分别针对每个对象,但我更愿意全局更改{StaticResource NormalFontSize},如下所示:
<VisualState x:Name="Small">
<VisualState.StateTriggers>
<AdaptiveTrigger MinWindowWidth="{StaticResource SmallWindowWidth}" />
</VisualState.StateTriggers>
<VisualState.Setters>
<Setter Target="{StaticResource NormalFontSize}" Value="12"/>
</VisualState.Setters>
</VisualState>
…这似乎不起作用,因为它不是财产。那么,是否有任何方法可以更改XAML(!)中的静态资源?好的,您可以通过一些调整来完成。 注意,我已经在UWP中对其进行了本地测试,以回答您的问题,但我还没有在我发布的任何项目中使用它 第一步,
- 如果您需要更改具有依赖属性的资源,如实心笔刷的颜色。[无需包装]
- 如果您需要更改没有依赖属性的资源,如double value[需要包装器]
public class DoubleWrapper : DependencyObject { public double Value { get { return (double)GetValue(ValueProperty); } set { SetValue(ValueProperty, value); } } // Using a DependencyProperty as the backing store for Value. This enables animation, styling, binding, etc... public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(double), typeof(DoubleWrapper), new PropertyMetadata(0.0)); }
最后,当您在VisualStateSetter中更改FontSizeWrapper或MainBrush的值属性时,它将更新所有绑定
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="LayoutStates">
<VisualState x:Name="NarrowState">
<VisualState.StateTriggers>
<AdaptiveTrigger MinWindowWidth="0"/>
</VisualState.StateTriggers>
<VisualState.Setters>
</VisualState.Setters>
</VisualState>
<VisualState x:Name="WideState">
<VisualState.Setters>
<Setter Target="FontSizeWrapper.(DoubleWrapper.Value)" >
<Setter.Value>
<x:Double>25</x:Double>
</Setter.Value>
</Setter>
<Setter Target="MainBrush.(SolidColorBrush.Color)" Value="Aqua" />
</VisualState.Setters>
<VisualState.StateTriggers>
<AdaptiveTrigger MinWindowWidth="800"/>
</VisualState.StateTriggers>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<TextBlock Text="This is test" Foreground="{StaticResource MainBrush}"
VerticalAlignment="Center"
FontSize="{Binding Value, Source={StaticResource FontSizeWrapper}}"/>
</Grid>
25
即使有可能,它也不会像这样工作,因为StaticResource即使更改,也不会更新它的“使用者”。所以我想您必须解决这个问题,可能是绑定、INotifyPropertyChanged和自定义对象……在XAML中,有两种类型的资源:静态资源和动态资源。这是由于绑定是如何被设计来更新的。静态资源在启动时读取到内存中一次,但动态资源在每次属性系统感觉到需要刷新时读取。代码项目有一篇文章:StaticResources vs.DynamicResources,这将对您有所帮助。
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="LayoutStates">
<VisualState x:Name="NarrowState">
<VisualState.StateTriggers>
<AdaptiveTrigger MinWindowWidth="0"/>
</VisualState.StateTriggers>
<VisualState.Setters>
</VisualState.Setters>
</VisualState>
<VisualState x:Name="WideState">
<VisualState.Setters>
<Setter Target="FontSizeWrapper.(DoubleWrapper.Value)" >
<Setter.Value>
<x:Double>25</x:Double>
</Setter.Value>
</Setter>
<Setter Target="MainBrush.(SolidColorBrush.Color)" Value="Aqua" />
</VisualState.Setters>
<VisualState.StateTriggers>
<AdaptiveTrigger MinWindowWidth="800"/>
</VisualState.StateTriggers>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<TextBlock Text="This is test" Foreground="{StaticResource MainBrush}"
VerticalAlignment="Center"
FontSize="{Binding Value, Source={StaticResource FontSizeWrapper}}"/>
</Grid>