private View GetOrCreateConent(ITabItem tab) { if (!(Tabs?.Any() == true && ContentTemplate != null)) { return(null); } View element = null; if (!(_tabViews.TryGetValue(tab, out element))) { var selector = ContentTemplate as DataTemplateSelector; if (selector != null) { var template = selector.SelectTemplate(tab, this); element = template?.CreateContent() as View; } else { element = (View)ContentTemplate.CreateContent(); } if (element == null) { throw new InvalidOperationException( "Could not instantiate content. Please make sure that your ContentTemplate is configured correctly."); } _tabViews[tab] = element; element.BindingContext = tab; } return(element); }
/// <summary> /// Page is appeared to screen and animation is finished /// </summary> public virtual void OnAppeared(AppearEventArgs args) { if (IsDebugEnabled) { Debug.WriteLine(NavigationBar.GetTitle(this) + ": OnAppeared"); } LifecycleState = LifecycleStates.Appeared; if (ContentCreateEvent == ContentCreateEvents.Appeared && ContentTemplate != null && Content is View == false && _contentElement == null) { if (_contentElement != null && Children.Contains(_contentElement)) { Children.Remove(_contentElement); } _contentElement = ContentTemplate.CreateContent() as View; Children.Insert(0, _contentElement); if (Content != null) { _contentElement.BindingContext = Content; } } }
/// <summary> /// Called when TabItem is appeared on TabView's viewport /// </summary> public void OnAppeared(CarouselAppearingArgs args) { if (ContentTemplate != null && Content is View == false && _actualContentElement == null && (ContentCreateEvent == ContentCreateEvents.Appeared || (ContentCreateEvent == ContentCreateEvents.ScrollEnded && args.IsScrolling == false))) { _actualContentElement = ContentTemplate.CreateContent() as View; if (Content != null) { _actualContentElement.BindingContext = Content; } if (ContentCreateEvent == ContentCreateEvents.ScrollEnded && ContentAnimation != null) { Animation animation = ContentAnimation.Create(_actualContentElement); EventHandler eventHandler = null; eventHandler = (s, e) => { _actualContentElement.SizeChanged -= eventHandler; Device.BeginInvokeOnMainThread(() => { animation.Commit(this, "ContentCreateAnimationName", 64, (uint)ContentAnimation.Duration); }); }; _actualContentElement.SizeChanged += eventHandler; } Children.Add(_actualContentElement); ContentCreated?.Invoke(this, new EventArgs()); } }
/// <summary> /// Called when properties changes /// </summary> protected override void OnPropertyChanged([CallerMemberName] string propertyName = null) { if (propertyName == ContentProperty.PropertyName) { if (Content is View content) { if (_contentElement != null && Children.Contains(_contentElement)) { Children.Remove(_contentElement); } _contentElement = content; Children.Insert(0, _contentElement); } else if (_contentElement != null) { _contentElement.BindingContext = Content; } } else if (propertyName == ContentTemplateProperty.PropertyName && ContentCreateEvent == ContentCreateEvents.Appearing) { if (ContentTemplate != null && Content is View == false) { if (_contentElement != null && Children.Contains(_contentElement)) { Children.Remove(_contentElement); } _contentElement = ContentTemplate.CreateContent() as View; Children.Insert(0, _contentElement); if (Content != null) { _contentElement.BindingContext = Content; } } } else if (propertyName == FooterProperty.PropertyName) { if (Footer is View footer) { if (_footerElement != null && Children.Contains(_footerElement)) { Children.Remove(_footerElement); } _footerElement = footer; Children.Insert(0, _footerElement); } else if (_footerElement != null) { _footerElement.BindingContext = Footer; } } else if (propertyName == FooterTemplateProperty.PropertyName) { if (FooterTemplate != null && Footer is View == false) { if (_footerElement != null && Children.Contains(_footerElement)) { Children.Remove(_footerElement); } _footerElement = ContentTemplate.CreateContent() as View; Children.Insert(0, _footerElement); if (Footer != null) { _footerElement.BindingContext = Footer; } } } else { base.OnPropertyChanged(propertyName); } }
/// <summary> /// Called when popup IsOpened changes /// </summary> private void OnIsOpenChanged(bool oldValue, bool newValue) { if (PlacementTarget == null || Content == null) { throw new Exception("PlacementTarget is null"); } if (PopupLayout == null) { throw new Exception("PopupLayout is null. Set app popup layout when app start."); } this.AbortAnimation(_openingAnimationName); this.AbortAnimation(_closingAnimationName); if (newValue) { // If actual content is not created if (_actualContent == null) { if (Content is View content) { _actualContent = content; } else if (ContentTemplate != null) { _actualContent = ContentTemplate.CreateContent() as View; if (Content != null) { Binding bind = new Binding("Content"); bind.Source = this; bind.Mode = BindingMode.OneWay; _actualContent.SetBinding(View.BindingContextProperty, bind); } } } if (_popupRootLayout.Content != _actualContent) { _popupRootLayout.Content = _actualContent; } _openPopups.Add(this); InitializeForOpeningAnimation(); SetContentLayoutOptions(Placement); // Create opening animation _openingAnimationGroup = CreateOpeningAnimation(); if (PopupLayout.Children.Contains(_popupRootLayout) == false) { // Add popup to layout PopupLayout.Children.Add(_popupRootLayout); } if (_openingAnimationGroup != null) { _openingAnimationGroup.Commit(this, _openingAnimationName, 64, (uint)OpeningAnimation.Duration, Easing.Linear, (double p, bool isAborted) => { AnimationFinished?.Invoke(this, IsOpen); }); } else { AnimationFinished?.Invoke(this, IsOpen); } IsOpenChanged?.Invoke(this, IsOpen); OnOpened(); } else { _openPopups.Remove(this); // Create closing animation _closingAnimationGroup = CreateClosingAnimation(); if (_closingAnimationGroup != null) { _closingAnimationGroup.Commit(this, _closingAnimationName, 64, (uint)ClosingAnimation.Duration, Easing.Linear, (arg1, arg2) => { if (arg2 == false) { PopupLayout.Children.Remove(_popupRootLayout); } AnimationFinished?.Invoke(this, IsOpen); }); } else { PopupLayout.Children.Remove(_popupRootLayout); AnimationFinished?.Invoke(this, IsOpen); } IsOpenChanged?.Invoke(this, IsOpen); OnClosed(); } }