Xamarin.Forms Hands On
Hands- On: Xamarin Forms Ziele Kennenlernen von Xamarin.Forms, wich:gste Layouts und Views UI aus Code mit Data Binding Erweitern von Forms Elementen mit Na:ve Custom Renderer UI aus Xaml, Custom View mit XAML Aufrufen von Systemfunk:onen mit DependencyService
Pages hpp://developer.xamarin.com/guides/cross- plasorm/xamarin- forms/controls/pages/
Layouts hpp://developer.xamarin.com/guides/cross- plasorm/xamarin- forms/controls/layouts/
LayoutOp:ons Start Center End Expand WidthRequest HeightRequest
Views hpp://developer.xamarin.com/guides/cross- plasorm/xamarin- forms/controls/views/
UI Gallery Sample App Zeigt Verwendung wich:gster Elemente
App Lifecycle hpp://developer.xamarin.com/guides/cross- plasorm/xamarin- forms/working- with/app- lifecycle/
Demo App New Solu:on Blank Xamarin Forms App PCL Name, id etc. Project Structure Run on ios & Android
Demo App Step 1 New Solu:on Blank Xamarin Forms App PCL Name, id etc. Run on ios & Android
Demo App Step 2 Master Detail Page public class MasterDetail : MasterDetailPage Master: ListView Detail: ContentView Model: Shop ImageSource Image String Name String Descrip:on String URL
Data Binding / Cell Template var shops = new List<Shop>{ new Shop{Image= hpp://, Name= var list = new ListView { ItemsSource = shops, ItemTemplate = new DataTemplate (() => { var imagecell = new ImageCell (); imagecell.setbinding (ImageCell.ImageSourceProperty, "Image"); imagecell.setbinding (ImageCell.TextProperty, "Name"); imagecell.setbinding (ImageCell.DetailProperty, "Descrip:on"); return imagecell; ) ;
Select item / set detail binding context list.itemselected += (sender, e) => { Detail.BindingContext = e.selecteditem; IsPresented = false; ; list.selecteditem = shops [0];
Detail Page Data binding public class ShopPage : ContentPage{ this.setbinding (ContentPage.TitleProperty, "Name"); var shoplabel = new Label { XAlign = TextAlignment.Center ; Content = new StackLayout { Ver:calOp:ons = LayoutOp:ons.FillAndExpand, HorizontalOp:ons = LayoutOp:ons.FillAndExpand, Children = { shoplabel ; shoplabel.setbinding (Label.TextProperty, "Name");
Detail Page: Browser View var browser = new WebView{ Ver:calOp:ons = LayoutOp:ons.FillAndExpand, HorizontalOp:ons = LayoutOp:ons.FillAndExpand ; browser.setbinding (WebView.SourceProperty, "URL"); Navigate Back / Forward: Add BuPon (- > Horizontal StackLayout)
Problem: Reload missing GoForward GoBack Eval(string) Reload? (Na:ve Func:on)
Solu:on: Custom WebView, Renderer Create Class MyWebView : WebView public delegate void ReloadDelegate(); public ReloadDelegate Reload;
Android Renderer public class MyWebViewRenderer : WebViewRenderer { protected override void OnElementChanged (ElementChangedEventArgs< WebView> e) { base.onelementchanged (e); if (e.oldelement!= null) { ((MyWebView)e.OldElement).Reload - = DoReload; ((MyWebView)e.NewElement).Reload += DoReload; void DoReload(){ Control.Reload ();
ios Renderer public class MyWebViewRenderer : WebViewRenderer { protected override void OnElementChanged (VisualElementChangedEventArgs e) { base.onelementchanged (e); if (e.oldelement!= null) { ((MyWebView)e.OldElement).Reload - = DoReload; ((MyWebView)e.NewElement).Reload += DoReload; void DoReload(){ Reload ();
Magic using xyz; [assembly: ExportRenderer ( typeof (MyWebView), typeof (MyWebViewRenderer))] Namespace xyz{ public class MyWebViewRenderer
XAML View Create new Xaml View (ShopPage2) <?xml version="1.0" encoding="utf- 8"?> <ContentPage xmlns="hpp://xamarin.com/ schemas/2014/forms" xmlns:x="hpp://schemas.microsor.com/winfx/ 2009/xaml" x:class="shops.shoppage2 > <ContentPage.Content>. </ContentPage.Content> </ContentPage>
Xaml Content <StackLayout HorizontalOp:ons="FillAndExpand" Ver:calOp:ons="FillAndExpand"> <WebView x:name="browser Source="{Binding URL" HorizontalOp:ons="FillAndExpand" Ver:calOp:ons="FillAndExpand"/> <StackLayout Orienta:on="Horizontal"> <BuPon Text="Back" Clicked="BackClicked"/> <BuPon Text="Reload" Clicked="ReloadClicked"/> </StackLayout> </StackLayout>
CodeBehind Class public par:al class ShopPage2 : ContentPage { public ShopPage2 () { Ini:alizeComponent (); void BackClicked(object sender, EventArgs e){ browser.goback (); void ReloadClicked(object sender, EventArgs e){ browser.reload ();
Beispiel App Master- Detail auf ipad Quer
DependencyService Access Na:ve func:onality from shared code Declare Interface: public interface IToastService { void ShowToast(string text);
Implementa:on Android public class ToastService : IToastService { public ToastService () { public void ShowToast(string text){ var ctx = Forms.Context; Toast toast = Toast.MakeText(Forms.Context, text, ToastLength.Long); toast.show();
Implementa:on ios public async void ShowToast(string text){ var alert = new UIAlertView(text, null, null, null, null); alert.show (); await Task.Delay(3000); alert.dismisswithclickedbuponindex (- 1, true);
Magic Using xyz; [assembly: Xamarin.Forms.Dependency (typeof (ToastService))] Namespace xyz{ class.
Usage var service = DependencyService.Get<IToastService> (); service.showtoast("hallo Workshop"); APen:on: only one instance!
PlaSorm Tweaks hpp://developer.xamarin.com/guides/cross- plasorm/xamarin- forms/working- with/ plasorm- specifics/