繁体   English   中英

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

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

如何在 Xamarin 表单中将纯 c# 文件设置为启动文件? 我可以删除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();
        }
    }
}

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

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

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

问题是,名为App的类的 Visual Studio 的默认类模板是

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

namespace ContentPageExample
{
    class App
    {
    }
}

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

您可以通过向App添加显式访问修饰符来解决此问题

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

namespace ContentPageExample
{
    public class App
    {
    }
}

1有一个例外:您可以将InternalsVisibleToAttribute添加到一个程序集以允许另一个程序集访问内部类和方法,但我建议您很少使用它,并且绝对不要在当前情况下使用它。

您可以创建一个完整的 c# 文件作为 Xamarin 表单应用程序的启动。 但请确保您的启动类应从 Xamarin.Forms 的 Application 类派生。 因为它是跨平台应用程序的根页面。 所以你的启动类应该如下所示,

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

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

正如您提到的,您必须在应用程序启动时设置一个新的 c# 文件,让我们假设它为“Test.cs”。 在位于解决方案主项目中的 App.cs 文件的构造函数中,将新的 c# 文件“Test.cs”设置为应用程序的主页面,并提及您使用的页面类型(因为它可以是 ContentPage 或 NavigationPage) . 检查以下示例代码段。

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

如果您使用 MVVM 模式,那么您必须在 App.cs 文件的构造函数中使用 Class 文件注册您的 ViewModel。 创建可以在页面和 ViewModals、SetRoot()、弹出页面和 ViewModels 之间导航的自定义 NavigationService。 然后覆盖 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