TDC2017 - POA - Aprendendo a usar Xamarin para desenvolver aplicações moveis multi plataforma
-
Upload
rodrigo-urubatan -
Category
Technology
-
view
58 -
download
3
Transcript of TDC2017 - POA - Aprendendo a usar Xamarin para desenvolver aplicações moveis multi plataforma
Globalcode – Open4education
Aprendendo a usar Xamarin para desenvolver
aplicações moveis multi plataforma
Rodrigo Urubatan
http://www.urubatan.com.br - @urubatan – [email protected]
Globalcode – Open4education
Rodrigo Urubatan F. Jardim
• http://urubatan.com.br
• http://github.com/urubatan
• http://twitter.com/urubatan
• http://linkedin.com/in/urubatan
Globalcode – Open4education
Meu problema
Distribuição de noticias para clients por email
Difícil de ter certeza de quem leu cada notícia
Difícil de agregar mais valor a cada noticia
entregue
Difícil de traçar o perfil dos clients, tempo de leitura por assunto, …
Os clientes usam plataformas diferentes,
e desenvolvimento mobile não é o foco
principal da empresa
Globalcode – Open4education
Globalcode – Open4education
Um código
para todas as
plataformas!!!
Globalcode – Open4education
Xamarin não é perfeito
Globalcode – Open4education
Xamarin.Forms
Globalcode – Open4education
Ok, mas vai ficar só falando?
Globalcode – Open4education
RestClientthis.client = new
RestClient("http://address:port/");
Globalcode – Open4education
Login?
public async Task<User> RestLogin(string Username, string Password)
{
try
{
var request = new RestRequest("sessions.json", Method.POST);
request.AddJsonBody(new { session = new { username = Username, password = Password } });
var response = await client.Execute(request);
if (response.IsSuccess)
{
Application.Current.Properties["Password"] = Password;
Application.Current.Properties["Username"] = Username;
Application.Current.Properties["LoggedIn"] = "True";
await Application.Current.SavePropertiesAsync();
request = new RestRequest("sessions.json", Method.GET);
var response2 = await client.Execute<User>(request);
if (response2.IsSuccess)
{
var user = response2.Data;
return user;
}else{
return null;
}
}
else
{
return null;
}
}
catch (Exception)
{
return null;
}
}
Globalcode – Open4education
Mas não
funcionou!!
Todas as requisições REST são por definição
stateless
O protocolo HTTP tem um “contorno”
para isto, com “Cookies”
As plataformas WEB usam estes cookies
para manter o estado entre as
requisições
Globalcode – Open4education
Adicionando suporte a
Cookies ao nosso RestClient
this.client = newRestClient("http://reader.brightwire.com/");
var cookieContainer = newSystem.Net.CookieContainer();
client.CookieContainer = cookieContainer;
Globalcode – Open4education
Mapeando Objetos
public class Article{
public string Headline { get; set; }public string Body { get; set; }
}
public async Task<Article> ListArticles(){var request = new RestRequest("articles.json", Method.GET);var resp = await client.Execute<Article>(request);var storyList = resp.Data;return storyList;
}
Globalcode – Open4education
Mostrando Dados
<?xml version="1.0" encoding="utf-8" ?><ContentPagexmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:tdc2017poa_xam"
x:Class="tdc2017poa_xam.MainPage"><ContentPage.Content>
<StackLayout><Label Text="Stories List"
VerticalOptions="Start"HorizontalOptions="CenterAndExpand"
/><ListView x:Name="ArticlesList">
<ListView.ItemTemplate><DataTemplate>
<TextCell Text="{BindingHeadline}" />
</DataTemplate></ListView.ItemTemplate>
</ListView></StackLayout>
</ContentPage.Content></ContentPage>
namespace tdc2017poa_xam{public partial class MainPage : ContentPage{
private ReaderRestAPI clientApi;public MainPage(ReaderRestAPI clientApi){
this.clientApi = clientApi;InitializeComponent();Task.Factory.StartNew(async () =>{
var articles = awaitclientApi.ListArticles();
Device.BeginInvokeOnMainThread(() =>{
this.ArticlesList.ItemsSource = articles;
});});
}}}
Globalcode – Open4education
Ainda não funciona?
protect_from_forgery é um
problema, Podemos fazer uma
requisição e enviar o cabeçalho,
ou simplesmente desligar isto
para o exemplo
Globalcode – Open4education
Mostrando
detalhes!
MainPage = new NavigationPage(new tdc2017poa_xam.MainPage(this.clientApi));
Navigation.PushAsync(new ArticlePage(this.clientApi, (e.SelectedItem as Article).Id));
Globalcode – Open4education
Pontos importantes!!
Device.BeginInvokeOnMainThread
Task.Factory.StartNew
async/await
Application.Current.Properties/Application.Current.
SavePropertiesAsync/Application.Current.Propertie
s.ContainsKey
Globalcode – Open4education
Notificações? Microsoft.Azure.Mobile.Client
Globalcode – Open4education
E agora?
Globalcode – Open4education