0

有两个图像控件。

这是一个程序,当按下键盘上的左/右按钮时,左右滑动控件。

当你第一次运行它时,你只会看到图像控制编号 1。

按下键盘时,1 号和 2 号相互滑动。

并且数字2的图像源发生了变化。

目的是获取特定文件夹中的所有图像,这是带有键盘的幻灯片。

但也有一些问题。

如果您按住箭头键,动画循环比计划快得多。

坦率地说,我希望它会更快,但这并不容易。

如果您按住键盘并松开它,则会播放最后一个动画。

我们希望 Completed 事件仅在键盘最终释放时触发一次。

(清晰地重新排列图像)

<Window x:Class="WPFTest2022.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:WPFTest2022"
    mc:Ignorable="d"
    PreviewKeyDown="Window_PreviewKeyDown"
    PreviewKeyUp="Window_PreviewKeyUp"
    Title="MainWindow" Height="450" Width="800">
<Grid>
    <Image x:Name="NewPage" Width="Auto" Height="Auto" Stretch="Uniform">
        <Image.RenderTransform>
            <TransformGroup>
                <TranslateTransform x:Name="NewPageTranslateTransform" X="0" Y="0"/>
            </TransformGroup>
        </Image.RenderTransform>
    </Image>
    <Image x:Name="CurrentPage">
        <Image.RenderTransform>
            <TransformGroup>
                <TranslateTransform x:Name="CurrentPageTranslateTransform" X="0" Y="0"/>
            </TransformGroup>
        </Image.RenderTransform>
    </Image>
</Grid>
enum PageDirection
{
    Prev,
    Next
}

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    string imageDirectoryPath = @"D:\";
    List<string> imageList;

    int currentPageNumber = 0;

    DoubleAnimation NewPageAnimation = new DoubleAnimation();
    DoubleAnimation CurrentPageAnimation = new DoubleAnimation();

    Duration animationDuration = TimeSpan.FromSeconds(.2);

    bool IsKeyPressed = false;

    public MainWindow()
    {
        InitializeComponent();

        imageList = System.IO.Directory.GetFiles(imageDirectoryPath).ToList<string>();

        NewPageAnimation.To = 0;
        NewPageAnimation.Duration = animationDuration;
        
        CurrentPageAnimation.From = 0;
        CurrentPageAnimation.Duration = animationDuration;

        CurrentPage.Source = new BitmapImage(new Uri(imageList[currentPageNumber]));

        NewPageAnimation.Completed += (s, e) =>
        {
            isRunningAnimation = false;
        };
    }

    bool isRunningAnimation = false;

    private void ImageSlider(PageDirection direction)
    {
        if (isRunningAnimation)
        {
            CurrentPage.Source = NewPage.Source;
        }

        isRunningAnimation = true;

        if (direction.Equals(PageDirection.Next) && currentPageNumber >= imageList.Count - 1)
        {
            return;
        }

        NewPageAnimation.From = direction.Equals(PageDirection.Next) ? this.ActualWidth : -this.ActualWidth;
        
        CurrentPageAnimation.To = direction.Equals(PageDirection.Next) ? -this.ActualWidth : this.ActualWidth;

        NewPage.Source = new BitmapImage(new Uri(imageList[direction.Equals(PageDirection.Next) ? ++currentPageNumber : --currentPageNumber])); //new BitmapImage(new Uri(textBookList[pageKind.Equals(PageKind.Next) ? ++currentPageNumber : --currentPageNumber]));
        
        
        CurrentPageTranslateTransform.BeginAnimation(TranslateTransform.XProperty, CurrentPageAnimation, HandoffBehavior.Compose);
        NewPageTranslateTransform.BeginAnimation(TranslateTransform.XProperty, NewPageAnimation, HandoffBehavior.Compose);
    }

    
    private void CurrentPageAnimation_Completed(object sender, EventArgs e)
    {
       // Last Animation Completed
    }

    private void NewPageAnimation_Completed(object sender, EventArgs e)
    {
        // Last Animation Completed
    }

    private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        Key inputKey = e.Key.Equals(Key.ImeProcessed) ? e.ImeProcessedKey : e.Key;
        //NewPageAnimation.Completed -= new EventHandler(NewPageAnimation_Completed);
        IsKeyPressed = true;
        switch (inputKey)
        {
            case Key.D:
            case Key.Right:
                if (currentPageNumber < imageList.Count)
                {
                    this.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)(() =>
                    {
                        ImageSlider(PageDirection.Next);
                    }));
                }
                break;
            case Key.A:
            case Key.Left:
                if(currentPageNumber > 0)
                {
                    this.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)(() =>
                    {
                        ImageSlider(PageDirection.Prev);
                    }));
                }
                break;
        }
    }

    private void Window_PreviewKeyUp(object sender, KeyEventArgs e)
    {
        Key inputKey = e.Key.Equals(Key.ImeProcessed) ? e.ImeProcessedKey : e.Key;
        //NewPageAnimation.Completed += new EventHandler(NewPageAnimation_Completed);
        IsKeyPressed = false;
        switch (inputKey)
        {
            case Key.D:
            case Key.Right:
                break;
            case Key.A:
            case Key.Left:
                break;
        }
    }
}
4

0 回答 0