Xamarin.Froms项目中包含的文件

时间:2023-03-09 19:19:11
Xamarin.Froms项目中包含的文件
Clearly, the program created by the Xamarin.Forms template is very simple, so this is an excellent opportunity to examine the generated code files and figure out their interrelationships and how they work. 

Let’s begin with the code that’s responsible for drawing the text that you see on the screen. This is the App class in the Hello project. In a project created by Visual Studio, the App class is defined in the App.cs file, but in Xamarin Studio, the file is Hello.cs. If the project template hasn’t changed too much since this chapter was written, it probably looks something like this: 

原文

  我们使用Xamarin.Forms模板创建的初始项目非常简洁,然而通过这个程序,我们很容易查看生成的代码文件,并找出它们之间的关系,以及如何工作的。

  让我们从显示一段文字到屏幕上的程序开始,下面是Hello项目中的APP类,如果你是使用Visual Studio创建的工程,APP类是包含在一个叫做App.cs的类文件中,如果是Xamarin Studio该类是包含在一个Hello.cs的文件中。如果在本书示例书写后,Xamarin的项目模板没有重大变化,那么示例代码是下面这种样子。

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

using Xamarin.Forms;

namespace Hello
{
    public class App : Application
    {
        public App()
        {
            // The root page of your application
            MainPage = new ContentPage
            {
                Content = new StackLayout
                {
                    VerticalOptions = LayoutOptions.Center,
                    Children = {
                        new Label {
                            HorizontalTextAlignment  = TextAlignment.Center,
                            Text = "Welcome to Xamarin Forms!"
                        }
                    }
                }
            };
        }

        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
        }
    }
}
Notice that the namespace is the same as the project name. This App class is defined as public and derives from the Xamarin.Forms Application class. The constructor really has just one responsibility: to set the MainPage property of the Application class to an object of type Page. 

The code that the Xamarin.Forms template has generated here shows one very simple approach to defining this constructor: The ContentPage class derives from Page and is very common in single-page Xamarin.Forms applications. (You’ll see a lot of ContentPage throughout this book.) It occupies most of the phone’s screen with the exception of the status bar at the top of the Android screen, the buttons on the bottom of the Android screen, and the status bar at the top of the Windows Phone screen. (As you’ll discover, the iOS status bar is actually part of the ContentPage in single-page applications.) 

The ContentPage class defines a property named Content that you set to the content of the page. Generally this content is a layout that in turn contains a bunch of views, and in this case it’s set to a StackLayout, which arranges its children in a stack.

This StackLayout has only one child, which is a Label. The Label class derives from View and is used in Xamarin.Forms applications to display up to a paragraph of text. The VerticalOptions and HorizontalTextAlignment properties are discussed in more detail later in this chapter. 

For your own single-page Xamarin.Forms applications, you’ll generally be defining your own class that derives from ContentPage. The constructor of the App class then sets an instance of the class that you define to its MainPage property. You’ll see how this works shortly. 

原文

  命名空间的名称和项目的名称相同,App类定义为Public访问,从 Xamarin.Forms.Application类派生。构造函数的作用只有一个,去设置Application类的MainPage属性为一个Page对象。

  Xamarin.Forms模板生成的代码显示一个定义控制器的简单方法:使用从Page中派生的ContentPage,这种方法在单页面的Xamarin.Froms应用程序中非常普遍(在这本书中,你可以看到很多ContentPage)。

  ContentPage类有一个叫做Content的属性,可以去设置这个页面的内容。一般来说,内容使用一个布局元素,布局元素中包含一堆视图元素。在这种情况下,可以将Content的属性设置为一个栈式布局元素,它会将子节点按栈的方式进行布局。

  示例中的StackLayout只有一个子节点Label,Label派生自Xamarin.Forms.View,在Xamarin.Forms应用程序中用于显示一段文本。VerticalOptions和HorizontalTextAlignment属性在这章后面再详细的讨论。

  一般可以自己定义一个类,然后从ContentPage进行继承,然后在App类的构造器中,将自定义类的实例值赋值给MainPage属性,你将会看到ContentPage是如何工作的。

In the Hello solution, you’ll also see an AssemblyInfo.cs file for creating the PCL and a packages.config file that contains the NuGet packages required by the program. In the References section under Hello in the solution list, you’ll see at least the four libraries this PCL requires: 

  .NET (displayed as .NET Portable Subset in Xamarin Studio)
  Xamarin.Forms.Core
  Xamarin.Forms.Xaml
  Xamarin.Forms.Platform 

It is this PCL project that will receive the bulk of your attention as you’re writing a Xamarin.Forms application. In some circumstances the code in this project might require some tailoring for the various platforms, and you’ll see shortly how to do that. You can also include platform-specific code in the five application projects. 

The five application projects have their own assets in the form of icons and metadata, and you must pay particular attention to these assets if you intend to bring the application to market. But during the time that you’re learning how to develop applications using Xamarin.Forms, these assets can generally be ignored. You’ll probably want to keep these application projects collapsed in the solution list be-cause you don’t need to bother much with their contents.

But you really should know what’s in these application projects, so let’s take a closer look.  

In the References section of each application project, you’ll see references to the common PCL project (Hello in this case), as well as various .NET assemblies, the Xamarin.Forms assembles listed above, and additional Xamarin.Forms assemblies applicable to each platform: 

  Xamarin.Forms.Platform.Android
  Xamarin.Forms.Platform.iOS
  Xamarin.Forms.Platform.UAP (not explicitly displayed in the UWP project)
  Xamarin.Forms.Platform.WinRT
  Xamarin.Forms.Platform.WinRT.Tablet
  Xamarin.Forms.Platform.WinRT.Phone 

Each of these libraries defines a static Forms.Init method in the Xamarin.Forms namespace that initializes the Xamarin.Forms system for that particular platform. The startup code in each platform must make a call to this method.

You’ve also just seen that the PCL project derives a public class named App that derives from  Application. The startup code in each platform must also instantiate this App class.   

If you’re familiar with iOS, Android, or Windows Phone development, you might be curious to see how the platform startup code handles these jobs. 

  在Hello项目的Properties中有一个AssemblyInfo.cs类文件用于创建PCL,还有一个packages.config文件包含用于这个项目的NuGet包。在引用当中,可以看到PCL需要的四个最新的类库:

Xamarin.Froms项目中包含的文件

  在开发Xamarin.Forms应用程序时,PCL项目将会花费大部分的精力,在某些情况下,在这个项目中的代码需要根据不同的平台进行调整,并且很快就能看到如何做到这一点,另外也可以在五个不同的平台项目中包含平台相关的代码。

  在这5个应用程序项目中,可以看到一些图标和元数据的资源文件,如果你想将你的应用程序推向市场,那么你需要特别注意这些资源文件。然而,在学习如何使用Xamarin.Froms开发应用程序的时候,可以忽略这些资源文件。

  在每个应用程序项目的引用中,你可以看到对PCL项目(这个示例中就是Hello项目)的引用,当然还包括其他.NET程序集。各个应用程序项目中都有一个静态方法Xamarin.Forms.Forms.Init(),用于初始化Xamarin.Froms到特定的平台。每个项目的启动代码都必须调用这个方法。另外,在每个应用程序项目的启动项代码中,需要实例化的App类。

Xamarin.Froms项目中包含的文件

  如果你熟悉iOS、Android或者Windows Phone的开发,你可能想看一看启动项的代码到底是如何工作的。

iOS项目

An iOS project typically contains a class that derives from UIApplicationDelegate. However, the Xamarin.Forms.Platform.iOS library defines an alternative base class named FormsApplicationDelegate. In the Hello.iOS project, you’ll see this AppDelegate.cs file, here stripped of all extraneous  using directives and comments: 

原文

  在iOS项目中,通常包含一个从UIApplicationDelegate派生的类,但是Xamarin.Forms.iOS类库中定义的是一个替代的基类,叫做FormsApplicationDelegate。在Hello.iOS项目中,你可以这个定义在AppDelegate.cs的文件中,这里去除了多余的代码和注释。

using Foundation;
using UIKit; 

namespace Hello.iOS
{
    [Register("AppDelegate")]
    public partial class AppDelegate :
                            global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
    {
        public override bool FinishedLaunching(UIApplication app, NSDictionary options)
        {
            global::Xamarin.Forms.Forms.Init();
            LoadApplication(new App()); 

            return base.FinishedLaunching(app, options);
        }
    }
} 
The FinishedLaunching override begins by calling the Forms.Init method defined in the Xamarin.Forms.Platform.iOS assembly. It then calls a LoadApplication method (defined by the FormsApplicationDelegate), passing to it a new instance of the App class defined in the Hello namespace in the shared PCL. The page object set to the MainPage property of this App object can then be used to create an object of type UIViewController, which is responsible for rendering the page’s contents.  

原文

  FinishedLaunching方法被重写,使用针对iOS平台的Forms.Init方法,该方法定义在Xamarin.Forms.Platform.iOS程序集当中。接下来调用定义在PCL(Hello项目)中的 LoadApplication方法,传入一个App对象实例作为参数。然后通过调用基类的FinishedLaunching来创建一个展示内容页的对象 UIViewController。

Android项目

In the Android application, the typical MainActivity class must be derived from a Xamarin.Forms class named FormsApplicationActivity, defined in the Xamarin.Forms.Platform.Android assembly, and the Forms.Init call requires some additional information: 

原文

  在Android应用程序中,MainActivity必须从Xamarin.Froms中的FormsApplicationActivity类中派生,Forms.Init方法调用需要一些附加信息。

using Android.App;
using Android.Content.PM;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

namespace Hello.Droid
{
    [Activity(Label = "Hello", Icon = "@drawable/icon", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            global::Xamarin.Forms.Forms.Init(this, bundle);
            LoadApplication(new App());
        }
    }
}
The new instance of the App class in the Hello namespace is then passed to a LoadApplication method defined by FormsApplicationActivity. The attribute set on the MainActivity class indicates that the activity is not recreated when the phone changes orientation (from portrait to landscape or back) or the screen changes size. 

原文

  在公共的Hello项目中定义的App类型的对象,是被FormsApplicationActivity类中的LoadApplication方法所使用。设置在MainActivity类上面的特性,在手机改变定位或者屏幕大小的时候,它们不会被重新创建。

UWP项目

In the UWP project (or either of the two Windows projects), look first in the App.xaml.cs file tucked underneath the App.xaml file in the project file list. In the OnLaunched method you will see the call to Forms.Init using the event arguments:
Xamarin.Forms.Forms.Init(e);
Now look at the MainPage.xaml.cs file tucked underneath the MainPage.xaml file in the project file list. This file defines the customary MainPage class, but it actually derives from a Xamarin.Forms class specified as the root element in the MainPage.xaml file. A newly instantiated App class is passed to the LoadApplication method defined by this base class: 

原文

  在UWP项目中(或者是另外两个Windows项目),在App.xml的cs代码文件中有一个OnLaunched方法,调用了Forms.Init。

       protected override void OnLaunched(LaunchActivatedEventArgs e)
        {

#if DEBUG
            if (System.Diagnostics.Debugger.IsAttached)
            {
                this.DebugSettings.EnableFrameRateCounter = true;
            }
#endif

            Frame rootFrame = Window.Current.Content as Frame;

            // Do not repeat app initialization when the Window already has content,
            // just ensure that the window is active
            if (rootFrame == null)
            {
                // Create a Frame to act as the navigation context and navigate to the first page
                rootFrame = new Frame();

                rootFrame.NavigationFailed += OnNavigationFailed;

                Xamarin.Forms.Forms.Init(e);

                if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
                {
                    //TODO: Load state from previously suspended application
                }

                // Place the frame in the current Window
                Window.Current.Content = rootFrame;
            }

            if (rootFrame.Content == null)
            {
                // When the navigation stack isn't restored navigate to the first page,
                // configuring the new page by passing required information as a navigation
                // parameter
                rootFrame.Navigate(typeof(MainPage), e.Arguments);
            }
            // Ensure the current window is active
            Window.Current.Activate();
        }

  在MainPage.xaml的代码文件中,定义了一个MainPage类,App类的实例对象通过LoadApplication方法调用:

    public sealed partial class MainPage
    {
        public MainPage()
        {
            this.InitializeComponent();

            LoadApplication(new Hello.App());
        }
    }

没什么特别的地方

  如果你通过VisualStudio创建了一个Xamarin.Forms解决方案,并且不想去兼容某个平台,那么可以将这个项目删除掉(比如iOS和8.1)。

  但是如果后来,你有需要添加被删除的项目,可以通过下面步骤进行添加。右击Xamarin.Forms解决方案>添加>添加项目打开添加项目对话框。然后根据需要选择相应的空白项目进行添加(Blank App)。

  添加完新的项目之后,你需要看一下上面提到的Xamarin.Froms代码是如何引用的(包括初始化和引用公共类库),然后进行修改。可以看到,我们单独创建的Android项目或Windows Phone项目和Xamarin.Froms项目没有多大区别,除了对Xamarin.Froms进行的引用。