Forum Xamarin Xamarin.Android

Force view to redraw?

Invalidate() works when called inside a property (one of the properties in my view) which is called by MainActivity; it doesn't work when called in a normal method or a timer's elapsed method. Can anyone explain why this is, and what the correct call is to force a redraw?

public Point Coords
{
get
{
return coords;
}
set
{
coords = value;
Invalidate(); //this works
}
}

public void startTimer(int time)
{
Timer myTimer = new Timer(time);
myTimer.Elapsed += OnTimedEvent;
myTimer.Enabled = true;
Invalidate(); //this does not work
}

public void OnTimedEvent( object sender, ElapsedEventArgs e)
{
someGlobalVariable += 5;
Invalidate(); //this also does not work
}

Answers

  • CheesebaronCheesebaron DKInsider, University mod

    Try using PostInvalidate() instead. You are not on the UI thread inside of OnTimedEvent which is probably why it does not get invalidated.

  • That makes sense in the timer thread, but the first case above that didn't work (the method) is on the UI thread. And in anycase I tried PostInvalidate() as well it didn't change anything. I even tried setting the Coords property with a dummy value (to trigger the Invalidate() call that seems to already work) but that didn't work either.

  • CaioshinCaioshin ITMember ✭✭

    I'm trying to obtain the same result because I need that the same code called on onLayout event will be called when I pressed back; I called PostInvalidate() in OnVisibilityChanged when my page is going out with

    if (visibility != ViewStates.Visible)
    {
    this.PostInvalidate();
    }

    but when I return back my code that redraw the view is not called

Sign In or Register to comment.