加载图像时如何获得黑色背景?

Kar*_*con 3 wpf xaml

这是一个非常简单的应用程序,可加载带有黑色背景的窗口。

<Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Width="1365" Height="768"
        WindowStartupLocation="CenterScreen"
        Background="Black">
</Window>
Run Code Online (Sandbox Code Playgroud)

效果很好,但是如果添加图像控件,我希望它在显示图像之前显示一秒钟的黑色。

<Image Source="pack://application:,,,/assets/images/bg.jpg" />
Run Code Online (Sandbox Code Playgroud)

但是由于某种原因,背景显示为白色。加载后图像显示完美,但是我希望加载的那1秒是黑色,而不是白色。

加载图像时如何显示黑色背景?

Kcv*_*vin 5

我试图从后台加载图像,但是没有用。我尝试加载图像并使用动画从0到大的动画,但这并没有真正起作用,您必须使用固定的宽度。最后,我只是想使用Image.Loaded事件触发时触发的情节提要来更改可见性。

在工作解决方案中,在Image.Loaded事件触发 0.1秒钟保存图像的Grid的可见性将从Collapsed变为Visible 。您可以通过将“ MG”的默认可见性更改为“ Grid隐藏”或“可见” 来证明这是可行的,并且请注意,与设置为“折叠”相比,该窗口为“白色”的时间更长。

工作XAML

<Window x:Class="Test.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Test"
        mc:Ignorable="d"
        Title="MainWindow"
        Height="350"
        Width="525"
        Background="Black">
    <Grid x:Name="MG" Background="Black" Visibility="Collapsed">
        <Image Source="pack://application:,,,/assets/images/bg.jpg">
            <Image.Triggers>
                <EventTrigger RoutedEvent="Loaded">
                    <EventTrigger.Actions>
                        <BeginStoryboard>
                            <Storyboard>
                                <ObjectAnimationUsingKeyFrames Duration="0:0:0.1"
                                                               Storyboard.TargetName="MG"
                                                               Storyboard.TargetProperty="Visibility">
                                    <ObjectAnimationUsingKeyFrames.KeyFrames>
                                        <DiscreteObjectKeyFrame>
                                            <DiscreteObjectKeyFrame.Value>
                                                <Visibility>Visible</Visibility>
                                            </DiscreteObjectKeyFrame.Value>
                                        </DiscreteObjectKeyFrame>
                                    </ObjectAnimationUsingKeyFrames.KeyFrames>
                                </ObjectAnimationUsingKeyFrames>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger.Actions>
                </EventTrigger>
            </Image.Triggers>
        </Image>
    </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

注意:您可能必须根据要渲染的图像的文件大小调整时间。