在Win8.1和WinPhone 8.1之间产生不同视觉效果的XAML样式和模板
我正在基于我在Windows8.1应用程序上所做的工作构建一个通用应用程序。在那个应用程序中,我利用路径以尽可能高的质量显示各种图标 我用一种风格来做这件事:在Win8.1和WinPhone 8.1之间产生不同视觉效果的XAML样式和模板,xaml,windows-8.1,windows-phone-8.1,Xaml,Windows 8.1,Windows Phone 8.1,我正在基于我在Windows8.1应用程序上所做的工作构建一个通用应用程序。在那个应用程序中,我利用路径以尽可能高的质量显示各种图标 我用一种风格来做这件事: <Style x:Key="UnstyledGraphicsButtonStyle" TargetType="Button"> <Setter Property="ContentTemplate"> <Setter.Value> <DataTempl
<Style x:Key="UnstyledGraphicsButtonStyle" TargetType="Button">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<Path Width="{Binding Width, RelativeSource={RelativeSource Mode=TemplatedParent}}"
Height="{Binding Width, RelativeSource={RelativeSource Mode=TemplatedParent}}"
Stretch="Uniform"
Fill="{Binding Foreground, RelativeSource={RelativeSource Mode=TemplatedParent}}"
Data="{Binding Content, RelativeSource={RelativeSource Mode=TemplatedParent}}"/>
</DataTemplate>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid>
<Border x:Name="Border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Margin="0">
<ContentPresenter x:Name="ContentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" ContentTransitions="{TemplateBinding ContentTransitions}" Content="{TemplateBinding Content}" HorizontalAlignment="Left" Margin="6,4" VerticalAlignment="Center" RenderTransformOrigin="0.5,0.5" FontFamily="Global User Interface"/>
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
所以它是这样写的
<Border x:Name="Border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Margin="0" Width="60" Height="60" HorizontalAlignment="Left">
现在,问题是宽度和高度是硬编码的,而不是由父对象设置的。尽管上面的例子使用了一个60px的boarder和一个50px的按钮,但我的应用程序中有一些地方使用了不同的大小,因此希望避免在模板中硬编码尺寸
我是否在XAML中做了一些错误的事情来解释为什么Windows Phone XAML会这样做
最干净/最好的修复方法是什么
谢谢
Philip尝试将路径数据放入嵌套在Viewbox中的路径,如下图所示
<Border Background="{StaticResource ListViewItemPlaceholderBackgroundThemeBrush}"
Width="60"
Height="60"
Visibility="{Binding ImageIsAvailable, Converter={StaticResource HideIfTrue}}">
<Button Style="{StaticResource UnstyledGraphicsButtonStyle}"
Width="{Binding ImageWidth50}"
Height="{Binding ImageHeight50}"
Foreground="white"
Padding="0"
Background="Transparent"
BorderBrush="{x:Null}"
IsEnabled="False" >
<Button.ContentTemplate>
<DataTemplate>
<Viewbox>
<Path Stroke="{Binding Foreground, RelativeSource={RelativeSource Mode=TemplatedParent}}"
Width="{Binding ImageWidth50}"
Height="{Binding ImageHeight50}"
StrokeThickness="2"
Stretch="Uniform"
Data="Path Data Here" />
</Viewbox>
</DataTemplate>
</Button.ContentTemplate>
</Button>
</Border>
谢谢你的建议,加里。如果我像您在这里所做的那样将DataTemplate从样式中分离出来,它就会工作。如果我试图将DataTemplate保留在样式中,那么添加Viewbox并不能解决此问题。在Viewbox中,尝试为相应的属性添加{TemplateBinding Width}和{TemplateBinding Height}。通过这种方式,您可以使用任何在边框上起作用的值(如果起作用,则使用硬编码),但Viewbox将根据给定给父元素的大小对其进行调整。我尝试过,但似乎没有任何区别。我开始怀疑Windows Phone的XAML系统是否存在缺陷,因为分解DataTemplate可以解决问题,而不会对用于显示内容的XAML进行任何实际更改!
Width="60" Height="60" HorizontalAlignment="Left"
<Border x:Name="Border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Margin="0" Width="60" Height="60" HorizontalAlignment="Left">
<Border Background="{StaticResource ListViewItemPlaceholderBackgroundThemeBrush}"
Width="60"
Height="60"
Visibility="{Binding ImageIsAvailable, Converter={StaticResource HideIfTrue}}">
<Button Style="{StaticResource UnstyledGraphicsButtonStyle}"
Width="{Binding ImageWidth50}"
Height="{Binding ImageHeight50}"
Foreground="white"
Padding="0"
Background="Transparent"
BorderBrush="{x:Null}"
IsEnabled="False" >
<Button.ContentTemplate>
<DataTemplate>
<Viewbox>
<Path Stroke="{Binding Foreground, RelativeSource={RelativeSource Mode=TemplatedParent}}"
Width="{Binding ImageWidth50}"
Height="{Binding ImageHeight50}"
StrokeThickness="2"
Stretch="Uniform"
Data="Path Data Here" />
</Viewbox>
</DataTemplate>
</Button.ContentTemplate>
</Button>
</Border>