Skip to content

Commit

Permalink
Use HasBinding for Wpf/Uno in place of BindingOperations.GetBinding
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromelaban committed Apr 23, 2020
1 parent a63b29f commit f85446c
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 15 deletions.
26 changes: 26 additions & 0 deletions src/Uno/Prism.Uno/Extensions/DependencyObjectExtensions.Uno.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Text;

using Windows.UI.Xaml;
using Windows.UI.Xaml.Data;

namespace Prism
{
internal static partial class DependencyObjectExtensions
{
/// <summary>
/// Compatibility method to determine if the current thread can access a <see cref="DependencyObject"/>
/// </summary>
/// <param name="instance">The instance to check</param>
/// <returns><c>true</c> if the current thread has access to the instance, otherwise <c>false</c></returns>
public static bool CheckAccess(this DependencyObject instance)
#if __WASM__
// This needs to evolve, once threading is supported
// See https://github.com/unoplatform/uno/issues/2302
=> System.Threading.Thread.CurrentThread.ManagedThreadId == 1;
#else
=> instance.Dispatcher.HasThreadAccess;
#endif
}
}
23 changes: 11 additions & 12 deletions src/Wpf/Prism.Wpf/Extensions/DependencyObjectExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,19 @@

namespace Prism
{
internal static class DependencyObjectExtensions
internal static partial class DependencyObjectExtensions
{
public static bool CheckAccess(this DependencyObject instance)
#if __WASM__
// This needs to evolve, once threading is supported
// See https://github.com/unoplatform/uno/issues/2302
=> System.Threading.Thread.CurrentThread.ManagedThreadId == 1;
#elif HAS_WINUI
=> instance.Dispatcher.HasThreadAccess;
/// <summary>
/// Determines if a <see cref="DependencyProperty"/> has a binding set
/// </summary>
/// <param name="instance">The to use to search for the property</param>
/// <param name="property">The property to search</param>
/// <returns><c>true</c> if there is an active binding, otherwise <c>false</c></returns>
public static bool HasBinding(this FrameworkElement instance, DependencyProperty property)
#if HAS_WINUI
=> instance.GetBindingExpression(property) != null;
#else
=> instance.CheckAccess();
=> BindingOperations.GetBinding(instance, property) != null;
#endif

public static BindingExpression GetBinding(this FrameworkElement instance, DependencyProperty property)
=> instance.GetBindingExpression(property);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public DependencyObject HostControl
protected override void OnAttach()
{
bool itemsSourceIsSet = this.hostControl.ItemsSource != null;
itemsSourceIsSet = itemsSourceIsSet || (this.hostControl.GetBinding(ItemsControl.ItemsSourceProperty) != null);
itemsSourceIsSet = itemsSourceIsSet || this.hostControl.HasBinding(ItemsControl.ItemsSourceProperty);

if (itemsSourceIsSet)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Wpf/Prism.Wpf/Regions/ContentControlRegionAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ protected override void Adapt(IRegion region, ContentControl regionTarget)
throw new ArgumentNullException(nameof(regionTarget));

bool contentIsSet = regionTarget.Content != null;
contentIsSet = contentIsSet || (regionTarget.GetBinding(ContentControl.ContentProperty) != null);
contentIsSet = contentIsSet || regionTarget.HasBinding(ContentControl.ContentProperty);

if (contentIsSet)
throw new InvalidOperationException(Resources.ContentControlHasContentException);
Expand Down
2 changes: 1 addition & 1 deletion src/Wpf/Prism.Wpf/Regions/ItemsControlRegionAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected override void Adapt(IRegion region, ItemsControl regionTarget)
throw new ArgumentNullException(nameof(regionTarget));

bool itemsSourceIsSet = regionTarget.ItemsSource != null;
itemsSourceIsSet = itemsSourceIsSet || (regionTarget.GetBinding(ItemsControl.ItemsSourceProperty) != null);
itemsSourceIsSet = itemsSourceIsSet || regionTarget.HasBinding(ItemsControl.ItemsSourceProperty);

if (itemsSourceIsSet)
{
Expand Down

0 comments on commit f85446c

Please sign in to comment.