0
votes

I have a masterdetailpage and a contentpage that works as a rootpage, so to make a function run from my masterdetailpage i load it via a public method. But when I do it, it works when I type something in the log but when I try to make something real happen, for example change the text of a label or make something bigger etc. It doesn't run. Even though the System.Diagnostics.Debug inside the method runs. My code looks like this:

My masterdetailpage:

knappen.Clicked += (object sender, EventArgs e) => {
                var ourStartPage = new StartPage ();
                ourStartPage.InitThisPage();
            } 

And this is my contentpage (StartPage) with the public method that loads my particular function i want loaded:

public void InitThisPage() 
    {
        LoadData();
    }

async void LoadData() 
    {
        label.TextColor = Color.Red; //so this does not change color
        System.Diagnostics.Debug.WriteLine ("Does it reach this?"); //but it reaches this and types this out in the log.
    }

So above, it reaches the thing I type in the log but the Color of the label does not change. I have tried different labels, buttons, images etc but whatever I do beside run it in the log, does not seem to run.

What can the issue be here?

1
based on code you've posted in the past, I'm fairly certain that you have two instances of StartPage - one that is actually shown on the screen, and one you are creating after the button click. Updating the properties of one does not change the other. - Jason
I only have one "StartPage" and it is the only one i can reach when I create a variable. I can also reach StartPageViewModel. - Yannick
you have one StartPage class, but you have multiple instances of it. Just doing "new StartPage()" does not add it to the visual hierarchy so you can see it on the screen. - Jason
ahh ok. will the InvokeOnMainThread solve it? Or do you have a better solution than that? I cannot currently find InvokeOnMainThread when I try to write it in my code. I guess I am missing an assembly - Yannick
no, this is a fundamental problem with how you are designing your app. As I've suggested before, MessagingCenter would be a better approach, but you seem reluctant to try it - Jason

1 Answers

1
votes

Try updating the label's color on the main UI thread:

InvokeOnMainThread ( () => {
   label.TextColor = Color.Red; //so this does not change color
});

Ref: Working with the UI Thread