Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
[UWP] Fix crash app when create navigation/tabbed page without childr…
Browse files Browse the repository at this point in the history
…en pages fixes #3188 (#3514)
  • Loading branch information
Pavel Yakovlev authored and rmarinho committed Aug 23, 2018
1 parent 56d47a1 commit 57fd1de
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 66 deletions.
12 changes: 3 additions & 9 deletions Xamarin.Forms.Platform.UAP/FormsPresenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public FormsPresenter()
Unloaded += FormsPresenter_Unloaded;
SizeChanged += (s, e) =>
{
if (ActualWidth > 0 && ActualHeight > 0)
if (ActualWidth > 0 && ActualHeight > 0 && DataContext != null)
{
var page = (Page)DataContext;
((Page)page.RealParent).ContainerArea = new Rectangle(0, 0, ActualWidth, ActualHeight);
Expand All @@ -21,15 +21,9 @@ public FormsPresenter()
}

void FormsPresenter_Loaded(object sender, RoutedEventArgs e)
{
var page = (Page)DataContext;
page.SendAppearing();
}
=> (DataContext as Page)?.SendAppearing();

void FormsPresenter_Unloaded(object sender, RoutedEventArgs e)
{
var page = (Page)DataContext;
page.SendDisappearing();
}
=> (DataContext as Page)?.SendDisappearing();
}
}
2 changes: 1 addition & 1 deletion Xamarin.Forms.Platform.UAP/NavigationPageRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ static object GetDefaultColor()

void UpdateBackButton()
{
if (_navManager == null)
if (_navManager == null || _currentPage == null)
{
return;
}
Expand Down
114 changes: 58 additions & 56 deletions Xamarin.Forms.Platform.UAP/TabbedPageRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -408,85 +408,87 @@ void UpdateCurrentPage()
Control.SelectedItem = page;
}

void UpdateBarIcons() {
if (Control == null)
void UpdateBarIcons()
{
if (Control == null || Element == null)
return;

if (Element.IsSet(Specifics.HeaderIconsEnabledProperty))
{
bool headerIconsEnabled = Element.OnThisPlatform().GetHeaderIconsEnabled();
bool invalidateMeasure = false;
if (!Element.IsSet(Specifics.HeaderIconsEnabledProperty))
return;

// Get all stack panels affected by update.
var stackPanels = Control.GetDescendantsByName<WStackPanel>(TabBarHeaderStackPanelName);
foreach (var stackPanel in stackPanels)
bool headerIconsEnabled = Element.OnThisPlatform().GetHeaderIconsEnabled();
bool invalidateMeasure = false;

// Get all stack panels affected by update.
var stackPanels = Control.GetDescendantsByName<WStackPanel>(TabBarHeaderStackPanelName);
foreach (var stackPanel in stackPanels)
{
int stackPanelChildCount = stackPanel.Children.Count;
for (int i = 0; i < stackPanelChildCount; i++)
{
int stackPanelChildCount = stackPanel.Children.Count;
for (int i = 0; i < stackPanelChildCount; i++)
var stackPanelItem = stackPanel.Children[i];
if (stackPanelItem is WImage tabBarImage)
{
var stackPanelItem = stackPanel.Children[i];
if (stackPanelItem is WImage tabBarImage)
// Update icon image.
if (tabBarImage.GetValue(FrameworkElement.NameProperty).ToString() == TabBarHeaderImageName)
{
// Update icon image.
if (tabBarImage.GetValue(FrameworkElement.NameProperty).ToString() == TabBarHeaderImageName)
if (headerIconsEnabled)
{
if (headerIconsEnabled)
if (Element.IsSet(Specifics.HeaderIconsSizeProperty))
{
if (Element.IsSet(Specifics.HeaderIconsSizeProperty))
{
Size iconSize = Element.OnThisPlatform().GetHeaderIconsSize();
tabBarImage.Height = iconSize.Height;
tabBarImage.Width = iconSize.Width;
}
tabBarImage.HorizontalAlignment = WHorizontalAlignment.Center;
tabBarImage.Visibility = WVisibility.Visible;
Size iconSize = Element.OnThisPlatform().GetHeaderIconsSize();
tabBarImage.Height = iconSize.Height;
tabBarImage.Width = iconSize.Width;
}
else
{
tabBarImage.Visibility = WVisibility.Collapsed;
}

invalidateMeasure = true;
tabBarImage.HorizontalAlignment = WHorizontalAlignment.Center;
tabBarImage.Visibility = WVisibility.Visible;
}
else
{
tabBarImage.Visibility = WVisibility.Collapsed;
}

invalidateMeasure = true;
}
else if (stackPanelItem is WTextBlock tabBarTextblock)
}
else if (stackPanelItem is WTextBlock tabBarTextblock)
{
// Update text block.
if (tabBarTextblock.GetValue(FrameworkElement.NameProperty).ToString() == TabBarHeaderTextBlockName)
{
// Update text block.
if (tabBarTextblock.GetValue(FrameworkElement.NameProperty).ToString() == TabBarHeaderTextBlockName)
if (headerIconsEnabled)
{
if (headerIconsEnabled)
// Remember old values so we can restore them if icons are collapsed.
// NOTE, since all Textblock instances in this stack panel comes from the same
// style, we just keep one copy of the value (since they should be identical).
if (tabBarTextblock.TextAlignment != WTextAlignment.Center)
{
// Remember old values so we can restore them if icons are collapsed.
// NOTE, since all Textblock instances in this stack panel comes from the same
// style, we just keep one copy of the value (since they should be identical).
if (tabBarTextblock.TextAlignment != WTextAlignment.Center)
{
_oldBarTextBlockTextAlignment = tabBarTextblock.TextAlignment;
tabBarTextblock.TextAlignment = WTextAlignment.Center;
}

if (tabBarTextblock.HorizontalAlignment != WHorizontalAlignment.Center)
{
_oldBarTextBlockHorinzontalAlignment = tabBarTextblock.HorizontalAlignment;
tabBarTextblock.HorizontalAlignment = WHorizontalAlignment.Center;
}
_oldBarTextBlockTextAlignment = tabBarTextblock.TextAlignment;
tabBarTextblock.TextAlignment = WTextAlignment.Center;
}
else

if (tabBarTextblock.HorizontalAlignment != WHorizontalAlignment.Center)
{
// Restore old values.
tabBarTextblock.TextAlignment = _oldBarTextBlockTextAlignment;
tabBarTextblock.HorizontalAlignment = _oldBarTextBlockHorinzontalAlignment;
_oldBarTextBlockHorinzontalAlignment = tabBarTextblock.HorizontalAlignment;
tabBarTextblock.HorizontalAlignment = WHorizontalAlignment.Center;
}
}
else
{
// Restore old values.
tabBarTextblock.TextAlignment = _oldBarTextBlockTextAlignment;
tabBarTextblock.HorizontalAlignment = _oldBarTextBlockHorinzontalAlignment;
}
}
}
}

// If items have been made visible or collapsed in panel, invalidate current control measures.
if (invalidateMeasure)
Control.InvalidateMeasure();
}

// If items have been made visible or collapsed in panel, invalidate current control measures.
if (invalidateMeasure)
Control.InvalidateMeasure();
}

void UpdateToolbarPlacement()
{
Control.ToolbarPlacement = Element.OnThisPlatform().GetToolbarPlacement();
Expand Down

0 comments on commit 57fd1de

Please sign in to comment.