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

Commit

Permalink
Fixed Issue10679, avoid NRE accessing a disposed SwipeView (#10682)
Browse files Browse the repository at this point in the history
Co-authored-by: Rui Marinho <me@ruimarinho.net>
  • Loading branch information
jsuarezruiz and rmarinho committed Aug 18, 2020
1 parent 1d46ce9 commit f5575fb
Show file tree
Hide file tree
Showing 3 changed files with 187 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="utf-8" ?>
<local:TestShell xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:local="using:Xamarin.Forms.Controls"
xmlns:issues="using:Xamarin.Forms.Controls.Issues"
x:Class="Xamarin.Forms.Controls.Issues.Issue10679">
<Shell.Resources>
<ResourceDictionary>

<Color x:Key="NavigationPrimary">#2196F3</Color>

<Style x:Key="BaseStyle"
TargetType="Element">
<Setter Property="Shell.BackgroundColor" Value="{StaticResource NavigationPrimary}" />
<Setter Property="Shell.ForegroundColor" Value="White" />
<Setter Property="Shell.TitleColor" Value="White" />
<Setter Property="Shell.DisabledColor" Value="#B4FFFFFF" />
<Setter Property="Shell.UnselectedColor" Value="#95FFFFFF" />
<Setter Property="Shell.TabBarBackgroundColor" Value="{StaticResource NavigationPrimary}" />
<Setter Property="Shell.TabBarForegroundColor" Value="White" />
<Setter Property="Shell.TabBarUnselectedColor" Value="#95FFFFFF" />
<Setter Property="Shell.TabBarTitleColor" Value="White" />
</Style>

<Style BasedOn="{StaticResource BaseStyle}"
TargetType="TabBar" />

</ResourceDictionary>
</Shell.Resources>
<ShellContent ContentTemplate="{DataTemplate issues:FirstIssue10679Page}" />
</local:TestShell>
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
using System.Collections.Generic;
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;

#if UITEST
using Xamarin.Forms.Core.UITests;
using Xamarin.UITest;
using NUnit.Framework;
#endif

namespace Xamarin.Forms.Controls.Issues
{
[Preserve(AllMembers = true)]
[Issue(IssueTracker.Github, 10679, "SwipeView crash with NullReferenceException",
PlatformAffected.Android)]
public partial class Issue10679 : TestShell
{
public Issue10679()
{
#if APP
Device.SetFlags(new List<string>(Device.Flags ?? new List<string>()) { "SwipeView_Experimental" });
InitializeComponent();
#endif
}

protected override void Init()
{

}
}

[Preserve(AllMembers = true)]
public class FirstIssue10679Page : ContentPage
{
public FirstIssue10679Page()
{
Title = "Issue 10679";

var layout = new Grid();

var button = new Button
{
VerticalOptions = LayoutOptions.Center,
Text = "Go to Next Page"
};

layout.Children.Add(button);

Content = layout;

button.Clicked += async (sender, args) =>
{
await Shell.Current.Navigation.PushAsync(new SecondIssue10679Page()).ConfigureAwait(false);
};
}
}

[Preserve(AllMembers = true)]
public class SecondIssue10679Page : ContentPage
{
public SecondIssue10679Page()
{
var layout = new Grid();

var swipeView = new SwipeView();

var leftItem = new SwipeItem
{
BackgroundColor = Color.Red
};

var leftItems = new SwipeItems
{
Mode = SwipeMode.Execute,
SwipeBehaviorOnInvoked = SwipeBehaviorOnInvoked.Close
};

leftItems.Add(leftItem);

var rightItem = new SwipeItem
{
BackgroundColor = Color.Green
};

var rightItems = new SwipeItems
{
Mode = SwipeMode.Execute,
SwipeBehaviorOnInvoked = SwipeBehaviorOnInvoked.Close
};

rightItems.Add(rightItem);

var content = new Grid
{
BackgroundColor = Color.White
};

var contentLabel = new Label
{
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.Center,
Text = "Swipe Left or Right"
};

content.Children.Add(contentLabel);

swipeView.Content = content;
swipeView.LeftItems = leftItems;
swipeView.RightItems = rightItems;

layout.Children.Add(swipeView);

Content = layout;

leftItem.Invoked += async (sender, args) =>
{
Shell.Current.Navigation.InsertPageBefore(new ThirdIssue10679Page(), this);
_ = await Shell.Current.Navigation.PopAsync(false);
};

rightItem.Invoked += async (sender, args) =>
{
Shell.Current.Navigation.InsertPageBefore(new ThirdIssue10679Page(), this);
_ = await Shell.Current.Navigation.PopAsync(false);
};
}
}

[Preserve(AllMembers = true)]
public class ThirdIssue10679Page : ContentPage
{
public ThirdIssue10679Page()
{
var layout = new StackLayout();

var infoLabel = new Label
{
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.Center,
Text = "Without NullReferenceException, the test has passed."
};

layout.Children.Add(infoLabel);

Content = layout;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1412,6 +1412,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Issue10530.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue7780.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue8958.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue10679.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue10830.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue10735.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue10497.cs" />
Expand Down Expand Up @@ -1679,6 +1680,10 @@
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue8958.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue10679.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue10735.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
Expand Down

0 comments on commit f5575fb

Please sign in to comment.