画线后如何延迟?

时间:2016-10-25 10:57:13

标签: c# wpf

我正在研究一个wpf c#app,我希望在绘制一条线之后,在阻止UI时有1秒的延迟。

我的绘图功能在这里:

    private void DrawLine(int serviceTime, string lineName)
    {
        while (serviceTime != 0)
        {
            (this[lineName] as ObservableCollection<Line>).Add(new Line { X1 = x1, Y1 = y1, X2 = x2, Y2 = y2 });

            //Delay Code

            x1 += 10;
            x2 += 10;
            serviceTime--;
        }
        ResetXY();
    }

1 个答案:

答案 0 :(得分:0)

我使用Devexpress MVVM Dispatcher来解决我的问题:

    private void DrawLine(int serviceTime, string lineName)
    {
        while (serviceTime != 0)
        {
            DispatcherService.BeginInvoke(() =>
            {
                    (this[lineName] as ObservableCollection<Line>).Add(new Line { X1 = x1, Y1 = y1, X2 = x2, Y2 = y2 });
            });
            x1 += 10;
            x2 += 10;
            serviceTime--;
            Thread.Sleep(500);
        }
        ResetXY();
    }