Advertisement
SkysRad

Xamairn Forms UWP RaiseLower Child Issue

Feb 9th, 2017
446
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.52 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using Xamarin.Forms;
  5.  
  6. namespace Xam.Plugin.ETL
  7. {
  8.     public class ExpandableTabLayout : Layout<View>
  9.     {
  10.  
  11.         public static readonly BindableProperty TabCountProperty = BindableProperty.Create(nameof(TabCount), typeof(int), typeof(ExpandableTabLayout), 0, propertyChanged: OnTabCountChanged);
  12.         public static readonly BindableProperty FocusedViewProperty = BindableProperty.Create(nameof(FocusedView), typeof(View), typeof(ExpandableTabLayout), null, propertyChanged: OnFocusedViewChanged);
  13.  
  14.         private static void OnFocusedViewChanged(BindableObject bindable, object oldValue, object newValue)
  15.         {
  16.  
  17.         }
  18.  
  19.         private static void OnTabCountChanged(BindableObject bindable, object oldValue, object newValue)
  20.         {
  21.             // Handle animations and applying the new tab
  22.             var self = (ExpandableTabLayout) bindable;
  23.             self.InvalidateMeasure();
  24.         }
  25.  
  26.         public View FocusedView
  27.         {
  28.             get { return (View)GetValue(FocusedViewProperty); }
  29.             set { SetValue(FocusedViewProperty, value); }
  30.         }
  31.  
  32.         public int TabCount
  33.         {
  34.             get { return (int) GetValue(TabCountProperty); }
  35.             set { SetValue(TabCountProperty, value); }
  36.         }
  37.  
  38.         private bool TabsDisplayed = false;
  39.  
  40.         protected override void OnChildAdded(Element child)
  41.         {
  42.             if (FocusedView == null)
  43.                 FocusedView = child as View;
  44.             base.OnChildAdded(child);
  45.         }
  46.  
  47.         protected override void LayoutChildren(double x, double y, double width, double height)
  48.         {
  49.             SortStack();
  50.             if (!TabsDisplayed)
  51.             {
  52.                 foreach (var child in Children)
  53.                 {
  54.                     if (child != FocusedView)
  55.                         child.InputTransparent = true;
  56.                     LayoutChildIntoBoundingRegion(child, new Rectangle(x, y, width, height));
  57.                 }
  58.             }
  59.         }
  60.  
  61.         /// <summary>
  62.         /// Sorts the stack based on the FocusedView
  63.         /// </summary>
  64.         private void SortStack()
  65.         {
  66.             for (int i = 0; i != Children.Count; i++)
  67.             {
  68.                 if (Children[i] != FocusedView)
  69.                     LowerChild(Children[i]);
  70.             }
  71.  
  72.             RaiseChild(FocusedView);
  73.         }
  74.  
  75.         public void ToggleTabView()
  76.         {
  77.             TabsDisplayed = !TabsDisplayed;
  78.  
  79.             if (TabsDisplayed)
  80.                 DisplayTabs();
  81.             else
  82.                 HideTabs();
  83.         }
  84.  
  85.         private async void HideTabs()
  86.         {
  87.             // Hide unselected first
  88.             foreach (var child in Children)
  89.             {
  90.                 if (child != FocusedView)
  91.                 {
  92.  
  93.                 }
  94.             }
  95.  
  96.             // Display Selected
  97.             await FocusedView.ScaleTo(1, 500, Easing.SpringIn);
  98.  
  99.             // Place unselected into normal scale for later
  100.             foreach (var child in Children)
  101.             {
  102.                 if (child != FocusedView)
  103.                 {
  104.                     child.Scale = 1;
  105.                 }
  106.             }
  107.  
  108.             // Invalidate and redraw
  109.             InvalidateLayout();
  110.         }
  111.  
  112.         private void DisplayTabs()
  113.         {
  114.             foreach (var child in Children)
  115.             {
  116.                 child.ScaleTo(0.75, 500, Easing.SpringOut);
  117.             }
  118.         }
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement