C# Xamarin Timer Class Not Updating View

后端 未结 1 1762
花落未央
花落未央 2021-01-20 15:38



        
1条回答
  •  伪装坚强ぢ
    2021-01-20 16:36

    You are not on the UI thread as the Timer callback is on a background thread, use Device.BeginInvokeOnMainThread when updating UI elements in that case:

    So when updating your label instance in the callback, do this:

    Device.BeginInvokeOnMainThread(() => label.Text = "" +i;);
    

    If I were to update the text and update another element, would I just place a , after label.Text = ""+1or would I have to have a whole other line replicated,

    The parameter provided to BeginInvokeOnMainThread is an Action, so you can execute as much code as needed on the UI thread using just one "block":

    Device.BeginInvokeOnMainThread(() =>
    {
        ...;
        ...;
        ...;
    });
    

    Or:

    void UIThreadAction()
    {
        ...;
        ...;
        ...;
    }
    Device.BeginInvokeOnMainThread(UIThreadAction);
    

    0 讨论(0)
提交回复
热议问题