COMO CRIAR UM APLICATIVO UNIVERSAL WINDOWS PLATFORM CONSUMINDO WEB SERVICE ASP.NET WEB API Professor William Yamamoto www.williamyamamoto.com.br 1) Criar novo Projeto aulauwp no Menu - File - New Project Blank App (Universal Windows) e clicar no botão [OK] 2) Selecionar o Target Version mais NOVO e o Minimum Version mais VELHO e clicar no botão [OK] 1
3) No Solution Explorer clicar com o botão direito do mouse em aulauwp 4) Criar a classe Pessoa.cs selecionando o menu - Add - Class 5) Clicar no botão [Add] 2
6) Digitar na classe Pessoa.cs o seguinte código: using System.ComponentModel.DataAnnotations; namespace aulauwp class Pessoa [Key] public int Id get; set; public string Nome get; set; public string Sobrenome get; set; 7) Digitar no MainPage.xaml o seguinte código: <Page x:class="aulauwp.mainpage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:aulauwp" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:ignorable="d"> <Page.BottomAppBar> <CommandBar> <AppBarButton x:name="adicionarbutton" Icon="Add" Label="Adicionar" Click="adicionarButton_Click"/> </CommandBar> </Page.BottomAppBar> <Grid Background="ThemeResource ApplicationPageBackgroundThemeBrush"> <ListBox x:name="pessoaslista" Margin="0, 0, 0, 50"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Padding="20" Margin="20" Background="DarkCyan"> <TextBlock Text="Binding Nome" FontSize="24" Foreground="White"/> <TextBlock Text="Binding Sobrenome" Foreground="Silver"/> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </Grid> </Page> 3
8) Digitar no MainPage.xaml.cs o seguinte código: using Newtonsoft.Json; using System.Collections.Generic; using System.Net.Http; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Navigation; namespace aulauwp public sealed partial class MainPage : Page public MainPage() this.initializecomponent(); protected override async void OnNavigatedTo(NavigationEventArgs e) HttpClient client = new HttpClient(); var RespostaJson = await client.getstringasync ("http://fcvwebapi.azurewebsites.net/api/pessoas"); var pessoasresultado = JsonConvert.DeserializeObject<List<Pessoa>> (RespostaJson); pessoaslista.itemssource = pessoasresultado; private void adicionarbutton_click(object sender, Windows.UI.Xaml.RoutedEventArgs e) Frame.Navigate(typeof(AdicionarPessoa)); 9) No Solution Explorer clicar com o botão direito em aulauwp menu - Add - New Item 4
10) Selecionar a opção Blank Page, digitar o Name: AdicionarPessoa.xaml e clicar no botão Add 11) Digitar no AdicionarPessoa.xaml o seguinte código: <Page x:class="aulauwp.adicionarpessoa" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:aulauwp" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:ignorable="d"> <Page.BottomAppBar> <CommandBar> <AppBarButton x:name="adicionarbutton" Icon="Add" Label="Adicionar" Click="adicionarButton_Click"/> <AppBarButton x:name="cancelarbutton" Icon="Cancel" Label="Cancelar" Click="cancelarButton_Click"/> </CommandBar> </Page.BottomAppBar> <Grid Background="ThemeResource ApplicationPageBackgroundThemeBrush"> <StackPanel> <TextBox x:name="pessoanometextbox" PlaceholderText="Nome" Margin="20"/> <TextBox x:name="pessoasobrenometextbox" PlaceholderText="Sobrenome" Margin="20,0,20,0"/> </StackPanel> </Grid> </Page> 5
12) Digitar no AdicionarPessoa.xaml.cs o seguinte código: using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Newtonsoft.Json; using System.Net.Http; namespace aulauwp public sealed partial class AdicionarPessoa : Page public AdicionarPessoa() this.initializecomponent(); private async void adicionarbutton_click(object sender, RoutedEventArgs e) var pessoa = new Pessoa() Nome = pessoanometextbox.text, Sobrenome = pessoasobrenometextbox.text ; var pessoajson = JsonConvert.SerializeObject(pessoa); var client = new HttpClient(); var HttpContent = new StringContent(pessoaJson); HttpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue ("application/json"); await client.postasync ("http://fcvwebapi.azurewebsites.net/api/pessoas", HttpContent); private void cancelarbutton_click(object sender, RoutedEventArgs e) Frame.GoBack(); 6