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

Commit

Permalink
[C] get the name of a scoped object
Browse files Browse the repository at this point in the history
- fixes #12608
  • Loading branch information
StephaneDelcroix committed Oct 29, 2020
1 parent b59bb76 commit 4a5b4cd
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Xamarin.Forms.Core/Internals/NameScope.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;

namespace Xamarin.Forms.Internals
{
Expand All @@ -11,6 +12,7 @@ public class NameScope : INameScope
BindableProperty.CreateAttached("NameScope", typeof(INameScope), typeof(NameScope), default(INameScope));

readonly Dictionary<string, object> _names = new Dictionary<string, object>();
readonly Dictionary<object, string> _values = new Dictionary<object, string>();

object INameScope.FindByName(string name)
=> _names.TryGetValue(name, out var element) ? element : null;
Expand All @@ -21,8 +23,14 @@ void INameScope.RegisterName(string name, object scopedElement)
throw new ArgumentException($"An element with the key '{name}' already exists in NameScope", nameof(name));

_names[name] = scopedElement;
_values[scopedElement] = name;
}

//used by VS Live Visual Tree
internal string NameOf(object scopedObject)
=> _values.TryGetValue(scopedObject, out var name) ? name : null;


public static INameScope GetNameScope(BindableObject bindable) => (INameScope)bindable.GetValue(NameScopeProperty);

public static void SetNameScope(BindableObject bindable, INameScope value)
Expand All @@ -42,6 +50,7 @@ void INameScope.UnregisterName(string name)
if (!_names.ContainsKey(name))
throw new ArgumentException("name provided had not been registered.", nameof(name));

_values.Remove(_names[name]);
_names.Remove(name);
}
}
Expand Down

0 comments on commit 4a5b4cd

Please sign in to comment.