Nick V.
Nick V.

Reputation: 3

How to access a variable in another page

So I have this page in an UWP Windows 10 app using C#:

public sealed partial class MainPage : Page

This page contains a TextBlock called tbPageTitle.

I'd like to change the text of tbPageTitle to "bla" from another page, so I use the following code:

MainPage.tbPageTitle.text = "bla";

However, I get the following error:

CS0120 An object reference is required for the non-static field, method, or property 'MainPage.tbPageTitle'

I don't know what to do here. I feel like I've read every single Google result. I found some results to create a new instance of a class, so that would be for example:

MainPage mp = new MainPage();
mp.tbPageTitle.text = "bla";

But wouldn't that create a completely new MainPage? This also doesn't work by the way...

Upvotes: 0

Views: 1751

Answers (1)

DevAttendant
DevAttendant

Reputation: 636

According to the answer to Sandy's comment you have an inner Frame element in the MainPage where you load other pages. So the easiest way to get the current MainPage instance is the following:

MainPage mainPage = (Window.Current.Content as Frame).Content as MainPage;

Note that this will obviously fail if you every navigate outside of the MainPage and call this line there. Additionally note that objects you are creating in XAML are not public, what means that you can't access your tbPageTitle element here anyway, but will need to create any kind of wrapper property in your MainPage like this:

public string PageTitle {
    get { return tbPageTitle.Text; }
    set { tbPageTitle.Text = value; }
}

However as mentioned by HeySatan, this is not the most beautiful code design you are creating here. Maybe you could create a method to go to a specific frame, something like that:

public enum TabContent { Home, Replies, Messages, Settings }

public void OpenTab(TabContent content) {
    // Set Page title and navigate
    switch (content) {
        case TabContent.Home:
            tbPageTitle.Text = "Home";        
            InnerFrame.Navigate(typeof(HomePage));
            break;
        case TabContent.Replies:
            tbPageTitle.Text = "Replies";        
            InnerFrame.Navigate(typeof(RepliesPage));
            break;
        case TabContent.Messages:
            tbPageTitle.Text = "Messages";        
            InnerFrame.Navigate(typeof(MessagesPage));
            break;
        case TabContent.Settings:
            tbPageTitle.Text = "Settings";        
            InnerFrame.Navigate(typeof(SettingsPage));
            break;
    }
}

Main goal of this method is that if you have a button Settings in your HomePage you are only calling the following line and all logic to do the navigation stays in the MainPage and HomePage only has logic related to itself:

// In HomePage:
MainPage mainPage = (Window.Current.Content as Frame).Content as MainPage;
mainPage.OpenTab(TabContent.Settings);

If you don't want to access Window.Current.Content all the time, you could also declare a static method in your MainPage class and make access simpler:

// In MainPage:
public static MainPage Instance {
    // This will return null when your current page is not a MainPage instance!
    get { return (Window.Current.Content as Frame).Content as MainPage; }
}

// Now in HomePage it's only:
MainPage.Instance.OpenTab(TabContent.Settings);

Upvotes: 2

Related Questions