/// <summary> /// Replaces the strings in the Text property with tokens. /// </summary> private void ReplaceTextWithTokens() { // The "Text" property is not linked to the RichTextBox contents, thus we need to clear the RichTextBox // and add each token individually to the contents. this.Clear(); var tokens = new ObservableKeyedCollection <string, Token>(token => token.Key); if (!string.IsNullOrEmpty(Text)) { Paragraph para = this.CaretPosition.Paragraph ?? new Paragraph(); if (para != null) { string[] text = Text.Split(new[] { this.TokenDelimiter }, StringSplitOptions.RemoveEmptyEntries); foreach (string t in text) { var token = this.GetTokenByItemSource(t); token.Content = this.GetContentByToken(token); InlineUIContainer tokenContainer = this.CreateTokenContainer(token); para.Inlines.Add(tokenContainer); tokens.Add(token); } } if (!this.Document.Blocks.Contains(para)) { this.Document.Blocks.Add(para); } } this.Tokens = tokens; }
private void RemoveCombatants <T> (ObservableKeyedCollection <string, T> combatants, IEnumerable <Combatant> victims) where T : Combatant { foreach (var victim in victims) { combatants.Remove(victim.Handle); } }
public void IListOfTInsertShouldThrowException() { // arrange var source = new ObservableKeyedCollection<string, object>( o => o.ToString() ); IList<object> target = new ReadOnlyObservableKeyedCollection<string, object>( source ); // act Assert.Throws<NotSupportedException>( () => target.Insert( 0, new object() ) ); // assert }
public Encounter(string adventure, string name, ObservableKeyedCollection<string, CombatantWrapper> combatants) { // mostly here as a helper for JSON.Net this.Adventure = adventure; this.Name = name; if (combatants == null) { this.Combatants = new ObservableKeyedCollection<string, CombatantWrapper>(c => c.Handle); } else { this.Combatants = combatants; } }
public Encounter(string adventure, string name, ObservableKeyedCollection <string, CombatantWrapper> combatants) { // mostly here as a helper for JSON.Net this.Adventure = adventure; this.Name = name; if (combatants == null) { this.Combatants = new ObservableKeyedCollection <string, CombatantWrapper>(c => c.Handle); } else { this.Combatants = combatants; } }
public Ship() { GibGraphics = new List <string>(); Rooms = new ObservableKeyedCollection <int, Room>(r => r.ID); Rooms.CollectionChanged += (sender, e) => DoShipModified(new ShipModifiedEventArgs(ShipModifiedEventArgs.ShipModifiedAction.Rooms, e)); Doors = new ObservableCollectionEx <Door>(); Doors.CollectionChanged += (sender, e) => DoShipModified(new ShipModifiedEventArgs(ShipModifiedEventArgs.ShipModifiedAction.Doors, e)); Crew = new ObservableCollectionEx <CrewMember>(); Crew.CollectionChanged += (sender, e) => DoShipModified(new ShipModifiedEventArgs(ShipModifiedEventArgs.ShipModifiedAction.Crew, e)); //Weapons = new List<string>(); }
public void IndexerShouldReturnExpectedValue() { var source = new ObservableKeyedCollection<string, object>( o => o.ToString() ); var target = new ReadOnlyObservableKeyedCollection<string, object>( source ); var expected = new object(); source.Add( expected ); var actual = target[0]; Assert.Equal( expected, actual ); actual = target[expected.ToString()]; Assert.Equal( expected, actual ); }
/// <summary> /// Removes the token by key. /// </summary> /// <param name="key">The key.</param> /// <returns></returns> private Token RemoveTokenByKey(string key) { if (this.Tokens.Contains(key)) { var token = this.Tokens[key]; var tokens = new ObservableKeyedCollection <string, Token>(t => t.Key); tokens.AddRange(this.Tokens.Where(o => !o.Key.Equals(key))); this.Tokens = tokens; return(token); } return(null); }
public void IndexOfShouldReturnExpectedValue() { // arrange var source = new ObservableKeyedCollection<string, object>( o => o.ToString() ); var target = new ReadOnlyObservableKeyedCollection<string, object>( source ); var item = new object(); var expected = 0; source.Add( item ); // act var actual = target.IndexOf( item ); // assert Assert.Equal( expected, actual ); }
/// <summary> /// Load Favorites quotes from IO /// </summary> /// <returns>True if the data has been loaded</returns> public static async Task <ObservableKeyedCollection> LoadFavoritesCollection() { try { ObservableKeyedCollection collection = await DataSerializer <ObservableKeyedCollection> .RestoreObjectsAsync("FavoritesCollection.xml"); if (collection != null) { return(collection); } else { return(new ObservableKeyedCollection());; } } catch (IsolatedStorageException exception) { return(new ObservableKeyedCollection()); } }
public void AddShouldInsertNewItem() { // arrange var expectedProperties = new[] { "Count", "Item[]" }; var actualProperties = new List<string>(); var target = new ObservableKeyedCollection<string, int>( i => i.ToString() ); target.PropertyChanged += ( s, e ) => actualProperties.Add( e.PropertyName ); // act target.Add( 1 ); // assert Assert.Equal( 1, target.Count ); Assert.Equal( 1, target[0] ); Assert.True( actualProperties.SequenceEqual( expectedProperties ) ); }
public void RemoveShouldClearItemFromCollection() { // arrange var expectedProperties = new[] { "Count", "Item[]" }; var actualProperties = new List<string>(); var target = new ObservableKeyedCollection<string, int>( i => i.ToString() ); target.Add( 1 ); target.PropertyChanged += ( s, e ) => actualProperties.Add( e.PropertyName ); // act Assert.True( target.Remove( 1 ) ); // assert Assert.Equal( 0, target.Count ); Assert.True( actualProperties.SequenceEqual( expectedProperties ) ); }
public void IListIndexerShouldThrowExceptionOnWrite() { // arrange var source = new ObservableKeyedCollection<string, object>( o => o.ToString() ); IList target = new ReadOnlyObservableKeyedCollection<string, object>( source ); source.Add( new object() ); // act Assert.Throws<NotSupportedException>( () => target[0] = new object() ); // assert }
public void ICollectionOfTClearShouldThrowException() { var source = new ObservableKeyedCollection<string, object>( o => o.ToString() ); ICollection<object> target = new ReadOnlyObservableKeyedCollection<string, object>( source ); source.Add( new object() ); Assert.Throws<NotSupportedException>( () => target.Clear() ); }
public void IListRemoveShouldThrowException() { // arrange var source = new ObservableKeyedCollection<string, object>( o => o.ToString() ); IList target = new ReadOnlyObservableKeyedCollection<string, object>( source ); var item = new object(); source.Add( item ); // act Assert.Throws<NotSupportedException>( () => target.Remove( item ) ); // assert }
public void CopyToShouldCopySourceItems() { // arrange var source = new ObservableKeyedCollection<string, object>( o => o.GetHashCode().ToString() ); var target = new ReadOnlyObservableKeyedCollection<string, object>( source ); var expected = new[] { new object(), new object(), new object() }; var actual = new object[3]; source.AddRange( expected ); // act target.CopyTo( actual, 0 ); // assert Assert.True( actual.SequenceEqual( expected ) ); }
public void IListIsReadOnlyShouldAlwaysReturnTrue() { // arrange var source = new ObservableKeyedCollection<string, object>( o => o.ToString() ); IList target = new ReadOnlyObservableKeyedCollection<string, object>( source ); // act var actual = target.IsReadOnly; // assert Assert.True( actual ); }
public void IListIndexOfShouldNotMatchIncompatibleItemType() { // arrange var source = new ObservableKeyedCollection<string, string>( o => o.ToString() ); IList target = new ReadOnlyObservableKeyedCollection<string, string>( source ); var expected = -1; source.Add( "test" ); // act var actual = target.IndexOf( new object() ); // assert Assert.Equal( expected, actual ); }
public void IListContainsShouldNotMatchIncompatibleItemType() { // arrange var source = new ObservableKeyedCollection<string, string>( o => o.ToString() ); IList target = new ReadOnlyObservableKeyedCollection<string, string>( source ); var item = "test"; source.Add( item ); // act var actual = target.Contains( new object() ); // assert Assert.False( actual ); }
public void IListContainsShouldMatchSourceCollection() { // arrange var source = new ObservableKeyedCollection<string, object>( o => o.ToString() ); IList target = new ReadOnlyObservableKeyedCollection<string, object>( source ); var item = new object(); source.Add( item ); // act // assert Assert.True( target.Contains( item ) ); Assert.False( target.Contains( new object() ) ); }
public void IndexerShouldReplaceItem() { // arrange var target = new ObservableKeyedCollection<string, int>( i => i.ToString() ); target.Add( 1 ); // act Assert.PropertyChanged( target, "Item[]", () => target[0] = 2 ); // assert Assert.Equal( 2, target[0] ); }