none
wpf 快速切换图片会闪烁有什么解决方案 RRS feed

  • 问题

  • 10毫秒切换一次图片,不断的切换,而且不闪烁不卡顿有没有什么解决方案?

    这里是我做了一个demohttps://github.com/hoge-jafer/hoverpicture.git,定时器去切换没有反应,鼠标点击切换就有反应,不知道为什么

    2020年8月26日 15:01

全部回复

  • 你好,

    我下载了你的demo,分别运行了Kaishi_Click方法中的两种方式,并没有出现卡顿现象。你可以把复现问题的步骤告诉我吗?或者把出现闪烁和卡顿的图贴出来,这样我才能分析你的问题。

    谢谢

    Daisy Tian


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2020年8月27日 2:53
  • 在定时器里面10毫秒切换一次,图片没有来回切换,怎么回事呢?
    2020年8月27日 2:58
  • 你好,

    修改你的dispatcherTimer_Tick为:

     private void dispatcherTimer_Tick(object sender, EventArgs e)
            {
                string a = AppDomain.CurrentDomain.BaseDirectory + "test.mtsd";
                string b = AppDomain.CurrentDomain.BaseDirectory + "s000.mtsd";
                index++;
                if (index % 2 == 0)
                {
                    FileStream fsMyfiles = new FileStream(a, FileMode.Open);
                    BinaryReader brMyfiles = new BinaryReader(fsMyfiles);
                    byte[] vss = brMyfiles.ReadBytes(430000);
                    test.Source = null;
                    test.Source = WriteableBitmapNew(vss, 500, 860, 96, 96, PixelFormats.Bgr24);
                    fsMyfiles.Close();
                    Debug.WriteLine("显示a:test.mtsd");
                }
                else
                {
                    FileStream fsMyfiles = new FileStream(b, FileMode.Open);
                    BinaryReader brMyfiles = new BinaryReader(fsMyfiles);
                    byte[] vss = brMyfiles.ReadBytes(430000);
                    test.Source = null;
                    test.Source = WriteableBitmapNew(vss, 500, 860, 96, 96, PixelFormats.Bgr24);
                    fsMyfiles.Close();
                    Debug.WriteLine("显示b:s000.mtsd");
                }
            }

    以下是解释:

    你在dispatcherTimer_Tick里边用了for循环语句,所以每次执行完dispatcherTimer_Tick后,图片都会是后者,也就是else中的b图片。你应该使用和手动切换的语句一样,用index++来执行。

    以下我给你举一个简单的demo供你分析:

    首先是xaml页面代码:

      <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"></ColumnDefinition>
                <ColumnDefinition Width="*"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <TextBlock Grid.Row="0" Grid.Column="0"  x:Name="NoCycle" HorizontalAlignment="Center" TextWrapping="Wrap" Text="没有循环:" Foreground="Red" VerticalAlignment="Center" FontSize="72"/>
            <TextBlock Grid.Row="0" Grid.Column="1"  x:Name="Time_Text" HorizontalAlignment="Center" TextWrapping="Wrap" Text="00:00:00"  Foreground="Red" VerticalAlignment="Center" FontSize="72"/>
            <TextBlock Grid.Row="1" Grid.Column="0"  x:Name="CycleWith" HorizontalAlignment="Center" TextWrapping="Wrap" Text="含有循环:" VerticalAlignment="Center" FontSize="72"/>
            <TextBlock Grid.Row="1" Grid.Column="1"  x:Name="Time_TextWithCycle" HorizontalAlignment="Center" TextWrapping="Wrap" Text="00:00:00" VerticalAlignment="Center" FontSize="72"/>
        </Grid>

    对应的cs代码是:

    private DispatcherTimer Time = null;
            public MainWindow()
            {
                InitializeComponent();
                Time = new DispatcherTimer();
                Time.Interval = TimeSpan.FromMilliseconds(100);
                Time.Tick += Time_Tick;
                Time.Start();
            }
    
            void Time_Tick(object sender, EventArgs e)
            {
                Time_Text.Text = DateTime.Now.ToString("HH:mm:ss");
                for(int i =0; i<2;i++)
                {
                    if(i==0)
                    {
                        Time_TextWithCycle.Text = DateTime.Now.ToString("HH:mm:ss");
                        Time_TextWithCycle.Foreground = Brushes.Green;
                    }
                    else
                    {
                        Time_TextWithCycle.Text = DateTime.Now.ToString("HH:mm:ss");
                        Time_TextWithCycle.Foreground = Brushes.Blue;
                    }
                }
            }

    运行结果是:

    含有循环的你可以看出一直是Blue字体(Green颜色看不见),实际上Green颜色是执行的,只不过事件间隔太短,以至于肉眼不可见。

    谢谢

    Daisy Tian


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2020年8月28日 2:27