简体   繁体   中英

How to access textview in MainPage.xaml from MainActivity.cs in visual studio 2017

I have create xamarin cross-paltform. I have textviews in mainActivity.xanl that is shared to all other mobile system(ios,android,) that I need to call it from Mainpage in android. and is it possible to do it otherway around, by calling textview in mainactivity from mainpage

You could use Messaging Center to send notification from ContentPage to MainActivity.

in MainActivity

using Xamarin.Forms;
   protected override void OnCreate(Bundle savedInstanceState)
    {
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;

        base.OnCreate(savedInstanceState);

        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        global::Xamarin.Forms.Forms.Init(this, savedInstanceState);


        MessagingCenter.Subscribe<Object>(this, "testview", (arg) => {

            TextView textView = FindViewById<TextView>(Resource.Id.xxx);

            //... do some thing you want
        
        });


        LoadApplication(new App());
    }

in ContentPage

invoke the line when you want

MessagingCenter.Send<Object>(this, "testview");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM