为什么这个 'Label' 是一个字段?

Why is this 'Label' a field?

我目前正在通过 Xamarin Book 工作。在那里你可以看到这个代码:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Xamarin.Forms;

namespace BookCode

{
    public class Greetings : ContentPage
{
    public Greetings()
    {
        Label label;

        label = new Label
        {
            FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label)),
            HorizontalOptions = LayoutOptions.Center,
            VerticalOptions = LayoutOptions.Center
            };

        Content = label;

           SizeChanged += OnPageSizeChanged;


        void OnPageSizeChanged(object sender, EventArgs args)
        {
            label.Text = String.Format("{0} \u00D7 {1}", Width, Height);
        }
    }
}
}

在代码的解释中,您可以阅读以下内容:

"Instead, the event handler accesses the Label element (conveniently saved as a field) to display the Width and Height properties of the page. The Unicode character in the String.Format call is a times (×) symbol."

我目前对字段和属性的认识基本是这样的:

public class ClassName
{
private string field;

public string property {get {return field;} set {field = value;} }
}

我不明白为什么Label元素被保存为一个字段。可以另存为吗?

不是一个字段。字段为 members on a class or struct。这个标签只是一个局部变量。

书错了

您可以将标签的定义移动到 class 级别,使其成为字段或 属性。