-2

I added a public method to return variable pageLoaded but visual studio shows this error "An object reference is needed by the method MainPage.PageLoaded() not static". The logic is to finish Splash Activiy only if pageLoaded be true. If someone has a better idea for this, would be nice to know, I'm just started learning C#/Xamarin.

My code:

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;
        }
    }
}

Code 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;
                }
            }
        }
   }
}
clarkitect
  • 1,720
  • 14
  • 23
Éder Rocha
  • 1,538
  • 11
  • 29

2 Answers2

1

Problem(s):

Your .PageLoaded() method is an instance method. It can only be called on an instantiated object of type MainPage

Your reference to it, in your splash screen, is attempting to call it as a static method which has two problems:

  1. As stated above, you didn't define it using the static keyword
  2. Even if you define it static, it won't tell you anything about a real page being loaded

Solution:

Don't do this. Control the visibility of your splash screen from the instance of the page that knows when it has been loaded. Create and reference the splash screen from within MainPage

Edit:

To further clarify the difference:

// calling a method against the static definition of the class
MainPage.PageLoaded();
// calling a method against an instance of the class
new MainPage().PageLoaded();

The above code is not a solution but rather an example of the difference between an instance method and a static method. Your PageLoaded method should not be static as you've laid everything out so far.

clarkitect
  • 1,720
  • 14
  • 23
  • thank you for your answer. I have only one more question, if you allow me. Showing splash screen from MainPage. would be a valid splashscreen? I mean, isn't splash screen as an activity, a "native" feature to lead with the load? – Éder Rocha Sep 09 '18 at 18:28
-3

Since MainPage class is not static, you need to create an object of MainPage class and use that object to call the method from that class.

Badhon Jain
  • 938
  • 6
  • 20
  • 38
  • 3
    You're correct about static versus instance being the problem. Unfortunately, creating another instance of `MainPage` from within his activity doesn't solve this particular problem nor does it solve the misunderstanding of instance versus static methods. – clarkitect Sep 09 '18 at 18:24
  • I just tried to help him with his error from my mobile, didn't try to teach him best practice. People down vote for no reason now a days. – Badhon Jain Sep 09 '18 at 19:38