dimioLt
dimioLt

Reputation: 59

How to pass the x: Name of a control to another class and set values to the properties

I have an Entry which has an x:Name and I want to set text to it in another class, I would like to use Text property in another class. Is it possible to do this? I have tried this code but it does not work, the method Check() is called but the text is not applied.

Control class:

  public Entry MyEntry
        {
            //entrycontrol is the x:Name of the Entry
            get { return entrycontrol; }
        }
  public Control(){
            InitializeComponent();
            var vr = new MyMethods();
            vr.Check();
  }

MyMethods class:

 public async Task Check()
        {
           var page = new Control();

           page.MyEntry.Text = "Hello World";
        }

Upvotes: 0

Views: 132

Answers (1)

Lucas Zhang
Lucas Zhang

Reputation: 18861

In you case , you create a new page in the method Check , not the original Control.

You need to pass the current Control to the class .

public class MyMethods
    {
        Control CurrentControl;

        public MyMethods(Control currentControl)
        {
            CurrentControl = currentControl;
        }

        public async Task Check()
        {
         
           Device.BeginInvokeOnMainThread(()=> {


                Control.MyEntry.Text = "Hello World";

            });
        }
    }
var vr = new MyMethods(this);
vr.Check();

Upvotes: 1

Related Questions