-
Notifications
You must be signed in to change notification settings - Fork 1
Lesson3
Go to File -> New -> New Project
Select Cross-Platform
Select Mobile App(Xamarin.Forms)
You should see something like this in Solution Explorer.
It is fine if you don't have the UWP project, although it is recommended to have, because building and running UWP is fastest, but Windows only.
Extensible Application Markup Language (XAML) is a declarative XML-based language developed by Microsoft that is used for initializing structured values and objects.
Read through the tutorial here: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/xaml/xaml-basics/
You can look through all the possible options when looking through Visual Studio's suggestions, as seen below.
You can preview your UI when you are doing the design
Considered a container. Able to keep adding elements, will order elements sequentially.
Example of 2 Labels inside a StackLayout
<StackLayout>
<Label Text="Welcome to Xamarin.Forms!"
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand" />
<Label Text="HELLO"
FontSize="Large"
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand" />
</StackLayout>
Also another container. Much more controllable than StackLayout, can adjust rows and heights based on screen size, absolute size, or child element size.
Only way to add row is to add RowDefinition, column then ColumnDefinition Example of a 3x3 Grid
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="100" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="100" />
</Grid.ColumnDefinitions>
...
</Grid>
Resizes based on child element in the row/column, i.e. if child element is 60px wide then the row will be 60px wide
Takes up the rest of the space in the row/column
Takes up number of pixels according to the specified number
<Grid.RowDefinitions>
<RowDefinition Height="3*" />
<RowDefinition Height="*" />
<RowDefinition Height="100" />
</Grid.RowDefinitions>
In this case 100 pixels will be first allocated to the 3rd row. Then for the remaining space, 25% goes to 1st row, 75% goes to 2nd row
XAML uses 0 indexing, meaning the example below is assigned to the cell in row 2 column 2.
<Entry Grid.Row="1" Grid.Column="1"
Text="{Binding StudentId}"/>
Design something like this using grid
Name it MainViewModel
i) Make the class public
ii) Add a few public properties
public class MainViewModel
{
public string Name { get; set; }
public string StudentId { get; set; }
public string Email { get; set; }
}
Note: It has to be properties for the binding to work, property has the "get set" while fields don't
Go to MainPage.xaml.cs
In the constructor of MainPage, instantiate a MainViewModel object, and assign it to the BindingContext property
public MainPage()
{
InitializeComponent();
BindingContext = new MainViewModel();
}
<Entry Grid.Row="0" Grid.Column="1"
Text="{Binding Name}"/>
In MainViewModel.cs Add a function to print data e.g.
private void Print()
{
Debug.WriteLine($"Name: {Name}");
Debug.WriteLine($"StudentId: {StudentId}");
Debug.WriteLine($"Email: {Email}");
}
This is called a Command, can be used to bind to a button. You cannot bind a function to a button. Also in MainViewModel.cs
public ICommand ClickCommand {
get { return new Xamarin.Forms.Command(Print); }
}
<Button Text="Submit"
Command="{Binding ClickCommand}"/>
For an android app that looks like this
Press the submit button.
Open the output window
Data binding works now
The Model-View-ViewModel (MVVM) architectural pattern was invented with XAML in mind. The pattern enforces a separation between three software layers.
- the XAML user interface, called the View
- Underlying data model, called the Model
- An intermediary between the View and the Model, called the ViewModel
The View and the ViewModel are often connected through data bindings defined in the XAML file. The BindingContext for the View is usually an instance of the ViewModel
Note that there is also another very important layer, called Services. This is where most of the logic is handled, such as sending data to server or reading data from server.
In a very pure MVVM context, the ViewModel is only in charge of handling any data that directly deals with the UI, it has no responsibility on anything else, such as data processing. Moreover, in this context, it is necessary for the ViewModel to be able to run in a headless mode (i.e. without UI), such that UI logic can be tested without wasting time launching UI.