本文整理汇总了C#中ObservableDictionary.Remove方法的典型用法代码示例。如果您正苦于以下问题:C# ObservableDictionary.Remove方法的具体用法?C# ObservableDictionary.Remove怎么用?C# ObservableDictionary.Remove使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ObservableDictionary
的用法示例。
在下文中一共展示了ObservableDictionary.Remove方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DeleteItem
public void DeleteItem()
{
_handle = new EventWaitHandle(false, EventResetMode.ManualReset);
var test = new ObservableDictionary<int, string>();
test.ItemRemoved += Dictionary_ItemEvent;
test.Add(0, "myValue");
test.Remove(0);
Assert.IsTrue(_handle.WaitOne(10));
}
示例2: ToDictionary_Unset
public void ToDictionary_Unset()
{
var source = new ObservableDictionary<int, string>();
var get2 = Nothing<string>();
source.ToLiveLinq()[2].Subscribe(val => get2 = val);
get2.Should().Be(Nothing<string>());
source.Add(2, "Hi there");
get2.Should().Be(Something("Hi there"));
source.Remove(2);
get2.Should().Be(Nothing<string>());
}
开发者ID:ApocalypticOctopus,项目名称:Apocalyptic.Utilities.Net,代码行数:11,代码来源:DictionaryLiveLinqQueryTests.cs
示例3: ObservableDictionary_RaisesCollectionItemRemoved
public void ObservableDictionary_RaisesCollectionItemRemoved()
{
var dict = new ObservableDictionary<String, Int32>();
var removed = false;
dict.CollectionItemRemoved += (dictionary, key, value) =>
{
removed = (key == "Testing" && value == 1234);
};
dict["Testing"] = 1234;
dict.Remove("Testing");
TheResultingValue(removed).ShouldBe(true);
}
示例4: ChangeCollection
public void ChangeCollection()
{
_handle = new EventWaitHandle(false, EventResetMode.ManualReset);
var test = new ObservableDictionary<int, string>();
test.Changed += Dictionary_Changed;
test.Add(0, "myValue");
Assert.IsTrue(_handle.WaitOne(10), "Add() is not recognized as a change");
_handle.Reset();
test[0] = "newValue";
Assert.IsTrue(_handle.WaitOne(10), "this[] is not recognized as a change");
_handle.Reset();
test.Remove(0);
Assert.IsTrue(_handle.WaitOne(10), "Remove() is not recognized as a change");
}
示例5: ShouldNotNotifySubscribersAboutValueChangesAfterItemsAreRemovedFromDictionary
public void ShouldNotNotifySubscribersAboutValueChangesAfterItemsAreRemovedFromDictionary()
{
// given
var scheduler = new TestScheduler();
int key = 1;
var testInpcImplementationInstance = new MyNotifyPropertyChanged<int, string>(key);
var observer = scheduler.CreateObserver<IObservableDictionaryChange<int, MyNotifyPropertyChanged<int, string>>>();
var itemChangesObserver = scheduler.CreateObserver<IObservableDictionaryChange<int, MyNotifyPropertyChanged<int, string>>>();
using (var observableDictionary = new ObservableDictionary<int, MyNotifyPropertyChanged<int, string>>(scheduler: scheduler))
{
observableDictionary.ThresholdAmountWhenChangesAreNotifiedAsReset = int.MaxValue;
IDisposable dictionaryChangesSubscription = null;
IDisposable dictionaryItemChangesSubscription = null;
try
{
dictionaryChangesSubscription = observableDictionary.DictionaryChanges.Subscribe(observer);
dictionaryItemChangesSubscription = observableDictionary.ValueChanges.Subscribe(itemChangesObserver);
// when
observableDictionary.Add(key, testInpcImplementationInstance); // first general message - ItemAdd
testInpcImplementationInstance.FirstProperty = Guid.NewGuid().ToString(); // second general / first item change message - ItemChanged
observableDictionary.Remove(key); // third general message - ItemRemoved
testInpcImplementationInstance.SecondProperty = Guid.NewGuid().ToString(); // should no longer be observable on/via dictionary
scheduler.AdvanceBy(100);
// then
observer.Messages.Count.Should().Be(3);
observer.Messages[0].Value.Value.ChangeType.Should().Be(ObservableDictionaryChangeType.ItemAdded);
observer.Messages[0].Value.Value.Key.Should().Be(key);
observer.Messages[0].Value.Value.Value.Should().Be(testInpcImplementationInstance);
observer.Messages[1].Value.Value.ChangeType.Should().Be(ObservableDictionaryChangeType.ItemValueChanged);
observer.Messages[1].Value.Value.Key.Should().Be(default(int));
observer.Messages[1].Value.Value.Value.Should().Be(testInpcImplementationInstance);
observer.Messages[1].Value.Value.OldValue.Should().BeNull();
observer.Messages[1].Value.Value.ChangedPropertyName.Should().Be(nameof(MyNotifyPropertyChanged<int, string>.FirstProperty));
observer.Messages[2].Value.Value.ChangeType.Should().Be(ObservableDictionaryChangeType.ItemRemoved);
observer.Messages[2].Value.Value.Key.Should().Be(key);
observer.Messages[2].Value.Value.Value.Should().Be(testInpcImplementationInstance);
itemChangesObserver.Messages.Count.Should().Be(1);
itemChangesObserver.Messages.First().Value.Value.ChangeType.Should().Be(ObservableDictionaryChangeType.ItemValueChanged);
itemChangesObserver.Messages.First().Value.Value.Key.Should().Be(default(int));
itemChangesObserver.Messages.First().Value.Value.Value.Should().Be(testInpcImplementationInstance);
itemChangesObserver.Messages.First().Value.Value.OldValue.Should().BeNull();
itemChangesObserver.Messages.First().Value.Value.ChangedPropertyName.Should().Be(nameof(MyNotifyPropertyChanged<int, string>.FirstProperty));
}
finally
{
dictionaryChangesSubscription?.Dispose();
dictionaryItemChangesSubscription?.Dispose();
}
}
}
示例6: RemoveRaisesPropertyChangedEventForItemIndexerAndCount
public void RemoveRaisesPropertyChangedEventForItemIndexerAndCount()
{
// given
using (var observableDictionary = new ObservableDictionary<int, string>())
{
observableDictionary.Add(1, "One");
observableDictionary.MonitorEvents();
// when
observableDictionary.Remove(1);
// then
observableDictionary
.ShouldRaise(nameof(observableDictionary.PropertyChanged))
.WithSender(observableDictionary)
.WithArgs<PropertyChangedEventArgs>(args => args.PropertyName == "Item[]");
observableDictionary
.ShouldRaise(nameof(observableDictionary.PropertyChanged))
.WithSender(observableDictionary)
.WithArgs<PropertyChangedEventArgs>(args => args.PropertyName == nameof(observableDictionary.Count));
}
}
示例7: SuppressCountChangedNotificationsSuppressesCountChangedNotifications
public void SuppressCountChangedNotificationsSuppressesCountChangedNotifications()
{
// given
var scheduler = new TestScheduler();
var countChangesObserver = scheduler.CreateObserver<int>();
using (var observableDictionary = new ObservableDictionary<int, string>(scheduler: scheduler))
{
// when
observableDictionary.ThresholdAmountWhenChangesAreNotifiedAsReset = int.MaxValue;
IDisposable countChangesSubscription = null;
try
{
countChangesSubscription = observableDictionary.CountChanges.Subscribe(countChangesObserver);
using (observableDictionary.SuppressCountChangeNotifications(false))
{
observableDictionary.Add(1, "One");
observableDictionary.Add(2, "Two");
observableDictionary.Remove(1);
observableDictionary.Remove(2);
scheduler.AdvanceBy(4);
}
scheduler.AdvanceBy(1);
// then
countChangesObserver.Messages.Should().BeEmpty();
}
finally
{
countChangesSubscription?.Dispose();
}
}
}
示例8: RemoveNotifiesRemovalAsResetIfRequestedTest
public void RemoveNotifiesRemovalAsResetIfRequestedTest(int initialDictionarySize, int amountOfItemsToRemove)
{
if (amountOfItemsToRemove > initialDictionarySize)
throw new ArgumentOutOfRangeException(nameof(amountOfItemsToRemove), $"Must be less than {nameof(initialDictionarySize)}");
// given
var scheduler = new TestScheduler();
var initialValues = Enumerable.Range(0, initialDictionarySize).ToDictionary(item => item, item => $"#{item}");
var observer = scheduler.CreateObserver<IObservableDictionaryChange<int, string>>();
var resetsObserver = scheduler.CreateObserver<Unit>();
using (var observableDictionary = new ObservableDictionary<int, string>(initialValues, scheduler: scheduler))
{
// when
observableDictionary.ThresholdAmountWhenChangesAreNotifiedAsReset = 0;
using (observableDictionary.DictionaryChanges.Subscribe(observer))
{
using (observableDictionary.Resets.Subscribe(resetsObserver))
{
var removedKeyValuePairs = new List<KeyValuePair<int, string>>();
for (int i = 0; i < amountOfItemsToRemove; i++)
{
var lastEntry = observableDictionary.Last();
observableDictionary.Remove(lastEntry.Key);
removedKeyValuePairs.Add(lastEntry);
scheduler.AdvanceBy(2);
}
// then
observableDictionary.Count.Should().Be(initialDictionarySize - amountOfItemsToRemove);
observer.Messages.Count.Should().Be(amountOfItemsToRemove);
resetsObserver.Messages.Count.Should().Be(amountOfItemsToRemove);
if (initialDictionarySize > 0)
{
observer.Messages.Select(message => message.Value.Value.ChangeType).Should().OnlyContain(changeType => changeType == ObservableDictionaryChangeType.Reset);
observer.Messages.Select(message => message.Value.Value.Key).Should().Match(ints => ints.All(@int => Equals(default(int), @int)));
observer.Messages.Select(message => message.Value.Value.Value).Should().Match(strings => strings.All(@string => Equals(default(string), @string)));
}
}
}
}
}
示例9: RemoveNotifiesCountDecrease
public void RemoveNotifiesCountDecrease(int initialDictionarySize, int amountOfItemsToRemove)
{
if (amountOfItemsToRemove > initialDictionarySize)
throw new ArgumentOutOfRangeException(nameof(amountOfItemsToRemove), $"Must be less than {nameof(initialDictionarySize)}");
// given
var initialValues = Enumerable.Range(0, initialDictionarySize).ToDictionary(item => item, item => $"#{item}");
int observableReportedCount = initialValues.Count;
int countChangesCalled = 0;
using (var observableDictionary = new ObservableDictionary<int, string>(initialValues))
{
// when
observableDictionary.ThresholdAmountWhenChangesAreNotifiedAsReset = int.MaxValue;
observableDictionary.CountChanges.Subscribe(i =>
{
observableReportedCount = i;
countChangesCalled++;
});
for (int i = 0; i < amountOfItemsToRemove; i++)
{
observableDictionary.Remove(observableDictionary.Last().Key);
}
// then check whether all items have been accounted for
var expectedCount = initialDictionarySize - amountOfItemsToRemove;
observableReportedCount.Should().Be(expectedCount); // +1 because the upper for loop goes up to & inclusive the upperLimit
observableReportedCount.Should().Be(observableDictionary.Count);
countChangesCalled.Should().Be(amountOfItemsToRemove);
}
}
示例10: RemoveOfKeyThrowsOnNullKey
public void RemoveOfKeyThrowsOnNullKey()
{
// given
var initialKvPs = new List<KeyValuePair<string, string>>()
{
new KeyValuePair<string, string>("1", "One"),
new KeyValuePair<string, string>("2", "Two")
};
using (var observableDictionary = new ObservableDictionary<string, string>(initialKvPs))
{
// when
Action action = () => observableDictionary.Remove((string) null);
// then
action
.ShouldThrow<ArgumentNullException>()
.WithMessage("Value cannot be null.\r\nParameter name: key");
observableDictionary.Count.Should().Be(2);
}
}
示例11: RemoveOfKeyShouldReportBackCorrespondinglyOnNonExistingItems
public void RemoveOfKeyShouldReportBackCorrespondinglyOnNonExistingItems()
{
// given
var initialKvPs = new List<KeyValuePair<int, string>>()
{
new KeyValuePair<int, string>(1, "One"),
new KeyValuePair<int, string>(2, "Two")
};
using (var observableDictionary = new ObservableDictionary<int, string>(initialKvPs))
{
// when
var removalResult = observableDictionary.Remove(10);
// then
removalResult.Should().Be(false);
observableDictionary.Count.Should().Be(2);
}
}
示例12: RemoveOfKeyShouldNotThrowOnNonExistingItem
public void RemoveOfKeyShouldNotThrowOnNonExistingItem()
{
// given
var initialKvPs = new List<KeyValuePair<int, string>>()
{
new KeyValuePair<int, string>(1, "One"),
new KeyValuePair<int, string>(2, "Two")
};
using (var observableDictionary = new ObservableDictionary<int, string>(initialKvPs))
{
// when
Action invalidRemoveRangeForNonExistingKey = () => observableDictionary.Remove(10);
// then
invalidRemoveRangeForNonExistingKey
.ShouldNotThrow<ArgumentOutOfRangeException>();
observableDictionary.Count.Should().Be(2);
}
}
示例13: RemoveOfKeyRemovesExistingItem
public void RemoveOfKeyRemovesExistingItem()
{
// given
var initialKvPs = new List<KeyValuePair<int, string>>()
{
new KeyValuePair<int, string>(1, "One")
};
using (var observableDictionary = new ObservableDictionary<int, string>(initialKvPs))
{
// when
observableDictionary.Remove(1);
// then check whether all items have been accounted for
observableDictionary.Count.Should().Be(0);
observableDictionary.Should().NotContain(1, "One");
observableDictionary.Keys.Should().NotContain(1);
observableDictionary.Values.Should().NotContain("One");
}
}
示例14: ObservableDictionaryRemoveKeyTest
public void ObservableDictionaryRemoveKeyTest()
{
ObservableDictionary<GenericParameterHelper, GenericParameterHelper> target = new ObservableDictionary<GenericParameterHelper, GenericParameterHelper>();
KeyValuePair<GenericParameterHelper, GenericParameterHelper> itemAdded = new KeyValuePair<GenericParameterHelper, GenericParameterHelper>(new GenericParameterHelper(1), new GenericParameterHelper(1));
KeyValuePair<GenericParameterHelper, GenericParameterHelper> itemNonAdded = new KeyValuePair<GenericParameterHelper, GenericParameterHelper>(new GenericParameterHelper(2), new GenericParameterHelper(2));
target.Add(itemAdded);
Assert.AreEqual(1, target.Count);
target.CollectionChanged += delegate(object sender, NotifyCollectionChangedEventArgs e) {
Assert.AreEqual(NotifyCollectionChangedAction.Remove, e.Action);
Assert.IsNotNull(e.OldItems);
};
bool expectedAdded = true;
bool expectedNotAdded = false;
bool actualAdded = target.Remove(new GenericParameterHelper(1));
bool actualNotAdded = target.Remove(new GenericParameterHelper(2));
Assert.AreEqual(expectedAdded, actualAdded);
Assert.AreEqual(expectedNotAdded, actualNotAdded);
Assert.AreEqual(0, target.Count);
}