Esempio n. 1
0
 public BindingActionValue([NotNull] object memberSource, [NotNull] IBindingMemberInfo member)
 {
     Should.NotBeNull(memberSource, nameof(memberSource));
     Should.NotBeNull(member, nameof(member));
     _memberSource = ToolkitExtensions.GetWeakReference(memberSource);
     _member       = member;
 }
Esempio n. 2
0
        protected virtual IDataBinding CreateBinding([NotNull] IDataContext context, out object target, out IBindingPath targetPath)
        {
            IBindingSourceAccessor sourceAccessor;
            var formatExpression = context.GetData(BindingBuilderConstants.MultiExpression);
            var sourceDelegates  = context.GetData(BindingBuilderConstants.Sources, true);

            if (sourceDelegates.Count > 1 || formatExpression != null)
            {
                formatExpression = formatExpression ?? FormatMembersExpressionDelegate;
                var sources = new IObserver[sourceDelegates.Count];
                for (int index = 0; index < sourceDelegates.Count; index++)
                {
                    sources[index] = sourceDelegates[index].Invoke(context);
                }
                sourceAccessor = new MultiBindingSourceAccessor(sources, formatExpression, context);
            }
            else
            {
                sourceAccessor = new BindingSourceAccessor(sourceDelegates[0].Invoke(context), context, false);
            }
            var    binding = new DataBinding(new BindingSourceAccessor(GetBindingTarget(context, out target, out targetPath), context, true), sourceAccessor);
            object source;

            if (context.TryGetData(BindingBuilderConstants.Source, out source))
            {
                binding.Context.AddOrUpdate(BindingConstants.Source, ToolkitExtensions.GetWeakReference(source));
            }
            AddBehaviors(binding, context);
            return(binding);
        }
Esempio n. 3
0
 public MultiBindingPathMembers(WeakReference observerReference, object penultimateValue, IList <IBindingMemberInfo> members)
 {
     PenultimateValueRef = ToolkitExtensions.GetWeakReference(penultimateValue);
     _observerRef        = observerReference;
     _members            = members;
     _lastMember         = _members[_members.Count - 1];
 }
        protected override IBindingPathMembers UpdateInternal(IBindingPathMembers oldPath, bool hasSubscribers)
        {
            object source = GetActualSource();

            if (source == null || source.IsUnsetValue())
            {
                return(UnsetBindingPathMembers.Instance);
            }
            var srcRef = OriginalSource as WeakReference;

            if (oldPath != null && srcRef != null)
            {
                var members = oldPath as SingleBindingPathMembers;
                if (members != null)
                {
                    if (hasSubscribers)
                    {
                        _weakEventListener = TryObserveMember(source, members.LastMember, this, Path.Path);
                    }
                    return(members);
                }
            }
            IBindingMemberInfo lastMember = BindingServiceProvider
                                            .MemberProvider
                                            .GetBindingMember(source.GetType(), Path.Path, _ignoreAttachedMembers, true);

            if (hasSubscribers || srcRef == null)
            {
                _weakEventListener = TryObserveMember(source, lastMember, this, Path.Path);
            }
            return(new SingleBindingPathMembers(srcRef ?? ToolkitExtensions.GetWeakReference(source), Path, lastMember));
        }
Esempio n. 5
0
 public void SetViewModel(IViewModel viewModel)
 {
     WeakReference[] viewModels;
     lock (this)
     {
         if (_viewModelRef != null)
         {
             return;
         }
         _viewModelRef = ToolkitExtensions.GetWeakReference(viewModel);
         if (_childViewModels == null)
         {
             return;
         }
         viewModels       = _childViewModels.ToArray();
         _childViewModels = null;
     }
     for (int i = 0; i < viewModels.Length; i++)
     {
         var childVm = (IViewModel)viewModels[i].Target;
         if (childVm != null && !childVm.IsDisposed)
         {
             OnParentUpdated(childVm, viewModel);
         }
     }
 }
            public void Unsubscribe(bool dispose, bool oneTime)
            {
                if (_subscriber == null)
                {
                    return;
                }
                if (dispose && oneTime && GetReferenceValue() is ICommand)
                {
                    var penultimateValue = GetPenultimateValue();
                    if (!penultimateValue.IsNullOrUnsetValue())
                    {
                        LastContext = LastContext == null ? new DataContext() : new DataContext(LastContext);
                        LastContext.Remove(BindingConstants.Binding);
                        _source = ToolkitExtensions.GetWeakReference(penultimateValue);
                        return;
                    }
                }

                LastContext = null;
                //it's normal here.
                lock (this)
                {
                    UnsubscribeEventHandler();
                    UnsubscribeCommand();
                    _valueReference = null;
                }
                if (dispose)
                {
                    _canExecuteHandler = null;
                }
            }
 public WeakEventHandler(TTarget target, Action <TTarget, object, TArg> invokeAction, Delegate unsubscribeAction)
 {
     Should.NotBeNull(target, nameof(target));
     Should.NotBeNull(invokeAction, nameof(invokeAction));
     _invokeAction      = invokeAction;
     _unsubscribeAction = unsubscribeAction;
     _targetReference   = ToolkitExtensions.GetWeakReference(target);
 }
 public WeakActionSubscriber(object target, MethodInfo method)
 {
     Should.NotBeNull(target, nameof(target));
     Should.NotBeNull(method, nameof(method));
     _reference = ToolkitExtensions.GetWeakReference(target);
     _method    = method;
     _delegate  = (Action <object, object, T>)ToolkitServiceProvider
                  .ReflectionManager
                  .GetMethodDelegate(typeof(Action <object, object, T>), method);
     _hash = ActionSubscriber <object> .ActionSubscriberGetHashCode(target, method);
 }
Esempio n. 9
0
 public WeakEventListenerWrapper(IEventListener listener)
 {
     if (listener.IsWeak)
     {
         _item = listener;
     }
     else
     {
         _item = ToolkitExtensions.GetWeakReference(listener);
     }
 }
Esempio n. 10
0
 public static void AddOrientationChangeListener([NotNull] IOrientationChangeListener listener)
 {
     Should.NotBeNull(listener, "listener");
     lock (OrientationChangeListeners)
     {
         if (!_hasOrientationChangeSubscriber)
         {
             UIApplication.Notifications.ObserveDidChangeStatusBarOrientation(DidChangeStatusBarOrientation);
             _hasOrientationChangeSubscriber = true;
         }
         OrientationChangeListeners.Add(ToolkitExtensions.GetWeakReference(listener));
     }
 }
        protected override IBindingPathMembers UpdateInternal(IBindingPathMembers oldPath, bool hasSubscribers)
        {
            object source = GetActualSource();

            if (source.IsNullOrUnsetValue())
            {
                return(UnsetBindingPathMembers.Instance);
            }
            var members = oldPath as SingleBindingPathMembers;
            var srcRef  = OriginalSource as WeakReference;

            if (members != null)
            {
                if (srcRef != null || ReferenceEquals(members.Source, source))
                {
                    if (hasSubscribers && _observable && _weakEventListener == null)
                    {
                        _weakEventListener = TryObserveMember(source, members.LastMember, this, Path.Path);
                    }
                    return(members);
                }
                if (_hasStablePath)
                {
                    members.Reference = ToolkitExtensions.GetWeakReference(source);
                    if (hasSubscribers && _observable)
                    {
                        _weakEventListener?.Dispose();
                        _weakEventListener = TryObserveMember(source, members.LastMember, this, Path.Path);
                    }
                    return(members);
                }
            }
            IBindingMemberInfo lastMember = GetBindingMember(source.GetType(), Path.Path, _ignoreAttachedMembers, _optional);

            if (lastMember == null)
            {
                return(UnsetBindingPathMembers.Instance);
            }

            if (_observable && hasSubscribers)
            {
                _weakEventListener?.Dispose();
                _weakEventListener = TryObserveMember(source, lastMember, this, Path.Path);
            }
            return(new SingleBindingPathMembers(srcRef ?? ToolkitExtensions.GetWeakReference(source), Path, lastMember));
        }
Esempio n. 12
0
        public MultiViewModel()
        {
            var collection = new SynchronizedNotifiableCollection <TViewModel>();
            var list       = ServiceProvider.TryDecorate(this, collection);

            Should.BeOfType <INotifiableCollection <TViewModel> >(list, "DecoratedItemsSource");
            _itemsSource = (INotifiableCollection <TViewModel>)list;
            collection.AfterCollectionChanged = OnViewModelsChanged;
            _propertyChangedWeakEventHandler  = ReflectionExtensions.MakeWeakPropertyChangedHandler(this, (model, o, arg3) => model.OnItemPropertyChanged(o, arg3));
            var weakReference = ToolkitExtensions.GetWeakReference(this);

            _closeViewModelWeakHandler = (dispatcher, vm, arg3) =>
            {
                var self = (MultiViewModel <TViewModel>)weakReference.Target;
                return(self?.CloseViewModel(dispatcher, vm, arg3) ?? Empty.FalseTask);
            };
            DisposeViewModelOnRemove = ApplicationSettings.MultiViewModelDisposeViewModelOnRemove;
            CloseViewModelsOnClose   = ApplicationSettings.MultiViewModelCloseViewModelsOnClose;
        }
Esempio n. 13
0
            public void AddChildViewModel(IViewModel viewModel)
            {
                lock (this)
                {
                    if (_viewModelRef == null)
                    {
                        if (_childViewModels == null)
                        {
                            _childViewModels = new List <WeakReference>();
                        }
                        _childViewModels.Add(ToolkitExtensions.GetWeakReference(viewModel));
                        return;
                    }
                }
                var target = (IViewModel)_viewModelRef.Target;

                if (target != null)
                {
                    OnParentUpdated(viewModel, target);
                }
            }
Esempio n. 14
0
 private void SetValue(object value)
 {
     _value = _isWeak ? ToolkitExtensions.GetWeakReference(value) : value;
 }
Esempio n. 15
0
 public WeakEventListener(IEventListener listener)
 {
     _listenerRef = ToolkitExtensions.GetWeakReference(listener);
 }
Esempio n. 16
0
        private static void InitializeParentViewModel(IViewModel viewModel, IViewModel parentViewModel, IDataContext context)
        {
            if (parentViewModel == null)
            {
                return;
            }
            ObservationMode observationMode;

            if (!context.TryGetData(InitializationConstants.ObservationMode, out observationMode))
            {
                observationMode = ApplicationSettings.ViewModelObservationMode;
            }
            viewModel.Settings.Metadata.AddOrUpdate(ViewModelConstants.ParentViewModel, ToolkitExtensions.GetWeakReference(parentViewModel));
            if (observationMode.HasFlagEx(ObservationMode.ParentObserveChild))
            {
                viewModel.Subscribe(parentViewModel);
            }
            if (observationMode.HasFlagEx(ObservationMode.ChildObserveParent))
            {
                parentViewModel.Subscribe(viewModel);
            }
        }
 /// <summary>
 ///     Updates the current values.
 /// </summary>
 protected override void UpdateInternal()
 {
     try
     {
         if (_weakEventListener != null)
         {
             _weakEventListener.Dispose();
         }
         object source = GetActualSource();
         if (source == null || source.IsUnsetValue())
         {
             _pathMembers = UnsetBindingPathMembers.Instance;
         }
         else
         {
             IBindingMemberInfo lastMember = BindingServiceProvider
                                             .MemberProvider
                                             .GetBindingMember(source.GetType(), Path.Path, _ignoreAttachedMembers, true);
             _pathMembers       = new SingleBindingPathMembers(OriginalSource as WeakReference ?? ToolkitExtensions.GetWeakReference(source), Path, lastMember);
             _weakEventListener = TryObserveMember(source, lastMember, this, Path.Path);
         }
     }
     catch (Exception)
     {
         _pathMembers = UnsetBindingPathMembers.Instance;
         throw;
     }
 }
Esempio n. 18
0
 private static WeakReference GetWeakReference(object target)
 {
     return(target as WeakReference ?? ToolkitExtensions.GetWeakReference(target));
 }
Esempio n. 19
0
 protected virtual IEventListener CreateSourceListener()
 {
     return(new DefaultListener(ToolkitExtensions.GetWeakReference(this)));
 }
Esempio n. 20
0
 public bool AddNotifier(object item)
 {
     Should.NotBeNull(item, nameof(item));
     if (!HasCanExecuteImpl)
     {
         return(false);
     }
     lock (_notifiers)
     {
         if (Contains(item, false))
         {
             return(true);
         }
         Action <RelayCommandBase, object> notifier = CreateNotifier(item);
         if (notifier == null)
         {
             return(false);
         }
         _notifiers.Add(new KeyValuePair <WeakReference, Action <RelayCommandBase, object> >(ToolkitExtensions.GetWeakReference(item), notifier));
         return(true);
     }
 }
Esempio n. 21
0
        protected override IBindingPathMembers UpdateInternal(IBindingPathMembers oldPath, bool hasSubscribers)
        {
            object source = GetActualSource();

            if (source.IsNullOrUnsetValue())
            {
                return(UnsetBindingPathMembers.Instance);
            }
            ClearListeners();
            int lastIndex;

            if (_hasStablePath)
            {
                var pathMembers = oldPath as MultiBindingPathMembers;
                if (pathMembers != null)
                {
                    var list = pathMembers.Members;
                    lastIndex = list.Count - 1;
                    for (int index = 0; index < list.Count; index++)
                    {
                        var pathMember = list[index];
                        if (_observable)
                        {
                            var observer = TryObserveMember(source, pathMember, index == lastIndex);
                            if (observer != null)
                            {
                                _listeners.Add(observer);
                            }
                        }
                        if (index == lastIndex)
                        {
                            break;
                        }
                        source = pathMember.GetValue(source, null);
                        if (source.IsNullOrUnsetValue())
                        {
                            if (Path.IsDebuggable)
                            {
                                DebugInfo($"Value is not available for '{pathMember.Path}'", new[] { GetActualSource(false) });
                            }
                            return(UnsetBindingPathMembers.Instance);
                        }
                    }
                    pathMembers.PenultimateValueRef = ToolkitExtensions.GetWeakReference(source);
                    return(pathMembers);
                }
            }

            IList <string> items = Path.Parts;

            lastIndex = items.Count - 1;
            var members = new List <IBindingMemberInfo>();

            for (int index = 0; index < items.Count; index++)
            {
                IBindingMemberInfo pathMember = GetBindingMember(source.GetType(), items[index], _ignoreAttachedMembers, _optional);
                if (pathMember == null)
                {
                    return(UnsetBindingPathMembers.Instance);
                }
                members.Add(pathMember);
                if (_observable)
                {
                    var observer = TryObserveMember(source, pathMember, index == lastIndex);
                    if (observer != null)
                    {
                        _listeners.Add(observer);
                    }
                }
                if (index == lastIndex)
                {
                    break;
                }
                source = pathMember.GetValue(source, null);
                if (source.IsNullOrUnsetValue())
                {
                    if (Path.IsDebuggable)
                    {
                        DebugInfo($"Value is not available for '{pathMember.Path}'", new[] { GetActualSource(false) });
                    }
                    return(UnsetBindingPathMembers.Instance);
                }
            }

            return(new MultiBindingPathMembers(_lastMemberListener.Reference, source, members));
        }
Esempio n. 22
0
 public ConstResourceObject(object value)
 {
     _reference = value as WeakReference ?? ToolkitExtensions.GetWeakReference(value);
 }
Esempio n. 23
0
 public RootResourceObject([NotNull] object target, [NotNull] IBindingMemberInfo rootMemberInfo)
 {
     _target         = ToolkitExtensions.GetWeakReference(target);
     _rootMemberInfo = rootMemberInfo;
     rootMemberInfo.TryObserve(target, this);
 }
Esempio n. 24
0
 internal WeakListenerInternal(object source, EventInfo eventInfo)
 {
     _sourceRef = ToolkitExtensions.GetWeakReference(source);
     _eventInfo = eventInfo;
 }
Esempio n. 25
0
 public WeakPropertyChangedListener(INotifyPropertyChanged propertyChanged)
 {
     _listeners       = MugenMvvmToolkit.Empty.Array <KeyValuePair <WeakEventListenerWrapper, string> >();
     _propertyChanged = ToolkitExtensions.GetWeakReference(propertyChanged);
 }
 public CloseCommandWrapper(ICommand nestedCommand, NavigationProvider provider, IViewModel viewModel)
 {
     NestedCommand = nestedCommand;
     _provider     = provider;
     _reference    = ToolkitExtensions.GetWeakReference(viewModel);
 }
Esempio n. 27
0
 private HandlerSubscriber(object target, bool isViewModelHandler)
 {
     _hashCode           = RuntimeHelpers.GetHashCode(target);
     _reference          = ToolkitExtensions.GetWeakReference(target);
     _isViewModelHandler = isViewModelHandler;
 }