private void InitDependencies() { Application.Current.MainWindow.Activated += WeakEventHandler.Create(this, (@this, o, args) => Task.Run(@this.UpdateAppsAndUrls)); this.WhenAnyValue(vm => vm.OtpSecret).Where(x => !string.IsNullOrWhiteSpace(x)) .Subscribe(o => ErrorOtpSecret = (ValidateBase32String(OtpSecret) ? null : TranslationSource.Instance["PasswordManager.Error.OtpSecretInvalid"])); this.WhenAnyValue(vm => vm.ErrorAccountName, vm => vm.ErrorAccountLogin, vm => vm.ErrorOtpSecret) .Subscribe(observer => HasError = !(ErrorAccountName == null && ErrorAccountLogin == null && ErrorOtpSecret == null)); this.WhenAnyValue(vm => vm.Name, vm => vm.Login, vm => vm.IsPasswordChanged, vm => vm.OtpSecret, vm => vm.HasOpt, vm => vm.Apps, vm => vm.Urls) .Subscribe(o => VerifyHasChanged()); // Some issues arise when using WhenAnyValue is 8 or more arguments this.WhenAnyValue(vm => vm.EditOtp).Subscribe(o => VerifyHasChanged()); this.WhenAnyValue(vm => vm.SelectedApp).Subscribe(OnAppSelected); this.WhenAnyValue(vm => vm.SelectedUrl).Subscribe(OnUrlSelected); Observable.FromEventPattern <NotifyCollectionChangedEventArgs>(AppsAndUrls, nameof(ObservableCollection <string> .CollectionChanged)) .Subscribe(change => AppsOrUrlsCollectonChanges()); OpenedApps.Add(loadingAppInfo); OpenedForegroundUrls.Add(addUrlAppInfo); OpenedForegroundUrls.Add(loadingAppInfo); Task.Run(UpdateAppsAndUrls).ContinueWith(_ => { Application.Current.Dispatcher.Invoke(() => { OpenedApps.Remove(loadingAppInfo); OpenedForegroundUrls.Remove(loadingAppInfo); }); }); }
public void WeakEventTest() { int val = 0; TestTarget target = new TestTarget { AddToSum = (i) => { val += i; } }; myTarget = target; TestClass tc = new TestClass(); tc.TestEvent += WeakEventHandler.Create(target.Test, e => tc.TestEvent -= e); tc.RaiseTestEvent(); Assert.AreEqual(1, val); Assert.AreEqual(1, tc.GetInvocationList().Length); target = null; GC.Collect(2, GCCollectionMode.Forced); tc.RaiseTestEvent(); Assert.AreEqual(2, val); Assert.AreEqual(1, tc.GetInvocationList().Length); myTarget = null; GC.Collect(2, GCCollectionMode.Forced); val = 0; tc.RaiseTestEvent(); Assert.AreEqual(0, val); Assert.AreEqual(0, tc.GetInvocationList().Length); }
internal TextTracker( Workspace workspace, DocumentId documentId, SourceTextContainer textContainer, Action <Workspace, DocumentId, SourceText> onChangedHandler) { _workspace = workspace; _documentId = documentId; this.TextContainer = textContainer; _onChangedHandler = onChangedHandler; // use weak event so TextContainer cannot accidentally keep workspace alive. _weakOnTextChanged = WeakEventHandler <TextChangeEventArgs> .Create(this, (target, sender, args) => target.OnTextChanged(sender, args)); }