按顺序移动按钮

时间:2015-03-30 08:45:24

标签: c# .net wpf

抱歉英语不好。 我想按RUN后按顺序移动按钮。 从这个意义上说,当运行算法我想逐步移动按钮。 例如移动13,然后移动14,然后移动15     puzzle

我尝试了一切,我累了:(

    public static class StoryboardExtensions
    {
        public static async Task BeginAsync(this Storyboard storyboard)
        {
            TaskCompletionSource<bool> tb = new TaskCompletionSource<bool>();
            EventHandler onComplete = null;
            onComplete = (s, e) =>
            {
                storyboard.Completed -= onComplete;
                tb.SetResult(true);
            };
            storyboard.Completed += onComplete;
            storyboard.Begin();
            await tb.Task;
        }
    }

我尝试等待和异步但不起作用,完成算法后将所有按钮移动到一起,而不是按顺序移动按钮

对于xaml文件中的一个按钮:

    <Button Name="butt1" Content="1" HorizontalAlignment="Left" VerticalAlignment="Top"
             Width="70" Height="70" >
        <Button.Triggers>
            <EventTrigger RoutedEvent="Button.Loaded">
                <BeginStoryboard>
                    <Storyboard Name="stor1">
                        <ThicknessAnimation Name="thic1" Storyboard.TargetName="butt1" Storyboard.TargetProperty="Margin"
                            Duration="0:0:0.25"/>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </Button.Triggers>
    </Button>

在.cs文件中,首先我尝试等待异步但不起作用然后我评论它们

    public void stor_Completed(string num, char direction)
    {
        Button butt = (Button)this.FindName("butt" + num);
        Storyboard stor = (Storyboard)this.FindName("stor" + num);
        ThicknessAnimation thic = (ThicknessAnimation)this.FindName("thic" + num);
        thic.From = butt.Margin;
        thic.To = null;
        //thic.By = null;
        TranslateTransform tt = new TranslateTransform();
        tt.X = butt.RenderTransform.Value.OffsetX;
        tt.Y = butt.RenderTransform.Value.OffsetY;

        if (direction == 'U')
        {
            thic.By = new Thickness(0, -80, 0, 0);
        }
        else if (direction == 'D')
        {
            thic.By = new Thickness(0, 80, 0, 0);
        }
        else if (direction == 'R')
        {
            thic.By = new Thickness(80, 0, 0, 0);
        }
        else if (direction == 'L')
        {
            thic.By = new Thickness(-80, 0, 0, 0);
        }
        stor.Children.Add(thic);
        stor.Begin();

        //await stor.BeginAsync();        //or tow next line
        //Task tcs = StoryboardExtensions.BeginAsync(stor);
        //await tcs;
    }

我称之为此功能:

    stor_Completed((matrix[indexOf16]).ToString(), 'L');

请帮帮我

1 个答案:

答案 0 :(得分:0)

已修改版本

class StoryboardExtension看起来不错,但您应该像这样更改stor_Completed

        public async void AnimationInSequence()
        {    
            await stor_Completed("13", 'L').BeginAsync();
            await stor_Completed("10", 'D').BeginAsync();
            await stor_Completed("9", 'R').BeginAsync();   
        }

        public Storyboard stor_Completed(string num, char direction)
        {
            Button butt = (Button)this.FindName("butt" + num);
            Storyboard stor = (Storyboard)this.FindName("stor" + num);
            ThicknessAnimation thic = (ThicknessAnimation)this.FindName("thic" + num);
            thic.From = butt.Margin;
            thic.To = null;
            //thic.By = null;
            TranslateTransform tt = new TranslateTransform();
            tt.X = butt.RenderTransform.Value.OffsetX;
            tt.Y = butt.RenderTransform.Value.OffsetY;

            if (direction == 'U')
            {
                thic.By = new Thickness(0, -80, 0, 0);
            }
            else if (direction == 'D')
            {
                thic.By = new Thickness(0, 80, 0, 0);
            }
            else if (direction == 'R')
            {
                thic.By = new Thickness(80, 0, 0, 0);
            }
            else if (direction == 'L')
            {
                thic.By = new Thickness(-80, 0, 0, 0);
            }
            stor.Children.Add(thic);
            return stor;
        }

请注意,我在此编辑中测试了我的代码,按预期工作(故事板动画按顺序排列)

整个项目位于this repository

点击Questions =&gt;按钮一起移动(您的代码)

点击Solution =&gt;他们一步一步地移动(我的代码)