




























































































Estude fácil! Tem muito documento disponível na Docsity
Ganhe pontos ajudando outros esrudantes ou compre um plano Premium
Prepare-se para as provas
Estude fácil! Tem muito documento disponível na Docsity
Prepare-se para as provas com trabalhos de outros alunos como você, aqui na Docsity
Encontra documentos específicos para os exames da tua universidade
Prepare-se com as videoaulas e exercícios resolvidos criados a partir da grade da sua Universidade
Responda perguntas de provas passadas e avalie sua preparação.
Ganhe pontos para baixar
Ganhe pontos ajudando outros esrudantes ou compre um plano Premium
Neste documento, aprenderá a criar e manipular diferentes componentes em xamarin.forms usando c#. O documento aborda a criação de labels, botões, entrada de texto e outros elementos básicos, além de expor como criar páginas, manipular eventos e utilizar layouts. O documento também inclui exemplos de código para ajudar no entendimento.
Tipologia: Resumos
1 / 181
Esta página não é visível na pré-visualização
Não perca as partes importantes!





























































































Xamarin.Forms Notes for Professionals
Chapter 1: Getting started with
Xamarin.Forms
Version Release Date 3.0.0 2018-05- 2.5.0 2017-11- 2.4.0 2017-09- 2.3.1 2016-08- 2.3.0-hotfix1 2016-06- 2.3.0 2016-06- 2.2.0-hotfix1 2016-05- 2.2.0 2016-04- 2.1.0 2016-03- 2.0.1 2016-01- 2.0.0 2015-11- 1.5.1 2016-10- 1.5.0 2016-09- 1.4.4 2015-07- 1.4.3 2015-06- 1.4.2 2015-04- 1.4.1 2015-03- 1.4.0 2015-03- 1.3.5 2015-03- 1.3.4 2015-02- 1.3.3 2015-02- 1.3.2 2015-02- 1.3.1 2015-01- 1.3.0 2014-12- 1.2.3 2014-10- 1.2.2 2014-07- 1.2.1 2014-07- 1.2.0 2014-07- 1.1.1 2014-06- 1.1.0 2014-06- 1.0.1 2014-06-
Section 1.1: Installation (Visual Studio)
Xamarin.Forms is a cross-platform natively backed UI toolkit abstraction that allows developers to easily create user interfaces that can be shared across Android, iOS, Windows, and Windows Phone. The user interfaces are rendered using the native controls of the target platform, allowing Xamarin.Forms applications to retain the appropriate look and feel for each platform.
Xamarin Plugin for Visual Studio
To get started with Xamarin.Forms for Visual Studio you need to have the Xamarin plugin itself. The easiest way to have it installed is to download and install the latest Visual Studio.
If you already have the latest Visual Studio installed, go to Control Panel > Programs and Features, right click on Visual Studio, and click Change. When the installer opens, click on Modify, and select the cross-platform mobile development tools:
You can also select to install the Android SDK:
Uncheck it if you already have the SDK installed. You will be able to setup Xamarin to use existing Android SDK later.
Xamarin.Forms
Xamarin.Forms is a set of libraries for your Portable Class library and native assemblies. The Xamarin.Forms library itself is available as a NuGet package. To add it to your project just use the regular Install-Package command of the Package Manager Console:
Install-Package Xamarin.Forms
for all of your initial assemblies (for example MyProject, MyProject.Droid and MyProject.iOS).
The easiest way to get started with Xamarin.Forms is to create an empty project in Visual Studio:
Step 2: Investigating the sample
Having created the solution, a sample application will be ready to be deployed. Open the App.cs located in the root of the portable project and investigate the code. As seen below, the Contents of the sample is a StackLayout which contains a Label:
using Xamarin.Forms;
namespace Hello_World { 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 } } }
Step 3: Launching the application
Now simply right-click the project you want to start (HelloWorld.Droid or HelloWorld.iOS) and click Set as StartUp Project. Then, in the Visual Studio toolbar, click the Start button (the green triangular button that resembles a Play button) to launch the application on the targeted simulator/emulator.
Chapter 3: Xamarin Forms Layouts
Section 3.1: AbsoluteLayout
AbsoluteLayout positions and sizes child elements proportional to its own size and position or by absolute values. Child views may be positioned and sized using proportional values or static values, and proportional and static values can be mixed.
A definition of an AbsoluteLayout in XAML looks like this:
The same layout would look like this in code:
Title = "Absolute Layout Exploration - Code"; var layout = new AbsoluteLayout();
var centerLabel = new Label { Text = "I'm centered on iPhone 4 but no other device.", LineBreakMode = LineBreakMode.WordWrap};
AbsoluteLayout.SetLayoutBounds (centerLabel, new Rectangle (115, 159, 100, 100));
// No need to set layout flags, absolute positioning is the default
var bottomLabel = new Label { Text = "I'm bottom center on every device.", LineBreakMode = LineBreakMode.WordWrap }; AbsoluteLayout.SetLayoutBounds (bottomLabel, new Rectangle (.5, 1, .5, .1)); AbsoluteLayout.SetLayoutFlags (bottomLabel, AbsoluteLayoutFlags.All);
var rightBox = new BoxView{ Color = Color.Olive }; AbsoluteLayout.SetLayoutBounds (rightBox, new Rectangle (1, .5, 25, 100)); AbsoluteLayout.SetLayoutFlags (rightBox, AbsoluteLayoutFlags.PositionProportional);
var leftBox = new BoxView{ Color = Color.Red }; AbsoluteLayout.SetLayoutBounds (leftBox, new Rectangle (0, .5, 25, 100)); AbsoluteLayout.SetLayoutFlags (leftBox, AbsoluteLayoutFlags.PositionProportional);
var topBox = new BoxView{ Color = Color.Blue }; AbsoluteLayout.SetLayoutBounds (topBox, new Rectangle (.5, 0, 100, 25)); AbsoluteLayout.SetLayoutFlags (topBox, AbsoluteLayoutFlags.PositionProportional);
var twoFlagsBox = new BoxView{ Color = Color.Blue }; AbsoluteLayout.SetLayoutBounds (topBox, new Rectangle (.5, 0, 1, 25)); AbsoluteLayout.SetLayoutFlags (topBox, AbsoluteLayoutFlags.PositionProportional | AbsoluteLayout.WidthProportional);
layout.Children.Add (bottomLabel); layout.Children.Add (centerLabel); layout.Children.Add (rightBox); layout.Children.Add (leftBox); layout.Children.Add (topBox);
The AbsoluteLayout control in Xamarin.Forms allows you to specify where exactly on the screen you want the child elements to appear, as well as their size and shape (bounds).
There are a few different ways to set the bounds of the child elements based on the AbsoluteLayoutFlags enumeration that are used during this process. The AbsoluteLayoutFlags enumeration contains the following values:
All : All dimensions are proportional. HeightProportional : Height is proportional to the layout. None : No interpretation is done. PositionProportional : Combines XProportional and YProportional. SizeProportional : Combines WidthProportional and HeightProportional. WidthProportional : Width is proportional to the layout. XProportional : X property is proportional to the layout. YProportional : Y property is proportional to the layout.
The process of working with the layout of the AbsoluteLayout container may seem a little counterintuitive at first, but with a little use it will become familiar. Once you have created your child elements, to set them at an absolute position within the container you will need to follow three steps. You will want to set the flags assigned to the elements using the AbsoluteLayout.SetLayoutFlags() method. You will also want to use the AbsoluteLayout.SetLayoutBounds() method to give the elements their bounds. Finally, you will want to add the child elements to the Children collection. Since Xamarin.Forms is an abstraction layer between Xamarin and the device-specific implementations, the positional values can be independent of the device pixels. This is where the layout flags mentioned previously come into play. You can choose how the layout process of the Xamarin.Forms controls should interpret the values you define.
<--DEFINITIONS...--! >
<ContentView Grid.Row="0" Grid.Column="0" /> <ContentView Grid.Row="1" Grid.Column="0" /> <ContentView Grid.Row="2" Grid.Column="0" />
<ContentView Grid.Row="0" Grid.Column="1" /> <ContentView Grid.Row="1" Grid.Column="1" /> <ContentView Grid.Row="2" Grid.Column="1" />
In C# code:
var grid = new Grid(); //DEFINITIONS... var topLeft = new Label { Text = "Top Left" }; var topRight = new Label { Text = "Top Right" }; var bottomLeft = new Label { Text = "Bottom Left" }; var bottomRight = new Label { Text = "Bottom Right" }; grid.Children.Add(topLeft, 0, 0); grid.Children.Add(topRight, 0, 1); grid.Children.Add(bottomLeft, 1, 0); grid.Children.Add(bottomRight, 1, 1);
For Height and Width a number of units are available.
Auto – automatically sizes to fit content in the row or column. Specified as GridUnitType.Auto in C# or as Auto in XAML. Proportional – sizes columns and rows as a proportion of the remaining space. Specified as a value and GridUnitType.Star in C# and as #* in XAML, with # being your desired value. Specifying one row/column with
Note: The width values for columns are set as Auto by default in Xamarin.Forms, which means that the width is determined from the size of the children. Note that this differs from the implementation of XAML on Microsoft platforms, where the default width is *, which will fill the available space.
Section 3.3: ContentPresenter
A layout manager for templated views. Used within a ControlTemplate to mark where the content to be presented appears.
Section 3.4: ContentView
An element with a single content. ContentView has very little use of its own. Its purpose is to serve as a base class for user-defined compound views.
Code
stack.Children.Add(new Entry());
Section 3.6: Frame
An element containing a single child, with some framing options. Frame have a default Xamarin.Forms.Layout.Padding of 20.
<Label Text="I've been framed!" HorizontalOptions="Center" VerticalOptions="Center" />
Code
var frameView = new Frame { Content = new Label { Text = "I've been framed!", HorizontalOptions = LayoutOptions.Center, VerticalOptions = LayoutOptions.Center }, OutlineColor = Color.Red };
Section 3.7: TemplatedView
An element that displays content with a control template, and the base class for ContentView.
Section 3.8: RelativeLayout
A Layout that uses Constraints to layout its children.
RelativeLayout is used to position and size views relative to properties of the layout or sibling views. Unlike AbsoluteLayout, RelativeLayout does not have the concept of the moving anchor and does not have facilities for positioning elements relative to the bottom or right edges of the layout. RelativeLayout does support positioning elements outside of its own bounds.
A RelativeLayout in XAML, is like this: