从由 Xamarin.Forms 中的另一个 ContentPage 调用的类文件调用 Navigation.PushAsync()

Cos*_*tas 1 c# xamarin xamarin.forms

我的项目中有一个与任何内容都不相关的 .cs 文件Pages,它由 .cs 文件调用MainPage

调用await Navigation.PushAsync(new MyPage());结果出现以下错误: Error CS0103 The name 'Navigation' does not exist in the current context

Class定义从更改class MyClassclass MyClass : MainPage错误更改为:Error CS0120 An object reference is required for the non-static field, method, or property 'NavigableElement.Navigation'

哪种调用方式正确Navigation.PushAsync()且不会出错?

Jas*_*son 5

用于App.Current.MainPage获取对活动页面的引用(您可能需要强制转换它),然后使用它的Navigation属性