user10575898
user10575898

Reputation:

How to Edit Text From Another Class In another Class?

i would like change the text of in a class i transfered from another class in an Entry ,this is my code

public class contentPage:ContentPage
{
    public Entry TextEnrty;
    public Button doneButton;
    public contentPage()

    {
        doneButton.Clicked+=doneButtonClicked;
        Content =new StackLayout
        {
            Children = {TextEntry,doneButton}
        }
    }

    private void doneButton_Clicked(object sender, System.EventArgs e)
    {
        App.Current.MainPage= new ContentPage2(TextEntry.text)
    }
}
public class ContentPage2:ContentPage
{
    public label TextLabel;
    public Button EditButton;
    public ContentPage2(string parameter)

    {
        EditButton.clicked+=EditButtonClicked;
        TextLabel.Text = parameter;
        Content =new StackLayout
        {
            Children = {TextLabel,EditButton}
        }
    }

    private void EditButtonClicked(object sender, System.EventArgs e)
    {
        App.Current.MainPage= new contentPage()
    }
}

According to the code when Text is entered in the TextEntry in ContentPage, The TextEntry.Text Is sent as a parameter to contentpage2 after doneButton is clicked, and the value is set equal to TextLabel.Text,

Now, i want to edit the TextLabel.text,but since i am not using the navigation button bit EditButton, the old text will not show in the TextEntry after i go back to edit the text.

So my problem is that , i want the old text to show when i go back to chenge the text and not an empty Entry.

Upvotes: 0

Views: 515

Answers (2)

lorenzw
lorenzw

Reputation: 396

Why don't you just make a second constructor like this:

contentPage(string entryText)
{
    InitializeComponent();

    TextEnrty.Text=entryText;
}

And in the line in your secon class where you "navigate" back to your class, when creating a new instance of contentPage use this new Constructor.

Upvotes: 1

divyang4481
divyang4481

Reputation: 1783

you should try MVVM pattern and share viewmodel across this two view

<Application.Resources>
    <local:ViewModel x:Key="sharedViewModel" />
</Application.Resources>

Upvotes: 0

Related Questions