src*_*091 2 c# xaml inverse windows-phone-8
在我的WP8应用程序中,我想做一个颜色反转效果.我不知道我应该使用什么工具,所以我只是用一般的术语解释我想要的东西.
它应该如何工作:说我有一个UserControl由黑色矩形和一些白色文本组成的顶部.我想对该用户控件应用一些东西,它将反转UserControl它所覆盖的部分颜色.一些不可见的矩形跨越50%,UserControl在该区域背景将是白色,文本将是黑色.我希望它是动态的,所以我可以在运行时更改它覆盖的区域.
这是一张图片来说明这一点:

反转效应适用于控制的一半.
我相信通过使用具有相同文本,反色和不透明蒙版的两个控件可以实现这一点,但我想知道这是否可以以更干净和直接的方式完成?
我想,你要寻找的理想要么是两个TextBlocks具有OpacityMask适用于在上面像一个;
<Grid MaxWidth="100">
<TextBlock Text="Hey check it out we can change object gradients! yay!" Foreground="Red"
TextWrapping="Wrap" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<TextBlock Text="Hey check it out we can change object gradients! yay!" Foreground="Blue"
TextWrapping="Wrap" HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock.OpacityMask>
<LinearGradientBrush StartPoint="0.1,0.1" EndPoint="0.75,0.75">
<LinearGradientBrush.GradientStops>
<GradientStop Offset="0.322" Color="Black"/>
<GradientStop Offset="0.739" Color="Transparent"/>
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</TextBlock.OpacityMask>
</TextBlock>
</Grid>
Run Code Online (Sandbox Code Playgroud)
或者你可以LinearGradientBrush直接将Foreground(或Background其他元素)直接应用于;
<Border Width="100" Height="50">
<Border.Background>
<LinearGradientBrush StartPoint="0.062,0.552" EndPoint="0.835,0.548">
<LinearGradientBrush.GradientStops>
<GradientStop Offset="0.5" Color="White"/>
<GradientStop Offset="0.5" Color="Black"/>
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</Border.Background>
<TextBlock Text="Hello World!" HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock.Foreground>
<LinearGradientBrush StartPoint="0.1,0.1" EndPoint="0.75,0.75">
<LinearGradientBrush.GradientStops>
<GradientStop Offset="0.5" Color="Black"/>
<GradientStop Offset="0.5" Color="White"/>
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</TextBlock.Foreground>
</TextBlock>
</Border>
Run Code Online (Sandbox Code Playgroud)
或者说是80年代的风格;
<Border Width="100" Height="50">
<Border.Background>
<LinearGradientBrush StartPoint="0.472,0.047" EndPoint="0.47,0.942">
<LinearGradientBrush.GradientStops>
<GradientStop Offset="0.541" Color="White"/>
<GradientStop Offset="0.548" Color="Black"/>
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</Border.Background>
<TextBlock Text="Hello World!" HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock.Foreground>
<LinearGradientBrush StartPoint="0.472,0.047" EndPoint="0.47,0.942">
<LinearGradientBrush.GradientStops>
<GradientStop Offset="0.631" Color="Black"/>
<GradientStop Offset="0.635" Color="White"/>
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</TextBlock.Foreground>
</TextBlock>
</Border>
Run Code Online (Sandbox Code Playgroud)
试一试,希望这会有所帮助.