سؤال

I'm trying to do a System.Windows.Shapes.Rectangle rotation in Y axis simulating a card rotation, showing all the route.

The problem is that the UI only refreshes at the end.

Simplified version of code

Call of method

for (i=0; i<=180; i++)
{
    int j = i;
    Dispatcher.BeginInvoke(new a_dispatcher(() => {
            print_animation_of_card(card, i);
        }), 
        null);
}

Method

private void print_animation_of_card(System.Windows.Shapes.Rectangle card)
{
    ...
    System.Windows.Media.PlaneProjection p = 
        card.Projection as System.Windows.Media.PlaneProjection;
    p.RotationY = i;
    card.Projection = p;
}

I have also tried to put the FOR into the method, with same result...

How can I do to show all the rotation movement of the System.Windows.Shapes.Rectangle?

هل كانت مفيدة؟

المحلول

I believe the problem is that you are using BeginInvoke() instead of Invoke. This does not wait for one angle of rotation to complete before the next one is called, so the calls are all stacked on top of eachother.

Try it with Dispatcher.Invoke() or put the whole loop inside one Dispatcher.BeginInvoke() and see if the results are more to your satisfaction.

نصائح أخرى

Finally I have done it with a BackgroundWorker, thanks @Jay for the answers.

Call of method

BackgroundWorker bw = new BackgroundWorker();
bw.DoWork += (object sender, DoWorkEventArgs e) =>
{
    print_animation_of_card(card);
};
bw.RunWorkerAsync();

Method

private void print_animation_of_card(System.Windows.Shapes.Rectangle card)
{
    for (int i = 0; i <= 180; i++)
    {
        Thread.Sleep(3);
        Dispatcher.BeginInvoke(new a_dispatcher(() =>
        {
            //same code as topic method code
        }),null);
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top