Im currently working myself trough a Xamarin Book. There you can see this Code:
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);
}
}
}
}
And in an explanation of the code you can read this:
"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."
My current knowledge of fields and properties is basically this:
public class ClassName
{
private string field;
public string property {get {return field;} set {field = value;} }
}
I dont understand why the Label element is saved as a field. Could it be saved as something else?
It is not a field. Fields are members on a class or struct. This label is just a local variable.
The book is wrong.
You can make it a field or property obviously by moving the definition of the label to the class level.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With