简体   繁体   English

在 Xamarin.Forms 中将 C# 文件设置为启动文件?

[英]Set C# file as startup file in Xamarin.Forms?

How can I set pure c# file as startup file in Xamarin forms?如何在 Xamarin 表单中将纯 c# 文件设置为启动文件? I can delete both MainPage.xaml and MainPage.xaml.cs and add follwoing code from the ContentPage documentation :我可以删除MainPage.xamlMainPage.xaml.cs并从ContentPage文档中添加以下代码:

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.如果将上述代码添加到App.xaml.cs ,则可以正常工作。 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但是如果我同时删除App.xamlApp.xaml.cs ,然后将上面的代码添加到新文件App.cs ,则会出现错误

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.在 Android 项目的 MainActivity.cs 中找不到类型或命名空间名称“App”(您是否缺少 using 指令或程序集引用?)。

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.我现在能想到的,这个问题可能出现的唯一方法是depecited代码不是确切的代码,在你的App.cs ,但你让Visual Studio中添加到您的代码添加一个新的类。

The problem is, that the default class template of Visual Studio for a class named App is问题是,名为App的类的 Visual Studio 的默认类模板是

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.请注意,我们的类没有访问修饰符( publicinternal )。 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 .但是对于类,默认的隐式访问级别是internal ,即只有同一程序集中的类才能看到此类并创建1 的实例,因为MainActivity位于另一个程序集中,因此不允许访问App

You can fix this, by adding an explicit access modifier to App您可以通过向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. 1有一个例外:您可以将InternalsVisibleToAttribute添加到一个程序集以允许另一个程序集访问内部类和方法,但我建议您很少使用它,并且绝对不要在当前情况下使用它。

You can create a complete c# file as a startup of Xamarin forms Application.您可以创建一个完整的 c# 文件作为 Xamarin 表单应用程序的启动。 But make sure your startup class should be derived from the Application class of Xamarin.Forms.但请确保您的启动类应从 Xamarin.Forms 的 Application 类派生。 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,在 Android 中,MainActivity 类,

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".正如您提到的,您必须在应用程序启动时设置一个新的 c# 文件,让我们假设它为“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).在位于解决方案主项目中的 App.cs 文件的构造函数中,将新的 c# 文件“Test.cs”设置为应用程序的主页面,并提及您使用的页面类型(因为它可以是 ContentPage 或 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.如果您使用 MVVM 模式,那么您必须在 App.cs 文件的构造函数中使用 Class 文件注册您的 ViewModel。 Create the custom NavigationService which can navigate between pages & ViewModals, SetRoot(), Pops out pages & ViewModels.创建可以在页面和 ViewModals、SetRoot()、弹出页面和 ViewModels 之间导航的自定义 NavigationService。 And then override the OnStart() method of App.cs file & set SetRoot() for the new c# file.然后覆盖 App.cs 文件的 OnStart() 方法并为新的 c# 文件设置 SetRoot()。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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