c# - 更改页面背景时图像变化可怕

标签 c# windows-runtime windows-phone windows-phone-8.1 winrt-xaml

我的项目中有一个页面,当用户在屏幕上触摸时,该页面会将背景属性更改为 Assets 中的随机图像,但是当更改时,页面背景在更改为我的图像之前 0.05 秒变为黑色或白色。太丑了。

这是我在cs文件中的代码

private void LayoutRoot_ManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e)
    {
        Random ran = new Random();
        randomPage.Background = new ImageBrush
        {
            ImageSource =
                new BitmapImage { UriSource = new Uri("ms-appx:///Assets/Backgrounds/" + ran.Next(22) + ".jpg") }
        };

        e.Handled = true;
    }

如何使图片页面背景平滑?

最佳答案

尝试使用 StoryBoard 动画。

Image 设置动画的示例代码

<Image  Stretch="Uniform" Name="myImage" Source="/Images/w1.png" />

<Storyboard x:Name="Storyboard1">
    <ObjectAnimationUsingKeyFrames  Storyboard.TargetProperty="(Image.Source)"  Storyboard.TargetName="myImage" RepeatBehavior="Forever">
        <DiscreteObjectKeyFrame KeyTime="0" Value="="/Images/w1.png"></DiscreteObjectKeyFrame>
        <DiscreteObjectKeyFrame KeyTime="0:0:1" Value="="/Images/w2.png"></DiscreteObjectKeyFrame>
        <DiscreteObjectKeyFrame KeyTime="0:0:2" Value="="/Images/w3.png"></DiscreteObjectKeyFrame>
        <DiscreteObjectKeyFrame KeyTime="0:0:3" Value="="/Images/w4.png"></DiscreteObjectKeyFrame>
        <DiscreteObjectKeyFrame KeyTime="0:0:4" Value="="/Images/w5.png"></DiscreteObjectKeyFrame>
    </ObjectAnimationUsingKeyFrames>
</Storyboard>

Storyboard1.Begin();

在您的情况下,您可能希望在代码隐藏中创建 StoryBoard 并设置您的随机属性。您甚至可以将动画从一帧“缓和”到另一帧,从而使显示更加平滑。

关于c# - 更改页面背景时图像变化可怕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27720450/

相关文章:

c# - 如何上线店铺审核?

jquery - 触摸滑动在 Windows 8 IE 10 手机中不起作用

windows-phone-7 - Windows Phone 8中的自动暗/亮图标支持

c# - Linq to Twitter 错误

c# - 查找在 Visual Studio 或单元测试中未使用 await 的异步用法

c# - EF Core 2.2 中的基类存在问题

c# - 通用接口(interface)实现困惑

c# - 我可以为多个 Windows 应用商店应用程序创建相互登录系统吗?

amazon - 用于 Amazon API 访问的 WinRT 和缺失的 Web API 模型

c# - 如何正确使用 JSON