本文整理汇总了C#中ObservableCollection.Single方法的典型用法代码示例。如果您正苦于以下问题:C# ObservableCollection.Single方法的具体用法?C# ObservableCollection.Single怎么用?C# ObservableCollection.Single使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ObservableCollection
的用法示例。
在下文中一共展示了ObservableCollection.Single方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ObservableFilteredCollection_LiveFilteringTest
public void ObservableFilteredCollection_LiveFilteringTest()
{
var source = new ObservableCollection<TestObject>(Enumerable.Range(0, 10).Select(i => new TestObject(i)));
var target = new ObservableFilteredCollection<TestObject>(source, s => (s.Value & 1) != 0, "Value");
NotifyCollectionChangedEventArgs lastEventArgs = null;
NotifyCollectionChangedEventHandler callback = (_, e) => lastEventArgs = e;
target.CollectionChanged += callback;
Assert.IsTrue(target.Select(t => t.Value).SequenceEqual(new[] { 1, 3, 5, 7, 9 }));
source.RemoveRange(o => o.Value == 2);
Assert.IsTrue(target.Select(t => t.Value).SequenceEqual(new[] { 1, 3, 5, 7, 9 }));
Assert.IsNull(lastEventArgs);
source.RemoveRange(o => o.Value == 3);
Assert.IsTrue(target.Select(t => t.Value).SequenceEqual(new[] { 1, 5, 7, 9 }));
Assert.AreEqual(NotifyCollectionChangedAction.Remove, lastEventArgs.Action);
Assert.AreEqual(3, lastEventArgs.OldItems.Cast<TestObject>().Single().Value);
lastEventArgs = null;
source.Single(o => o.Value == 5).Value = 6;
Assert.IsTrue(target.Select(t => t.Value).SequenceEqual(new[] { 1, 7, 9 }));
Assert.AreEqual(NotifyCollectionChangedAction.Remove, lastEventArgs.Action);
Assert.AreEqual(6, lastEventArgs.OldItems.Cast<TestObject>().Single().Value);
lastEventArgs = null;
source.First(o => o.Value == 6).Value = 5;
Assert.IsTrue(target.Select(t => t.Value).SequenceEqual(new[] { 1, 7, 9, 5 }));
Assert.AreEqual(NotifyCollectionChangedAction.Add, lastEventArgs.Action);
Assert.AreEqual(5, lastEventArgs.NewItems.Cast<TestObject>().Single().Value);
lastEventArgs = null;
}
示例2: ConfigurationViewModel
public ConfigurationViewModel()
{
DisplayName = "CFG";
InstalledFonts = new ObservableCollection<FontFamily>(Fonts.SystemFontFamilies.OrderBy(c => c.Source));
var settings = ConfigurationSerializer.Load();
SelectedFont = string.IsNullOrEmpty(settings.Font) ? InstalledFonts.First() : InstalledFonts.Single(c => c.Source == settings.Font);
}
示例3: InvokeAsyncTest
public async Task InvokeAsyncTest()
{
var ints = new ObservableCollection<int>();
var dispatcherOperation = ints.InvokeAsync(() => ints.Add(3));
Dispatcher.CurrentDispatcher.DoEvents();
await dispatcherOperation;
Assert.AreEqual(3, ints.Single());
}
示例4: ShowArtistSearchResults
internal static void ShowArtistSearchResults(IEnumerable<SynoItem> items)
{
var albumViewModels = new ObservableCollection<ArtistViewModel>(items.Select(o => new ArtistViewModel(o)));
foreach (var viewModel in albumViewModels)
{
var getAlbumsTask = App.audioStationSession.GetAlbumsForArtistAsync(viewModel.Artist);
// TODO : instead of writing the strategy here, we could call a delegate which would then be changeable : this would enable alternative search scenarios for limited bandwidth scenarios.
getAlbumsTask.ContinueWith(task => albumViewModels.Single(o=>o.Artist == task.AsyncState).Albums = new ObservableCollection<AlbumViewModel>(task.Result.Select(o => new AlbumViewModel(o))));
}
var artistSearchViewModel = new ArtistSearchResultsViewModel(albumViewModels);
var page = new ArtistSearchResults(artistSearchViewModel);
Window.Current.Content = page;
}
示例5: IndexOf
/// <summary>
/// Find a tasklist by ID and then return its index
/// </summary>
/// <param name="observableCollection"></param>
/// <param name="taskList"></param>
/// <returns></returns>
private int IndexOf(ObservableCollection<TaskList> lists, TaskList taskList)
{
try
{
TaskList taskListRef = lists.Single(tl => tl.ID == taskList.ID);
return lists.IndexOf(taskListRef);
}
catch (Exception)
{
return -1;
}
}
示例6: CreateTags
public void CreateTags(ObservableCollection<Tag> tagList)
{
var newTags = new ObservableCollection<Tag>();
if (taskTags != null)
{
foreach (var taskTag in taskTags)
{
var foundTag = tagList.Single<Tag>(t => t.ID == taskTag.TagID);
if (foundTag != null)
{
newTags.Add(foundTag);
}
}
}
// store the tag collection (which will invoke setter and trigger databinding)
Tags = newTags;
}
示例7: InitializeFolders
private ObservableCollection<Folder> InitializeFolders()
{
// get the default folders and enqueue an operation to insert each one of them and their subitems
var folders = new ObservableCollection<Folder>(UserConstants.DefaultFolders(null));
foreach (var folder in folders)
{
string queueName = folder.Name.StartsWith("$") ? RequestQueue.SystemQueue : RequestQueue.UserQueue;
FolderDictionary.Add(folder.ID, folder);
RequestQueue.EnqueueRequestRecord(queueName,
new RequestQueue.RequestRecord() { ReqType = RequestQueue.RequestRecord.RequestType.Insert, Body = folder, ID = folder.ID, IsDefaultObject = true });
foreach (var item in folder.Items)
{
RequestQueue.EnqueueRequestRecord(queueName,
new RequestQueue.RequestRecord() { ReqType = RequestQueue.RequestRecord.RequestType.Insert, Body = item, ID = item.ID, IsDefaultObject = true });
}
StorageHelper.WriteFolder(folder);
}
// extract the $Client folder and handle it specially
var cf = folders.Single(f => f.Name == SystemEntities.Client);
folders.Remove(cf);
ClientFolder = cf;
// extract the $PhoneClient folder and handle it specially
var pcf = folders.Single(f => f.Name == SystemEntities.PhoneClient);
folders.Remove(pcf);
PhoneClientFolder = pcf;
// initialize the SelectedCount for a few default folders and lists
/*
foreach (var folder in folders)
{
if (folder.Name == UserEntities.People ||
folder.Name == UserEntities.Places)
{
ListMetadataHelper.IncrementListSelectedCount(pcf, folder);
continue;
}
foreach (var item in folder.Items)
{
if (item.Name == UserEntities.Tasks ||
item.Name == UserEntities.Groceries)
{
ListMetadataHelper.IncrementListSelectedCount(pcf, item);
continue;
}
}
}
*/
return folders;
}
示例8: CreateTags
public void CreateTags(ObservableCollection<Tag> tagList)
{
var newTags = new ObservableCollection<Tag>();
if (itemTags != null)
{
foreach (var itemTag in itemTags)
{
try
{
var foundTag = tagList.Single<Tag>(t => t.ID == itemTag.TagID);
if (foundTag != null)
{
newTags.Add(foundTag);
}
}
catch (Exception)
{
}
}
}
// store the tag collection (which will invoke setter and trigger databinding)
//Tags = newTags;
// don't trigger databinding
tags = newTags;
}