Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin main method or equivalent

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.

like image 516
mefiX Avatar asked Jan 02 '23 11:01

mefiX


2 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.

like image 90
eakgul Avatar answered Jan 11 '23 20:01

eakgul


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
    }
}
like image 30
ywwy Avatar answered Jan 11 '23 20:01

ywwy