为什么我的椭圆的不透明度会自动改变,而我实际上并没有在代码中更改它?

Why is the opacity of my ellipse changing automatically when I don't actually change it in the code?

我的 Windows 通用应用程序中有一个椭圆,我想在鼠标进入和离开该椭圆时更改其填充颜色。我只想改变颜色,别无其他。但是,每当我输入鼠标时,不透明度会自动降低。你能找出我代码中的问题吗?

谢谢。

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;
    }

问题很可能是您正在更改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);

这两行都指定了 100alpha。现在,在我们的 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

基本上,将这两行 Color.FromArgb 中的 100 更改为 255,你就成功了。

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;
}