当我在代码中实际更改椭圆时,为什么椭圆的不透明度会自动改变?

时间:2015-05-15 20:24:42

标签: c# visual-studio xaml win-universal-app

我的Windows Universal App中有一个Ellipse,当鼠标进入并离开该椭圆时,我想更改其填充颜色。我想做的就是改变颜色而不是别的。但是,只要我输入鼠标,不透明度就会自动降低。你能否在我的代码中找出问题。

谢谢。

XAML

        <Ellipse x:Name="glamourEllipse" HorizontalAlignment="Left" Height="226" Margin="594,260,0,0" Stroke="#FFB43F3F" VerticalAlignment="Top" Width="226" RenderTransformOrigin="0.5,0.5" UseLayoutRounding="False" d:LayoutRounding="Auto" Tapped="glamour_Tapped">
            <Ellipse.Fill>
                <SolidColorBrush Color="#FFB43C3C"/>
            </Ellipse.Fill>
            <Ellipse.RenderTransform>
                <CompositeTransform Rotation="-90.254"/>
            </Ellipse.RenderTransform>
        </Ellipse>

C#

    SolidColorBrush hoverBrush = new SolidColorBrush();

    private void mouse_Enter(object sender, PointerRoutedEventArgs e)
    {

        hoverBrush.Color = Color.FromArgb(100, 140, 40, 40);
        hoverBrush.Opacity = 1;
        nsfwEllipse.Fill = hoverBrush;

    }

    private void mouse_Exit(object sender, PointerRoutedEventArgs e)
    {

        hoverBrush.Color = Color.FromArgb(100, 180, 60, 60);
        hoverBrush.Opacity = 1;
        nsfwEllipse.Fill = hoverBrush;
    }

1 个答案:

答案 0 :(得分:3)

问题可能是您 更改MouseEnterMouseExit事件的透明度:

<SolidColorBrush Color="#FFB43C3C"/>

此行将颜色设置为255 alpha, 180 red, 60 green and 60 blue。另一方面你的代码:

hoverBrush.Color = Color.FromArgb(100, 140, 40, 40);
hoverBrush.Color = Color.FromArgb(100, 180, 60, 60);

两行都指定alpha 100。现在,在我们的decimal数学系统中,这将是完全有效的,并且意味着100%不透明度,但在计算机的8-bit32-bit世界中,它并非如此。基本上,该代码将alpha设置为64十六进制。 (更改XAML中的FF,您将看到透明度相同。)

Color.FromArgb的重载期望0是透明的,255是不透明的。

https://msdn.microsoft.com/en-us/library/system.drawing.color.fromargb%28v=vs.110%29.aspx

基本上,在这两条100行中将255更改为Color.FromArgb,您就是黄金。

private void mouse_Enter(object sender, PointerRoutedEventArgs e)
{
    hoverBrush.Color = Color.FromArgb(255, 140, 40, 40);
    hoverBrush.Opacity = 1;
    nsfwEllipse.Fill = hoverBrush;
}

private void mouse_Exit(object sender, PointerRoutedEventArgs e)
{
    hoverBrush.Color = Color.FromArgb(255, 180, 60, 60);
    hoverBrush.Opacity = 1;
    nsfwEllipse.Fill = hoverBrush;
}

或者,完全删除该值。

private void mouse_Enter(object sender, PointerRoutedEventArgs e)
{
    hoverBrush.Color = Color.FromArgb(140, 40, 40);
    hoverBrush.Opacity = 1;
    nsfwEllipse.Fill = hoverBrush;
}

private void mouse_Exit(object sender, PointerRoutedEventArgs e)
{
    hoverBrush.Color = Color.FromArgb(180, 60, 60);
    hoverBrush.Opacity = 1;
    nsfwEllipse.Fill = hoverBrush;
}

两个选项总体上具有相同的效果。第二个还保证您不会意外地再次执行此操作,因为alpha始终255

编辑:最后,为了完整性,您也可以在代码中使用十六进制,这可能是更好的选择,因为它完全相同:

private void mouse_Enter(object sender, PointerRoutedEventArgs e)
{
    hoverBrush.Color = Color.FromArgb(0xFF, 0x8C, 0x28, 0x28);
    hoverBrush.Opacity = 1;
    nsfwEllipse.Fill = hoverBrush;
}

private void mouse_Exit(object sender, PointerRoutedEventArgs e)
{
    hoverBrush.Color = Color.FromArgb(0xFF, 0xB4, 0x3C, 0x3C);
    hoverBrush.Opacity = 1;
    nsfwEllipse.Fill = hoverBrush;
}

或者:

private void mouse_Enter(object sender, PointerRoutedEventArgs e)
{
    hoverBrush.Color = Color.FromArgb(0x8C, 0x28, 0x28);
    hoverBrush.Opacity = 1;
    nsfwEllipse.Fill = hoverBrush;
}

private void mouse_Exit(object sender, PointerRoutedEventArgs e)
{
    hoverBrush.Color = Color.FromArgb(0xB4, 0x3C, 0x3C);
    hoverBrush.Opacity = 1;
    nsfwEllipse.Fill = hoverBrush;
}