return empty dictionary instead of null (#6675) fixes #6614

* return empty dictionary instead of null

* add warning if Element is being reparented

* fix warning message

* only fire warning if parent has inner children
This commit is contained in:
Shane Neuville 2019-06-28 04:17:11 -06:00 коммит произвёл Rui Marinho
Родитель 3ffb15cb94
Коммит 40961c12b6
4 изменённых файлов: 67 добавлений и 4 удалений

Просмотреть файл

@ -0,0 +1,55 @@
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, 6614, "[Android] Tabindex Calculation crashing when calculating on Layout", PlatformAffected.Android)]
public class Issue6614 : TestContentPage
{
Button _button = null;
string _instruction1 = "Turn on Screen Reader and click me.";
protected override void Init()
{
_button = new Button()
{
Text = _instruction1,
Command = new Command(() =>
{
if(Content is ContentView currentContentView)
{
var currentContent = currentContentView.Content;
currentContentView.Content = null;
this.Content = currentContent;
_button.Text = "Success";
}
else
{
var currentContent = this.Content;
var contentView = new ContentView();
this.Content = contentView;
contentView.Content = currentContent;
_button.Text = "Click me one more time";
}
}),
TabIndex = 1
};
Content = new StackLayout()
{
Children =
{
_button
}
};
}
}
}

Просмотреть файл

@ -948,6 +948,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Issue5888.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Issue5888.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue6334.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Issue6334.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue6368.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Issue6368.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue6614.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Bugzilla22229.xaml"> <EmbeddedResource Include="$(MSBuildThisFileDirectory)Bugzilla22229.xaml">
@ -1227,7 +1228,7 @@
<ItemGroup> <ItemGroup>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue5268.xaml"> <EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue5268.xaml">
<SubType>Designer</SubType> <SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator> <Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource> </EmbeddedResource>
</ItemGroup> </ItemGroup>
</Project> </Project>

Просмотреть файл

@ -187,7 +187,13 @@ namespace Xamarin.Forms
OnPropertyChanging(); OnPropertyChanging();
if (RealParent != null) if (RealParent != null)
{
((IElement)RealParent).RemoveResourcesChangedListener(OnParentResourcesChanged); ((IElement)RealParent).RemoveResourcesChangedListener(OnParentResourcesChanged);
if(value != null && (RealParent is Layout || RealParent is IControlTemplated))
Log.Warning("Element", $"{this} is already a child of {RealParent}. Remove {this} from {RealParent} before adding to {value}.");
}
RealParent = value; RealParent = value;
if (RealParent != null) if (RealParent != null)
{ {

Просмотреть файл

@ -16,7 +16,7 @@ namespace Xamarin.Forms
{ {
countChildrensWithTabStopWithoutThis = 0; countChildrensWithTabStopWithoutThis = 0;
Element parentPage = (element as NavigableElement).Parent; Element parentPage = (element as Element)?.Parent;
while (parentPage != null && !(parentPage is Page)) while (parentPage != null && !(parentPage is Page))
parentPage = parentPage.Parent; parentPage = parentPage.Parent;
@ -26,7 +26,7 @@ namespace Xamarin.Forms
descendantsOnPage = shell.Items; descendantsOnPage = shell.Items;
if (descendantsOnPage == null) if (descendantsOnPage == null)
return null; return new Dictionary<int, List<ITabStopElement>>();
var childrensWithTabStop = new List<ITabStopElement>(); var childrensWithTabStop = new List<ITabStopElement>();
foreach (var descendant in descendantsOnPage) foreach (var descendant in descendantsOnPage)
@ -34,8 +34,9 @@ namespace Xamarin.Forms
if (descendant is ITabStopElement visualElement && visualElement.IsTabStop) if (descendant is ITabStopElement visualElement && visualElement.IsTabStop)
childrensWithTabStop.Add(visualElement); childrensWithTabStop.Add(visualElement);
} }
if (checkContainsElement && !childrensWithTabStop.Contains(element)) if (checkContainsElement && !childrensWithTabStop.Contains(element))
return null; return new Dictionary<int, List<ITabStopElement>>();
countChildrensWithTabStopWithoutThis = childrensWithTabStop.Count - 1; countChildrensWithTabStopWithoutThis = childrensWithTabStop.Count - 1;
return childrensWithTabStop.GroupToDictionary(c => c.TabIndex); return childrensWithTabStop.GroupToDictionary(c => c.TabIndex);