我添加了一个公共方法来返回变量 pageLoaded,但 Visual Studio 显示此错误“MainPage.PageLoaded() 方法需要对象引用而不是静态”。逻辑是仅当 pageLoaded 为 true 时才完成 Splash Activiy。如果有人对此有更好的想法,很高兴知道,我刚刚开始学习 C#/Xamarin。我的代码:namespace MyApp{ public partial class MainPage : ContentPage { private bool pageLoaded = false; public MainPage() { InitializeComponent(); webnav.HeightRequest = 1000; webnav.WidthRequest = 1000; webnav.Source = "https://www.example.com"; } public void Webnav_Navigated(object sender, WebNavigatedEventArgs e) { pageLoaded = true; } public bool PageLoaded() { return pageLoaded; } }}代码 2:......using MyApp;namespace MyApp.Droid{ [Activity(Label = "My App", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = false, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)] public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity { protected override void OnCreate(Bundle savedInstanceState) { TabLayoutResource = Resource.Layout.Tabbar; ToolbarResource = Resource.Layout.Toolbar; base.OnCreate(savedInstanceState); global::Xamarin.Forms.Forms.Init(this, savedInstanceState); LoadApplication(new App()); while (true) { if (MainPage.PageLoaded()) { SplashScreen.fa.Finish(); break; } } } }}
2 回答

临摹微笑
TA贡献1982条经验 获得超2个赞
问题):
您的.PageLoaded()
方法是一个实例方法。它只能在类型的实例化对象上调用MainPage
您在初始屏幕中对它的引用试图将其称为静态方法,但存在两个问题:
如上所述,您没有使用static 关键字定义它
即使您将其定义为静态,它也不会告诉您有关正在加载的真实页面的任何信息
解决方案:
不要这样做。从知道何时加载的页面实例控制启动屏幕的可见性。从内部创建和引用初始屏幕MainPage
编辑:
为了进一步澄清差异:
// calling a method against the static definition of the class
MainPage.PageLoaded();
// calling a method against an instance of the class
new MainPage().PageLoaded();
上面的代码不是解决方案,而是实例方法和静态方法之间差异的示例。 您的 PageLoaded方法不应该是静态的,因为到目前为止您已经布置了所有内容。
- 2 回答
- 0 关注
- 227 浏览
添加回答
举报
0/150
提交
取消