Skip to content

Commit

Permalink
[X] send tree changed events on parenting (xamarin#8760)
Browse files Browse the repository at this point in the history
so thye will be triggered in all cases
  • Loading branch information
StephaneDelcroix authored and samhouts committed Dec 6, 2019
1 parent 0250aef commit ad78adb
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 47 deletions.
2 changes: 1 addition & 1 deletion Xamarin.Forms.Build.Tasks/SetPropertiesVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -931,7 +931,7 @@ internal static IEnumerable<Instruction> RegisterSourceInfo(ILContext context, I
yield return Create(Ldc_I4, lineInfo.LineNumber); //lineNumber
yield return Create(Ldc_I4, lineInfo.LinePosition); //linePosition

yield return Create(Call, module.ImportMethodReference(("Xamarin.Forms.Xaml", "Xamarin.Forms.Xaml.Diagnostics", "VisualDiagnostics"),
yield return Create(Call, module.ImportMethodReference(("Xamarin.Forms.Core", "Xamarin.Forms.Xaml.Diagnostics", "VisualDiagnostics"),
methodName: "RegisterSourceInfo",
parameterTypes: new[] {
("mscorlib", "System", "Object"),
Expand Down
3 changes: 3 additions & 0 deletions Xamarin.Forms.Core/Element.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.ComponentModel;
using System.Runtime.CompilerServices;
using Xamarin.Forms.Internals;
using Xamarin.Forms.Xaml.Diagnostics;

namespace Xamarin.Forms
{
Expand Down Expand Up @@ -200,6 +201,8 @@ public Element Parent
SetInheritedBindingContext(this, null);
}

VisualDiagnostics.SendVisualTreeChanged(this, value);

OnParentSet();

OnPropertyChanged();
Expand Down
3 changes: 3 additions & 0 deletions Xamarin.Forms.Core/Xamarin.Forms.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
<PackageReference Include="System.Dynamic.Runtime" Version="4.3.0" />
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
</ItemGroup>
<ItemGroup>
<Folder Include="Xaml\Diagnostics\" />
</ItemGroup>
<PropertyGroup>
<GitInfoReportImportance>high</GitInfoReportImportance>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
Expand Down Expand Up @@ -28,43 +29,6 @@ internal static void SendVisualTreeChanged(object parent, object child)
public static XamlSourceInfo GetXamlSourceInfo(object obj) => sourceInfos.TryGetValue(obj, out var sourceinfo) ? sourceinfo : null;
}

public class XamlSourceInfo
{
public XamlSourceInfo(Uri sourceUri, int lineNumber, int linePosition)
{
SourceUri = sourceUri;
LineNumber = lineNumber;
LinePosition = linePosition;
}

public Uri SourceUri { get; }
public int LineNumber { get; }
public int LinePosition { get; }

public void Deconstruct(out Uri sourceUri, out int lineNumber, out int linePosition)
{
sourceUri = SourceUri;
lineNumber = LineNumber;
linePosition = LinePosition;
}
}

public class VisualTreeChangeEventArgs : EventArgs
{
public VisualTreeChangeEventArgs(object parent, object child, int childIndex, VisualTreeChangeType changeType)
{
Parent = parent;
Child = child;
ChildIndex = childIndex;
ChangeType = changeType;
}

public object Parent { get; }
public object Child { get; }
public int ChildIndex { get; }
public VisualTreeChangeType ChangeType { get; }
}

public enum VisualTreeChangeType
{
Add = 0,
Expand Down
23 changes: 23 additions & 0 deletions Xamarin.Forms.Core/Xaml/Diagnostics/VisualTreeChangeEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;

namespace Xamarin.Forms.Xaml.Diagnostics
{
public class VisualTreeChangeEventArgs : EventArgs
{
public VisualTreeChangeEventArgs(object parent, object child, int childIndex, VisualTreeChangeType changeType)
{
Parent = parent;
Child = child;
ChildIndex = childIndex;
ChangeType = changeType;
}

public object Parent { get; }
public object Child { get; }
public int ChildIndex { get; }
public VisualTreeChangeType ChangeType { get; }
}
}
28 changes: 28 additions & 0 deletions Xamarin.Forms.Core/Xaml/Diagnostics/XamlSourceInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;

namespace Xamarin.Forms.Xaml.Diagnostics
{
public class XamlSourceInfo
{
public XamlSourceInfo(Uri sourceUri, int lineNumber, int linePosition)
{
SourceUri = sourceUri;
LineNumber = lineNumber;
LinePosition = linePosition;
}

public Uri SourceUri { get; }
public int LineNumber { get; }
public int LinePosition { get; }

public void Deconstruct(out Uri sourceUri, out int lineNumber, out int linePosition)
{
sourceUri = SourceUri;
lineNumber = LineNumber;
linePosition = LinePosition;
}
}
}
12 changes: 3 additions & 9 deletions Xamarin.Forms.Xaml/ApplyPropertiesVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -346,28 +346,22 @@ public static void SetPropertyValue(object xamlelement, XmlName propertyName, ob

//If it's a BindableProberty, SetValue
if (xpe == null && TrySetValue(xamlelement, property, attached, value, lineInfo, serviceProvider, out xpe)) {
if (!(node is ValueNode) && value != null && !value.GetType().GetTypeInfo().IsValueType && XamlFilePathAttribute.GetFilePathForObject(context.RootElement) is string path) {
if (!(node is ValueNode) && value != null && !value.GetType().GetTypeInfo().IsValueType && XamlFilePathAttribute.GetFilePathForObject(context.RootElement) is string path)
VisualDiagnostics.RegisterSourceInfo(value, new Uri(path, UriKind.Relative), ((IXmlLineInfo)node).LineNumber, ((IXmlLineInfo)node).LinePosition);
VisualDiagnostics.SendVisualTreeChanged(xamlelement, value);
}
return;
}

//If we can assign that value to a normal property, let's do it
if (xpe == null && TrySetProperty(xamlelement, localName, value, lineInfo, serviceProvider, context, out xpe)) {
if (!(node is ValueNode) && value != null && !value.GetType().GetTypeInfo().IsValueType && XamlFilePathAttribute.GetFilePathForObject(context.RootElement) is string path) {
if (!(node is ValueNode) && value != null && !value.GetType().GetTypeInfo().IsValueType && XamlFilePathAttribute.GetFilePathForObject(context.RootElement) is string path)
VisualDiagnostics.RegisterSourceInfo(value, new Uri(path, UriKind.Relative), ((IXmlLineInfo)node).LineNumber, ((IXmlLineInfo)node).LinePosition);
VisualDiagnostics.SendVisualTreeChanged(xamlelement, value);
}
return;
}

//If it's an already initialized property, add to it
if (xpe == null && TryAddToProperty(xamlelement, propertyName, value, xKey, lineInfo, serviceProvider, context, out xpe)) {
if (!(node is ValueNode) && value != null && !value.GetType().GetTypeInfo().IsValueType && XamlFilePathAttribute.GetFilePathForObject(context.RootElement) is string path) {
if (!(node is ValueNode) && value != null && !value.GetType().GetTypeInfo().IsValueType && XamlFilePathAttribute.GetFilePathForObject(context.RootElement) is string path)
VisualDiagnostics.RegisterSourceInfo(value, new Uri(path, UriKind.Relative), ((IXmlLineInfo)node).LineNumber, ((IXmlLineInfo)node).LinePosition);
VisualDiagnostics.SendVisualTreeChanged(xamlelement, value);
}
return;
}

Expand Down
5 changes: 5 additions & 0 deletions Xamarin.Forms.Xaml/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,8 @@
#pragma warning disable CS0612 // Type or member is obsolete
[assembly: TypeForwardedTo(typeof(Xamarin.Forms.Xaml.Internals.INameScopeProvider))]
#pragma warning restore CS0612 // Type or member is obsolete

[assembly: TypeForwardedTo(typeof(Xamarin.Forms.Xaml.Diagnostics.DebuggerHelper))]
[assembly: TypeForwardedTo(typeof(Xamarin.Forms.Xaml.Diagnostics.VisualDiagnostics))]
[assembly: TypeForwardedTo(typeof(Xamarin.Forms.Xaml.Diagnostics.VisualTreeChangeEventArgs))]
[assembly: TypeForwardedTo(typeof(Xamarin.Forms.Xaml.Diagnostics.XamlSourceInfo))]

0 comments on commit ad78adb

Please sign in to comment.