Xamarin C# - 从其他 类 调用异步方法
Xamarin C# - Calling Async Methods from other classes
对于项目中的每个页面,我都有推送到导航的异步方法,如下所示:
async void LoadOtherPage(object sender, EventArgs e)
{
await Navigation.PushAsync(new OtherPage());
}
由于应用程序的目的,多个页面定义了相同的功能。但是,我不想一直为每个单独的页面重新定义功能。我只想在一个静态 class 中定义我所有的异步函数,我可以调用它来切换页面。我尝试的解决方案如下:
static class PageCaller
{
static async public void LoadOtherPage(object sender, EventArgs e)
{
await Navigation.PushAsync(new OtherPage());
}
}
不幸的是,当我将 class 和函数设置为静态时,Navigation.PushAsync 没有定义。有没有一种简单的方法可以只定义一次我所有的异步页面调用函数,然后在我需要推送到导航堆栈时调用它们?
我改成了
static class PageCaller
{
static async public void LoadOtherPage(object sender, EventArgs e)
{
await App.Current.MainPage.Navigation.PushAsync(new OtherPage());
}
}
并且有效。谢谢!
对于项目中的每个页面,我都有推送到导航的异步方法,如下所示:
async void LoadOtherPage(object sender, EventArgs e)
{
await Navigation.PushAsync(new OtherPage());
}
由于应用程序的目的,多个页面定义了相同的功能。但是,我不想一直为每个单独的页面重新定义功能。我只想在一个静态 class 中定义我所有的异步函数,我可以调用它来切换页面。我尝试的解决方案如下:
static class PageCaller
{
static async public void LoadOtherPage(object sender, EventArgs e)
{
await Navigation.PushAsync(new OtherPage());
}
}
不幸的是,当我将 class 和函数设置为静态时,Navigation.PushAsync 没有定义。有没有一种简单的方法可以只定义一次我所有的异步页面调用函数,然后在我需要推送到导航堆栈时调用它们?
我改成了
static class PageCaller
{
static async public void LoadOtherPage(object sender, EventArgs e)
{
await App.Current.MainPage.Navigation.PushAsync(new OtherPage());
}
}
并且有效。谢谢!