Beispiel #1
0
        public void When_Null_Target()
        {
            var mr1 = WeakReferencePool.RentWeakReference(this, null);

            WeakReferencePool.ReturnWeakReference(this, mr1);
            Assert.AreEqual(1, WeakReferencePool.PooledReferences);
        }
        public TextBoxView(TextBox owner)
            : base(ContextHelper.Current)
        {
            _ownerRef = WeakReferencePool.RentWeakReference(this, owner);
            InitializeBinder();

            base.SetSingleLine(true);

            //This Background color is set to remove the native android underline on the EditText.
            this.SetBackgroundColor(Colors.Transparent);
            //Remove default native padding.
            this.SetPadding(0, 0, 0, 0);

            if (FeatureConfiguration.TextBox.HideCaret)
            {
                SetCursorVisible(false);
            }

            _isInitialized = true;

            // This ensures the TextBoxView gets resized as Text changes
            LayoutParameters = new Android.Views.ViewGroup.LayoutParams(
                Android.Views.ViewGroup.LayoutParams.WrapContent,
                Android.Views.ViewGroup.LayoutParams.WrapContent
                );
        }
Beispiel #3
0
        internal void SetDirectManipulationStateChangeHandler(IDirectManipulationStateChangeHandler?handler)
        {
            _directManipulationHandlerSubscription?.Dispose();

            if (handler is null)
            {
                return;
            }

            var weakHandler = WeakReferencePool.RentWeakReference(this, handler);

            UpdatesMode  = ScrollViewerUpdatesMode.Synchronous;
            ViewChanged += OnViewChanged;
            _directManipulationHandlerSubscription = Disposable.Create(() =>
            {
                ViewChanged -= OnViewChanged;
                WeakReferencePool.ReturnWeakReference(this, weakHandler);
            });

            void OnViewChanged(object?sender, ScrollViewerViewChangedEventArgs args)
            {
                if (args.IsIntermediate)
                {
                    return;
                }

                if (weakHandler.Target is IDirectManipulationStateChangeHandler h)
                {
                    h.NotifyStateChange(DMManipulationState.DMManipulationCompleted, default, default, default, default, default, default, default, default);
        public AttachedDependencyObject(object owner)
        {
            InitializeBinder();

            Owner = owner;
            OwnerWeakReference = WeakReferencePool.RentWeakReference(this, owner);
        }
Beispiel #5
0
        public void RegisterName(string name, object scopedElement)
        {
            if (_names.ContainsKey(name))
            {
                this.Log().Warn($"The name [{name}] already exists in the current XAML scope");
            }

            _names[name] = WeakReferencePool.RentWeakReference(this, scopedElement);
        }
Beispiel #6
0
        public void When_Empty()
        {
            var target = new object();

            var mr1 = WeakReferencePool.RentWeakReference(this, target);

            Assert.AreEqual(target, mr1.GetUnsafeTargetHandle().Target);
            Assert.AreEqual(this, mr1.Owner);
        }
Beispiel #7
0
        public void When_Null_Target_And_Owner()
        {
            var o1 = new Object();

            var mr1 = WeakReferencePool.RentWeakReference(null, null);

            WeakReferencePool.ReturnWeakReference(null, mr1);
            Assert.AreEqual(0, WeakReferencePool.PooledReferences);
        }
Beispiel #8
0
        private void AttachDismissCommand(IFrameworkElement control)
        {
            var b = control.FindName("DismissButton") as Button;

            if (b != null && b.Command == null)
            {
                var wr = WeakReferencePool.RentWeakReference(this, this);
                b.Command = new DelegateCommand(() => (wr.Target as TimePickerFlyout)?.Dismiss());
            }
        }
Beispiel #9
0
        public void When_IWeakReferenceProvider()
        {
            var target = new MyProvider();

            var mr1 = WeakReferencePool.RentWeakReference(this, target);

            Assert.AreEqual(target, mr1.GetUnsafeTargetHandle().Target);
            Assert.ReferenceEquals(target.WeakReference.GetUnsafeTargetHandle(), mr1.GetUnsafeTargetHandle());
            Assert.AreEqual(target, mr1.Owner);
        }
Beispiel #10
0
        public void When_WeakReferenceProvider_Collected()
        {
            var o1 = new Object();

            void test1()
            {
                var mr1 = WeakReferencePool.RentWeakReference(this, o1);

                WeakReferencePool.ReturnWeakReference(this, mr1);
            }

            test1();

            Assert.AreEqual(2, WeakReferencePool.PooledReferences);

            void test2()
            {
                var mr2 = WeakReferencePool.RentWeakReference(this, o1);

                WeakReferencePool.ReturnWeakReference(this, mr2);
            }

            test2();

            Assert.AreEqual(2, WeakReferencePool.PooledReferences);

            (WeakReference refref, WeakReference ownerRef, WeakReference targetRef) CreateUnreferenced()
            {
                var o2  = new Object();
                var mr2 = WeakReferencePool.RentWeakReference(o1, o2);

                return(new WeakReference(mr2), new WeakReference(mr2.GetUnsafeOwnerHandle()), new WeakReference(mr2.GetUnsafeTargetHandle()));
            }

            var r = CreateUnreferenced();

            Assert.IsNotNull(r.ownerRef.Target);
            Assert.IsNotNull(r.targetRef.Target);
            Assert.IsNotNull(r.refref.Target);

            Assert.AreEqual(0, WeakReferencePool.PooledReferences);

            GCCondition(5, () => r.refref.Target == null);

            Assert.IsNull(r.refref.Target);

            GCCondition(5, () => r.ownerRef.Target == null && r.targetRef.Target == null);

            Assert.IsNull(r.ownerRef.Target);
            Assert.IsNull(r.targetRef.Target);

            Assert.AreEqual(0, WeakReferencePool.PooledReferences);
        }
Beispiel #11
0
        public FrameworkTemplate(object?owner, FrameworkTemplateBuilder?factory)
        {
            InitializeBinder();

            _viewFactory = factory;
            _ownerRef    = WeakReferencePool.RentWeakReference(this, owner);

            // Compute the hash for this template once, it will be used a lot
            // in the ControlPool's internal dictionary.
            _hashCode = (factory?.Target?.GetHashCode() ?? 0) ^ (factory?.Method.GetHashCode() ?? 0);

            _xamlScope = ResourceResolver.CurrentScope;
        }
Beispiel #12
0
        public void When_WeakReferenceProvider_Reused()
        {
            var o1 = new Object();

            var mr1 = WeakReferencePool.RentWeakReference(this, o1);

            WeakReferencePool.ReturnWeakReference(this, mr1);

            var target = new MyProvider();

            Assert.AreEqual(target, target.WeakReference.Target);

            var mr2 = WeakReferencePool.RentWeakReference(target, o1);

            WeakReferencePool.ReturnWeakReference(target, mr2);

            Assert.AreEqual(target, target.WeakReference.Target);
        }
Beispiel #13
0
        public void When_Not_Owned_Returned()
        {
            var target1 = new object();
            var target2 = new object();

            var mr1 = WeakReferencePool.RentWeakReference(target1, target1);

            var wr = mr1.GetUnsafeTargetHandle();

            Assert.AreEqual(target1, mr1.GetUnsafeTargetHandle().Target);
            Assert.AreEqual(target1, mr1.Owner);

            WeakReferencePool.ReturnWeakReference(this, mr1);

            var mr2 = WeakReferencePool.RentWeakReference(this, target2);

            Assert.AreEqual(target2, mr2.GetUnsafeTargetHandle().Target);
            Assert.AreEqual(this, mr2.Owner);
            Assert.AreNotSame(wr, mr2.GetUnsafeTargetHandle());
        }
Beispiel #14
0
 private object Wrap(object value)
 => _hasWeakStorage && value != null && value != DependencyProperty.UnsetValue
                 ? WeakReferencePool.RentWeakReference(this, value)
                 : value;
Beispiel #15
0
 public void RegisterName(string name, object scopedElement)
 {
     _names.Add(name, WeakReferencePool.RentWeakReference(this, scopedElement));
 }
 public WeakResourceInitializer(object owner, ResourceInitializerWithOwner initializer)
 {
     _owner      = WeakReferencePool.RentWeakReference(this, owner);
     Initializer = () => initializer(_owner?.Target);
 }