为什么这个'标签'是一个领域?

axb*_*eit 3 c# field properties xamarin xamarin.forms

我目前通过Xamarin书自己工作.在那里你可以看到这个代码:

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);
        }
    }
}
}
Run Code Online (Sandbox Code Playgroud)

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

"相反,事件处理程序访问Label元素(方便地保存为字段)以显示页面的Width和Height属性.Btring.Format调用中的Unicode字符是一个时间(×)符号."

我目前对领域和属性的了解基本上是这样的:

public class ClassName
{
private string field;

public string property {get {return field;} set {field = value;} }
}
Run Code Online (Sandbox Code Playgroud)

我不明白为什么Label元素被保存为字段.它可以保存为其他东西吗?

Pat*_*man 9

不是一个领域.字段是类或结构上的成员.这个标签只是一个局部变量.

这本书错了.

通过将标签的定义移动到类级别,您可以明显地将其变为字段或属性.