我有两个活动A和B,我想从活动A调用活动B函数。我该怎么做?因此,我想在活动A中调用此函数。
public void LoadXMLData()
{
var assembly = typeof(MainPage).GetTypeInfo().Assembly;
Stream stream = assembly.GetManifestResourceStream("xmlEXAMPLE.persons.xml");
ObservableCollection<Person> PersonInformation;
var serializer = new XmlSerializer(typeof(ObservableCollection<Person>));
PersonInformation = (ObservableCollection<Person>)serializer.Deserialize(stream);
MyListView.ItemsSource = PersonInformation;
}
您的项目是xamarin.forms还是xamarin.android吗?
最简单的方法是将活动设置为全局静态。
如果您的项目是xamarin.forms,则可以将page
设置为static
:
public partial class Page1 : ContentPage
{
public static Page1 Instance;
public Page1()
{
InitializeComponent();
Instance = this;
}
//here is the method you want call in Page2
public void DoInPage2()
{
.......
}
}
然后在第2页中,您可以像Page1.Instance.DoInPage2();
一样致电>
如果您的项目是xamarin.android,也可以将activity
设置为static
public class ActivityA: Activity { public static ActivityA Instance; protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); SetContentView(....); Instance = this; } //here is the method you want call in ActivityB public void CallInActivityB() { ... } }
然后您可以在ActivityB中像
ActivityA.Instance.CallInActivityB();
一样调用它>