本文整理汇总了C#中ObservableDictionary.AddRange方法的典型用法代码示例。如果您正苦于以下问题:C# ObservableDictionary.AddRange方法的具体用法?C# ObservableDictionary.AddRange怎么用?C# ObservableDictionary.AddRange使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ObservableDictionary
的用法示例。
在下文中一共展示了ObservableDictionary.AddRange方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AddRangeRaisesPropertyChangeEventForItemIndexerAndCount
public void AddRangeRaisesPropertyChangeEventForItemIndexerAndCount()
{
// given
using (var observableDictionary = new ObservableDictionary<int, string>())
{
observableDictionary.MonitorEvents();
var items = new List<KeyValuePair<int, string>>()
{
new KeyValuePair<int, string>(1, "One"),
new KeyValuePair<int, string>(2, "Two"),
new KeyValuePair<int, string>(3, "Three"),
};
// when
observableDictionary.AddRange(items);
// 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));
}
}
示例2: AddRangeNotifiesCountChangeForEveryAddedItemIfAddedItemsAreLessThanThreshold
public void AddRangeNotifiesCountChangeForEveryAddedItemIfAddedItemsAreLessThanThreshold()
{
// given
var testScheduler = new TestScheduler();
var countChangesObserver = testScheduler.CreateObserver<int>();
int itemsAmountToAdd = 100;
using (var observableDictionary = new ObservableDictionary<int, string>())
{
observableDictionary.ThresholdAmountWhenChangesAreNotifiedAsReset = itemsAmountToAdd + 1;
observableDictionary.CountChanges.Subscribe(countChangesObserver);
var items = Enumerable.Range(0, itemsAmountToAdd).ToDictionary(i => i, i => i.ToString());
// when
observableDictionary.AddRange(items);
// then
countChangesObserver.Messages.Count.Should().Be(itemsAmountToAdd);
}
}
示例3: using
public void AddRangeRaisesPropertyChangeEventForItemIndexerdAndCountOnlyOnceWhenThresholdAmountWhenItemChangesAreNotifiedAsResetIsSetAccordingly()
{
// given
using (var observableDictionary = new ObservableDictionary<int, string>())
{
observableDictionary.ThresholdAmountWhenChangesAreNotifiedAsReset = 0;
observableDictionary.MonitorEvents();
var items = new List<KeyValuePair<int, string>>()
{
new KeyValuePair<int, string>(1, "One"),
new KeyValuePair<int, string>(2, "Two"),
new KeyValuePair<int, string>(3, "Three"),
};
// when
observableDictionary.AddRange(items);
// then
observableDictionary
.ShouldRaise(nameof(observableDictionary.PropertyChanged))
.WithSender(observableDictionary)
.WithArgs<PropertyChangedEventArgs>(args => args.PropertyName == "Item[]")
.Should().HaveCount(1); // ToDo: re-enable test if/when https://github.com/dennisdoomen/fluentassertions/issues/337 has been fixed
observableDictionary
.ShouldRaise(nameof(observableDictionary.PropertyChanged))
.WithSender(observableDictionary)
.WithArgs<PropertyChangedEventArgs>(args => args.PropertyName == nameof(observableDictionary.Count))
.Should().HaveCount(1); // ToDo: re-enable test if/when https://github.com/dennisdoomen/fluentassertions/issues/337 has been fixed
}
}
示例4: AddRangeOfKeyValuePairsThrowsOnNullItems
public void AddRangeOfKeyValuePairsThrowsOnNullItems()
{
// given
using (var observableDictionary = new ObservableDictionary<int, string>())
{
// when
Action action = () => observableDictionary.AddRange(null);
// then
action
.ShouldThrow<ArgumentNullException>()
.WithMessage("Value cannot be null.\r\nParameter name: items");
observableDictionary.Count.Should().Be(0);
}
}
示例5: AddRangeOfKeyValuePairsThrowsOnNonExistingKeys
public void AddRangeOfKeyValuePairsThrowsOnNonExistingKeys()
{
// given
var keyValuePairs = Enumerable.Range(0, 2)
.Select(i => new KeyValuePair<int, string>(i, $"#{i}"))
.ToList();
using (var observableDictionary = new ObservableDictionary<int, string>(keyValuePairs))
{
// when
Action invalidRemoveRangeForNonExistingKey = ()
=> observableDictionary.AddRange(
new List<KeyValuePair<int, string>>
{
new KeyValuePair<int, string>(0, "#0"),
new KeyValuePair<int, string>(1, "One"),
new KeyValuePair<int, string>(2, "Two")
});
// then
invalidRemoveRangeForNonExistingKey
.ShouldThrow<ArgumentOutOfRangeException>()
.WithMessage("The following key(s) are already in this dictionary and cannot be added to it: 0, 1\r\nParameter name: items");
observableDictionary.Count.Should().Be(3);
observableDictionary.Should().Contain(0, "#0");
observableDictionary.Should().Contain(1, "#1");
observableDictionary.Should().Contain(2, "Two");
}
}
示例6: AddRangeOfKeyValuePairsAddsItems
public void AddRangeOfKeyValuePairsAddsItems(int amountOfItemsToAdd)
{
// given
var keyValuePairs = Enumerable.Range(0, amountOfItemsToAdd)
.Select(i => new KeyValuePair<int, string>(i, $"#{i}"))
.ToList();
using (var observableDictionary = new ObservableDictionary<int, string>())
{
// when
observableDictionary.AddRange(keyValuePairs);
// then check whether all items have been accounted for
observableDictionary.Count.Should().Be(amountOfItemsToAdd);
foreach (var keyValuePair in keyValuePairs)
{
observableDictionary.Should().Contain(keyValuePair);
}
}
}