Xamarin main method or equivalent

Viewed 2246

I'm new to Xamarin and I'm looking for an entry point like a main() method. Also, I have a data storage (i.e. model) class, which constantly receives data from a web socket and shall be accessible from throughout the application (i.e. from multiple ViewModels). Where can I put significant and central classes like these? Would you put these in a static class?

Also: Is there something like a main-loop which is responsible for handling tasks and events?

I'd be very grateful for a generic/primer overview of all the "entry points" within a Xamarin application.

4 Answers

Each platform has their own main() like methods

  • Android: MainActivity.OnCreate()
  • iOS: AppDelegate.FinishedLaunching(UIApplication app, NSDictionary options)
  • UWP: App.OnLaunched(LaunchActivatedEventArgs e)

on Xamarin.Forms applications all those platforms instantiates App class and sets the MainPage.

You should use App.OnStart() method if you want to do for BL.

If you for example create a Cross Platform Mobile App in Visual Studio 2017 you will already get a scaffold. The data layer is located in the 'Services' Folder.

I would consider the "App.xaml" file as your entry point.

   public partial class App : Application
{
    //TODO: Replace with *.azurewebsites.net url after deploying backend to Azure
    public static string AzureBackendUrl = "http://localhost:5000";
    public static bool UseMockDataStore = false;
    public static bool UseEntityFramework = true;

    public App()
    {
        InitializeComponent();

        if (UseMockDataStore)
            DependencyService.Register<MockDataStore>();
        else if (UseEntityFramework)
            DependencyService.Register<SqLiteDataStore>();
        else
            DependencyService.Register<AzureDataStore>();

        MainPage = new MainPage();
    }

    protected override void OnStart()
    {
        // Handle when your app starts
    }

    protected override void OnSleep()
    {
        // Handle when your app sleeps
    }

    protected override void OnResume()
    {
        // Handle when your app resumes
    }
}

Assuming you are talking about Xamarin.Forms,

I'm new to Xamarin and I'm looking for an entry point like a main() method.

  • Now how Xamarin.Forms basically works is that the respective Native Classes i.e. MainActivity(Android), AppDelegate(iOS) and the App(UWP) call the App.Xaml class in the .Net Standard or PCL project. So that could be considered as an entry point just like your main() method of a console app.

I have a data storage (i.e. model) class, which constantly receives data from a web socket and shall be accessible from throughout the application (i.e. from multiple ViewModels).

  • For this, the best way of handling it is an SQLite Database I would suggest you also check this for a better understanding of how it works

Where can I put significant and central classes like these? Would you put these in a static class?

  • You can make them static but I would personally advise you to only put the redundant code as static. And Yes feel free to make static helper classes.

Also: Is there something like a main-loop which is responsible for handling tasks and events?

  • This part of your question is not clear to me kindly elaborate a little more

Feel free to revert in case of queries.

In Android you can override the default MainActivity like below:

enter image description here

Related