6

Skończyłem stronę główną mojej aplikacji w Xamarin.Forms Portable.Ruchomy przycisk akcji w Xamarin.Forms

Teraz chcę dodać przycisk akcji flotacji w moim projekcie Android!

Czy istnieje sposób, aby dodać FAB dla Androida na mojej istniejącej stronie głównej, która została zakodowana w Xamarin.Forms Portable.

LUB

Chcę utworzyć oddzielną stronę główną dla Androida i dodać nazwać jako tytułowa dla android?

Dzięki i Pozdrawiam.

+0

To zależy od tego, co chcesz dokładnie. Czy chcesz to tylko w swoim projekcie Android? Lub w innych Twoich aplikacjach? Czy masz jeszcze inne aplikacje? Jeśli tak, i po prostu chcesz go w swoim projekcie Android, prawdopodobnie chcesz "CustomRenderer". Spójrz na to; https://components.xamarin.com/gettingstarted/fab –

Odpowiedz

3

Budowanie Custom Control Dla właściwości Fab, aby być Bindable w Xamarin.Forms musimy niestandardowego formantu z Bindable nieruchomości.

public class FloatingActionButtonView : View 
{ 
    public static readonly BindableProperty ImageNameProperty = BindableProperty.Create<FloatingActionButtonView,string>(p => p.ImageName, string.Empty); 
    public string ImageName 
    { 
     get { return (string)GetValue (ImageNameProperty); } 
     set { SetValue (ImageNameProperty, value); } 
    } 

    public static readonly BindableProperty ColorNormalProperty = BindableProperty.Create<FloatingActionButtonView,Color>(p => p.ColorNormal, Color.White); 
    public Color ColorNormal 
    { 
     get { return (Color)GetValue (ColorNormalProperty); } 
     set { SetValue (ColorNormalProperty, value); } 
    } 

    public static readonly BindableProperty ColorPressedProperty = BindableProperty.Create<FloatingActionButtonView,Color>(p => p.ColorPressed, Color.White); 
    public Color ColorPressed 
    { 
     get { return (Color)GetValue (ColorPressedProperty); } 
     set { SetValue (ColorPressedProperty, value); } 
    } 

    public static readonly BindableProperty ColorRippleProperty = BindableProperty.Create<FloatingActionButtonView,Color>(p => p.ColorRipple, Color.White); 
    public Color ColorRipple 
    { 
     get { return (Color)GetValue (ColorRippleProperty); } 
     set { SetValue (ColorRippleProperty, value); } 
    } 
    ... 
} 

Następnie zmapujemy każdą właściwość do odpowiedniej właściwości w natywnym kontrole FAB.

załączyć Renderer

Jeśli chcemy użyć natywną kontrolę Xamarin.Forms musimy renderujący. Dla uproszczenia, użyjmy ViewRenderer. Ten renderer odwzoruje nasz zwyczaj FloatingActionButtonView na Android.Widget.FrameLayout.

public class FloatingActionButtonViewRenderer : ViewRenderer<FloatingActionButtonView, FrameLayout> 
{ 
    ... 
    private readonly Android.Content.Context context; 
    private readonly FloatingActionButton fab; 

    public FloatingActionButtonViewRenderer() 
    { 
     context = Xamarin.Forms.Forms.Context; 
     fab = new FloatingActionButton(context); 
     ... 
    } 

    protected override void OnElementChanged(ElementChangedEventArgs<FloatingActionButtonView> e) 
    { 
     base.OnElementChanged(e); 

     if (e.OldElement != null || this.Element == null) 
      return; 

     if (e.OldElement != null) 
      e.OldElement.PropertyChanged -= HandlePropertyChanged; 

     if (this.Element != null) { 
      //UpdateContent(); 
      this.Element.PropertyChanged += HandlePropertyChanged; 
     } 

     Element.Show = Show; 
     Element.Hide = Hide; 

     SetFabImage(Element.ImageName); 

     fab.ColorNormal = Element.ColorNormal.ToAndroid(); 
     fab.ColorPressed = Element.ColorPressed.ToAndroid(); 
     fab.ColorRipple = Element.ColorRipple.ToAndroid(); 

     var frame = new FrameLayout(Forms.Context); 
     frame.RemoveAllViews(); 
     frame.AddView(fab); 

     SetNativeControl (frame); 
    } 

    public void Show(bool animate = true) 
    { 
     fab.Show(animate); 
    } 

    public void Hide(bool animate = true) 
    { 
     fab.Hide(animate); 
    } 

    void HandlePropertyChanged (object sender, System.ComponentModel.PropertyChangedEventArgs e) 
    { 
     if (e.PropertyName == "Content") { 
      Tracker.UpdateLayout(); 
     } 
     else if (e.PropertyName == FloatingActionButtonView.ColorNormalProperty.PropertyName) 
     { 
      fab.ColorNormal = Element.ColorNormal.ToAndroid(); 
     } 
     else if (e.PropertyName == FloatingActionButtonView.ColorPressedProperty.PropertyName) 
     { 
      fab.ColorPressed = Element.ColorPressed.ToAndroid(); 
     } 
     else if (e.PropertyName == FloatingActionButtonView.ColorRippleProperty.PropertyName) 
     { 
      fab.ColorRipple = Element.ColorRipple.ToAndroid(); 
     } 
     ... 
    } 

    void SetFabImage(string imageName) 
    { 
     if(!string.IsNullOrWhiteSpace(imageName)) 
     { 
      try 
      { 
       var drawableNameWithoutExtension = Path.GetFileNameWithoutExtension(imageName); 
       var resources = context.Resources; 
       var imageResourceName = resources.GetIdentifier(drawableNameWithoutExtension, "drawable", context.PackageName); 
       fab.SetImageBitmap(Android.Graphics.BitmapFactory.DecodeResource(context.Resources, imageResourceName)); 
      } 
      catch(Exception ex) 
      { 
       throw new FileNotFoundException("There was no Android Drawable by that name.", ex); 
      } 
     } 
    } 
} 

Pull to wszystko razem

OK! Zbudowaliśmy niestandardowy formant i zmapowaliśmy go do renderera. Ostatnim krokiem jest wytyczenie kontroli w naszym przekonaniu.

public class MainPage : ContentPage 
{ 
    public MainPage() 
    { 
     var fab = new FloatingActionButtonView() { 
      ImageName = "ic_add.png", 
      ColorNormal = Color.FromHex("ff3498db"), 
      ColorPressed = Color.Black, 
      ColorRipple = Color.FromHex("ff3498db") 
     }; 

     // Main page layout 
     var pageLayout = new StackLayout { 
      Children = 
      { 
       new Label { 
        VerticalOptions = LayoutOptions.CenterAndExpand, 
        XAlign = TextAlignment.Center, 
        Text = "Welcome to Xamarin Forms!" 
       } 
      }}; 

     var absolute = new AbsoluteLayout() { 
      VerticalOptions = LayoutOptions.FillAndExpand, 
      HorizontalOptions = LayoutOptions.FillAndExpand }; 

     // Position the pageLayout to fill the entire screen. 
     // Manage positioning of child elements on the page by editing the pageLayout. 
     AbsoluteLayout.SetLayoutFlags(pageLayout, AbsoluteLayoutFlags.All); 
     AbsoluteLayout.SetLayoutBounds(pageLayout, new Rectangle(0f, 0f, 1f, 1f)); 
     absolute.Children.Add(pageLayout); 

     // Overlay the FAB in the bottom-right corner 
     AbsoluteLayout.SetLayoutFlags(fab, AbsoluteLayoutFlags.PositionProportional); 
     AbsoluteLayout.SetLayoutBounds(fab, new Rectangle(1f, 1f, AbsoluteLayout.AutoSize, AbsoluteLayout.AutoSize)); 
     absolute.Children.Add(fab); 

     Content = absolute; 
    } 
} 

Kompletny kod na Github: Floating Action Button Xamarin.Forms

+0

Visual Studio informuje mnie, że ogólna wersja BindableProperty.Create jest przestarzała i zostanie usunięta. Jak rozwiązać ten problem w aktualny, prawidłowy sposób? – zuckerthoben

+0

@zuckerthoben musisz zmienić BindableProperty.Create () na: 'public static readonly BindableProperty TheProperty = BindableProperty.Create (propertyName:" ThePropertyName ", returnType: typeof (" string/Color etc .. ")), declarationingType: typeof ("ClassName"), defaultValue: "Wartość domyślna"); ' – MalokuS

+0

Czy jest to teraz przestarzałe, ponieważ istnieją oficjalne biblioteki Support? – zuckerthoben

Powiązane problemy