Issue
Let me explain the current situation here. I have a main Xamarin.Forms project that consists of a main UI contentpage that is also being used by the Native Projects, because this is how a Xamarin.Forms project works.
The thing is that I want to use a control (such as a Label) that is being used in the MainPage.xaml of the main Xamarin.Forms project within the native Xamarin.Android project, inside MainActivity.cs. Within MainActivity.cs I want to update the value of the control, so that this is being displayed on the UI..
I have named the label as 'lblTest', however MainActivity.cs is unable to find this control. This makes sense of course, because this control does not exist within the Xamarin.Android project.
Do you have any idea how I can access this Xamarin.Forms control within a native Xamarin.Android project? I would appreciate this and thanks in advance.
Solution
As Jason mentioned , MessagingCenter
is a working but messy solution.
Another way is to get access the page first and then update the Label in a public method which is defined inside the page .
For example , if the page is MainPage of App, you can define a public method first .
public MainPage()
{
InitializeComponent();
}
//used to update the label
public void updateLabel(string data)
{
lblTest.Text = data;
}
And update the label in Android project.
((App.Current as App).MainPage as YourPage).updateLabel("the data you want to update");
Answered By - ColeX - MSFT
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.