简体   繁体   中英

Set C# file as startup file in Xamarin.Forms?

How can I set pure c# file as startup file in Xamarin forms? I can delete both MainPage.xaml and MainPage.xaml.cs and add follwoing code from the ContentPage documentation :

using System;
using Xamarin.Forms;

namespace ContentPageExample
{
    public class App : Application
    {
        public static Page GetMainPage ()
        {    
            return new ContentPage { 
                Content = new Label {
                    Text = "Hello, Forms!",
                    VerticalOptions = LayoutOptions.CenterAndExpand,
                    HorizontalOptions = LayoutOptions.CenterAndExpand,
                },
            };
        }

        public App ()
        {
            MainPage = GetMainPage();
        }
    }
}

If above code is added to the App.xaml.cs , it works. But if I delete both App.xaml and App.xaml.cs and then above code is added to a new file App.cs , there is an error

The type or namespace name 'App' could not be found (are you missing a using directive or an assembly reference?) in MainActivity.cs in Android project.

The only way I can currently think of that this issue may arise is that the code depecited is not the exact code that's in your App.cs , but you let Visual Studio add a new class to which you add the code.

The problem is, that the default class template of Visual Studio for a class named App is

using System;
using System.Collections.Generic;
using System.Text;

namespace ContentPageExample
{
    class App
    {
    }
}

Please note that there is no access modifier ( public , internal ) on our class. But for classes the default implicit access level is internal , ie only classes within the same assembly can see this class and create instances of 1 , since MainActivity is located in another assembly it is not allowed to access App .

You can fix this, by adding an explicit access modifier to App

using System;
using System.Collections.Generic;
using System.Text;

namespace ContentPageExample
{
    public class App
    {
    }
}

1 There is an exception: You can add the InternalsVisibleToAttribute to an assembly to allow another assembly to access internal classes and methods, but I'd advise you to use this rarely and definitely not in the present case.

You can create a complete c# file as a startup of Xamarin forms Application. But make sure your startup class should be derived from the Application class of Xamarin.Forms. Since its a root page for the cross-platform application. So your startup class should be like below,

public class StartUp : Application
{
    public StartUp()
    {
        // Set your main page here.
    }
}

In Android, MainActivity class,

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 StartUp());
    }
}

As you mention that you have to set a new c# file at the start-up of the application, let's assume it as "Test.cs". In the constructor of App.cs file located in the main project of your solution, set the new c# file "Test.cs" as Main page of your application mentioning the type of Page you using(as it can be a ContentPage or NavigationPage). Check the below sample snippet.

public App()
        {
            InitializeComponent();
            MainPage = new ContentPage (new Test());
        }

If you using MVVM Pattern, then you have to register your ViewModel with the Class file in the constructor of App.cs file. Create the custom NavigationService which can navigate between pages & ViewModals, SetRoot(), Pops out pages & ViewModels. And then override the OnStart() method of App.cs file & set SetRoot() for the new c# file.

    public App()
    {
        InitializeComponent();
        Instance = this;
        MainPage = new ContentPage { Title = "Test" };
        RegisterPages();
        RegisterModals();
        ModalPopping += ModalService.ModalPopping;
    }

    protected override void OnStart()
    {
        NavigationService.SetRoot(new Test());
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM