我在从app.xaml访问样式时遇到非常奇怪的情况,而且对于一种样式也是如此。
<Style x:Key="CustomButtonStyle" TargetType="local:ButtonControl">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:ButtonControl">
<Grid DataContext="{TemplateBinding DataContext}">
<Border x:Name="BorderImg" Visibility="{TemplateBinding Visibility}" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" CornerRadius="{TemplateBinding BorderCornerRadius}" DataContext="{TemplateBinding DataContext}">
<Image x:Name="Image" Source="{TemplateBinding BgImage}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" DataContext="{TemplateBinding DataContext}" Stretch="{TemplateBinding Stretch}" Visibility="{TemplateBinding BgImageVisibility}"/>
</Border>
<Border x:Name="BorderFocusImg" Visibility="{TemplateBinding Visibility}" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" CornerRadius="{TemplateBinding BorderCornerRadius}" DataContext="{TemplateBinding DataContext}">
<Image x:Name="FocusImage" Source="{TemplateBinding BgFocusImage}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" DataContext="{TemplateBinding DataContext}" Stretch="{TemplateBinding Stretch}" Visibility="{TemplateBinding BgFocusImageVisibility}"/>
</Border>
<Border x:Name="BorderName" Visibility="{TemplateBinding Visibility}" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" CornerRadius="{TemplateBinding BorderCornerRadius}" DataContext="{TemplateBinding DataContext}">
<TextBlock x:Name="Text" Opacity="{TemplateBinding DisplayText}" Margin="{TemplateBinding Padding}" TextWrapping="Wrap" Foreground="{TemplateBinding Foreground}" Text="{TemplateBinding Content}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" FontSize="{TemplateBinding FontSize}" FontWeight="{TemplateBinding FontWeight}" TextDecorations="{TemplateBinding FontDecorations}" FontStyle="{TemplateBinding FontStyle}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" DataContext="{TemplateBinding DataContext}"/>
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Name="CustomScrollViewerStyle" TargetType="local:CustomScrollViewer">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:CustomScrollViewer">
<Grid Margin="{TemplateBinding Padding}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
>
<Grid.Resources>
<Style x:Name="ScrollViewerWithCompressionStyle" TargetType="ScrollViewer">
<Setter Property="VerticalScrollBarVisibility" Value="Auto"/>
<Setter Property="HorizontalScrollBarVisibility" Value="Auto"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Padding" Value="0"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ScrollViewer">
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="ScrollStates">
<VisualStateGroup.Transitions>
<VisualTransition GeneratedDuration="00:00:00.5"/>
</VisualStateGroup.Transitions>
<VisualState x:Name="Scrolling">
<Storyboard>
<DoubleAnimation Storyboard.TargetName="VerticalScrollBar" Storyboard.TargetProperty="Opacity" To="1" Duration="0"/>
<DoubleAnimation Storyboard.TargetName="HorizontalScrollBar" Storyboard.TargetProperty="Opacity" To="1" Duration="0"/>
</Storyboard>
</VisualState>
<VisualState x:Name="NotScrolling">
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="VerticalCompression">
<VisualState x:Name="NoVerticalCompression"/>
<VisualState x:Name="CompressionTop"/>
<VisualState x:Name="CompressionBottom"/>
</VisualStateGroup>
<VisualStateGroup x:Name="HorizontalCompression">
<VisualState x:Name="NoHorizontalCompression"/>
<VisualState x:Name="CompressionLeft"/>
<VisualState x:Name="CompressionRight"/>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Grid Margin="{TemplateBinding Padding}">
<ScrollContentPresenter x:Name="ScrollContentPresenter" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}"/>
<ScrollBar x:Name="VerticalScrollBar" IsHitTestVisible="False" Height="Auto" Width="5" HorizontalAlignment="Right" VerticalAlignment="Stretch" Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" IsTabStop="False" Maximum="{TemplateBinding ScrollableHeight}" Minimum="0" Value="{TemplateBinding VerticalOffset}" Orientation="Vertical" ViewportSize="{TemplateBinding ViewportHeight}" />
<ScrollBar x:Name="HorizontalScrollBar" IsHitTestVisible="False" Width="Auto" Height="5" HorizontalAlignment="Stretch" VerticalAlignment="Bottom" Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" IsTabStop="False" Maximum="{TemplateBinding ScrollableWidth}" Minimum="0" Value="{TemplateBinding HorizontalOffset}" Orientation="Horizontal" ViewportSize="{TemplateBinding ViewportWidth}" />
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<ScrollViewer x:Name="ScrollViewer" Grid.Column="0" Grid.ColumnSpan="3"
Style="{StaticResource ScrollViewerWithCompressionStyle}"
ContentTemplate="{TemplateBinding ContentTemplate}"
Content="{TemplateBinding Content}"
HorizontalScrollBarVisibility="{TemplateBinding HorizontalScrollBarVisibility}"
VerticalScrollBarVisibility="{TemplateBinding VerticalScrollBarVisibility}"
/>
<ContentControl Grid.Column="0" Grid.ColumnSpan="1" Canvas.ZIndex="2"
Visibility="{TemplateBinding ArrowsVisiblity}"
HorizontalAlignment="Left">
<Image x:Name="LeftImage"
Source="{TemplateBinding LeftImageSource}"
Visibility="{TemplateBinding LeftImageVisibility}"
Stretch="UniformToFill"
Height="50"/>
</ContentControl>
<ContentControl Grid.Column="2" Grid.ColumnSpan="1" Canvas.ZIndex="2"
Visibility="{TemplateBinding ArrowsVisiblity}"
HorizontalAlignment="Right">
<Image x:Name="RightImage"
Source="{TemplateBinding RightImageSource}"
Visibility="{TemplateBinding RightImageVisibility}"
Stretch="UniformToFill"
Height="50"/>
</ContentControl>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我正在尝试通过
分配样式 var temp= (Style)Application.Current.Resources["CustomScrollViewerStyle"];
this.Style = temp;
这里我得到temp变量的值总是为“CustomScrollViewerStyle”为null。但是,当我使用“CustomButtonStyle”时,我得到了样式对象。为什么这两种样式有任何区别,因为它们都放在同一个文件(app.xaml)中?或者它是“CustomScrollViewerStyle”中的问题。有没有人提前注意到这类问题?
这是来自Windows Phone 8的移植代码。