示例#1
0
        public void FirstOrDefault_ObservableSourceNewFirstItemAdded_Update()
        {
            var update = false;
            var coll   = new NotifyCollection <string>()
            {
                "23"
            };
            var collCasted = (INotifyCollection <string>)coll;

            var test = Observable.Expression(() => collCasted.FirstOrDefault());

            test.ValueChanged += (o, e) =>
            {
                update = true;
                Assert.AreEqual("42", e.NewValue);
                Assert.AreEqual("23", e.OldValue);
            };

            Assert.AreEqual("23", test.Value);
            Assert.IsFalse(update);

            coll.Insert(0, "42");

            Assert.IsTrue(update);
            Assert.AreEqual("42", test.Value);
        }
示例#2
0
        public void FirstOrDefault_ObservableSourceFirstItemAdded_NoUpdateWhenDetached()
        {
            var update = false;
            var coll   = new NotifyCollection <string>();

            var test = Observable.Expression(() => coll.FirstOrDefault());

            test.ValueChanged += (o, e) => update = true;

            Assert.IsNull(test.Value);
            Assert.IsFalse(update);

            test.Detach();
            update = false;

            coll.Add("42");

            Assert.IsFalse(update);

            test.Attach();

            Assert.IsTrue(update);
            Assert.AreEqual("42", test.Value);
            update = false;

            coll.Insert(0, "23");

            Assert.IsTrue(update);
        }
示例#3
0
 static void Main(string[] args)
 {
     NotifyCollection nc = new NotifyCollection();
     nc.PropertyChanged += (s,e) =>
     {
         Console.WriteLine(e.PropertyName);
     };
     nc.Add(1);
     nc.Add(2);
     nc.Add(3);
     nc.Insert(2, 2);
     nc.Add(4);
     nc.Remove(4);
 }
        static void Main ( string[] args )
        {
            Delegates del = new Delegates();
            List<int> numbers = new List<int> { 5 , 4 , 8 , 7 , 9 , 6 , 6 , 6 , 3 , 1 , 20 };
            List<int> evens = del.FilterCollection(numbers , IsEven);
            foreach(var item in evens)
            {
                Console.Write(item + " ");
            }
            Console.WriteLine();
            AverageAggregator aggregator = new AverageAggregator();
            aggregator.AverageChanged += delegate ( object sender )
            {
                if(sender is AverageAggregator)
                {
                    AverageAggregator newSender = (AverageAggregator)sender;
                    Console.WriteLine("Average has changed!\nNew value: {0}" , newSender.Average);
                }
            };
            aggregator.AddNumber(6);
            aggregator.AddNumber(4);
            aggregator.AddNumber(2);
            aggregator.AddNumber(3);
            Student stu = new Student();
            stu.PropertyChanged += PropChange;
            stu.Name = "Anton";
            stu.FacultyNumber = 65888;
            stu.Grade = 5.58;

            NotifyCollection<int> newList = new NotifyCollection<int>();
            newList.PropertyChanged += (object sender, PropertyChangedEventArgs e)=>
        {
            Console.WriteLine("The list has changed!\n{0}" , e.PropertyName);
        };
            newList.Add(5);
            newList.Add(12);
            newList.Insert(1 , 20);
            newList.Remove(12);
            Console.ReadKey();
        }
示例#5
0
        public void FirstOrDefault_ObservableSourceNewFirstItemAdded_Update()
        {
            var update = false;
            var coll = new NotifyCollection<string>() { "23" };

            var test = Observable.Expression(() => coll.FirstOrDefault());

            test.ValueChanged += (o, e) =>
            {
                update = true;
                Assert.AreEqual("42", e.NewValue);
                Assert.AreEqual("23", e.OldValue);
            };

            Assert.AreEqual("23", test.Value);
            Assert.IsFalse(update);

            coll.Insert(0, "42");

            Assert.IsTrue(update);
            Assert.AreEqual("42", test.Value);
        }
示例#6
0
        static void Main(string[] args)
        {
            Console.InputEncoding = Encoding.Unicode;
            Console.OutputEncoding = Encoding.Unicode;
            AverageAggregator averageAgg = new AverageAggregator
                (delegate (object sender, decimal oldAverage, decimal newAverage)
                {
                    Console.Write("--->Handler: ");
                    Console.WriteLine("Average changed from {0} to {1}.", oldAverage, newAverage);
                });

            Console.WriteLine("---AverageAggregator---");
            Console.WriteLine();

            Console.WriteLine("Adding number to aggregator: 2");
            averageAgg.AddNumber(2);
            Console.WriteLine("Adding number to aggregator: 2");
            averageAgg.AddNumber(2);
            Console.WriteLine("Adding number to aggregator: 8");
            averageAgg.AddNumber(8);
            Console.WriteLine("Adding number to aggregator: 4");
            averageAgg.AddNumber(4);
            Console.WriteLine("Adding number to aggregator: 4");
            averageAgg.AddNumber(4);
            Console.WriteLine("Adding number to aggregator: 4");
            averageAgg.AddNumber(4);
            Console.WriteLine("Adding number to aggregator: -24");
            averageAgg.AddNumber(-24);
            Console.WriteLine("Adding number to aggregator: 0");
            averageAgg.AddNumber(0);
            Console.WriteLine("Adding number to aggregator: 0");
            averageAgg.AddNumber(0);
            Console.WriteLine("Adding number to aggregator: 10");
            averageAgg.AddNumber(10);
            Console.WriteLine("Adding number to aggregator: 1");
            averageAgg.AddNumber(1);
            Console.WriteLine("Adding number to aggregator: 1");
            averageAgg.AddNumber(1);
            Console.WriteLine("Adding number to aggregator: 2");
            averageAgg.AddNumber(2);

            Console.WriteLine();
            Console.WriteLine("---NotifyCollection---");
            Console.WriteLine();

            NotifyCollection<Book> notCol = new NotifyCollection<Book>
                ((sender, changeType, changedItemIndex, changedItemInfo) =>
                {
                    Console.Write("--->Handler: ");
                    switch (changeType)
                    {
                        case ItemChangeType.Add:
                            Console.WriteLine("Added item to collection on {0} index!", changedItemIndex);
                            break;
                        case ItemChangeType.Insert:
                            Console.WriteLine("Inserted item in collection on {0} index!", changedItemIndex);
                            break;
                        case ItemChangeType.Remove:
                            if (changedItemIndex == -1) Console.WriteLine("Collection items cleared!");
                            else Console.WriteLine("Removed item from collection being on {0} index!", changedItemIndex);
                            break;
                        case ItemChangeType.Replace:
                            Console.WriteLine("Replaced item in collection being on {0} index!", changedItemIndex);
                            break;
                        case ItemChangeType.ChangedProperty:
                            Console.WriteLine("Item on {0} index property {1} changed!", changedItemIndex, changedItemInfo);
                            break;
                        default:
                            break;
                    }
                });

            Book book1 = new Book("author1", "book1", 2000);
            Book book2 = new Book("author2", "book2", 2001);
            Book book3 = new Book("author3", "book3", 2002);
            Console.WriteLine("Adding {0}!", book1);
            notCol.Add(book1);
            Console.WriteLine("Adding {0}!", book2);
            notCol.Add(book2);
            Console.WriteLine("Inserting {0} on index 1!", book3);
            notCol.Insert(1, book3);
            Console.WriteLine("Changing {0} year to 2011!", book1);
            book1.Year = 2011;
            Console.WriteLine("Changing {0} year to 2005!", book3);
            book3.Year = 2005;
            Console.WriteLine("Removing {0}!", book2);
            notCol.Remove(book2);
            Console.WriteLine("Changing removed book year to 1995!");
            book2.Year = 1995;
            Console.WriteLine("Clearing all items!");
            notCol.Clear();
            Console.WriteLine("Adding {0}!", book1);
            notCol.Add(book1);
            Console.WriteLine("Adding {0}!", book2);
            notCol.Add(book2);
            Console.WriteLine("Replacing book on 0 index with {0}", book3);
            notCol[0] = book3;
            Console.WriteLine("Changing old book year to 1895!");
            book1.Year = 1895;
            Console.WriteLine("Changing new book year to 2003!");
            book3.Year = 2003;

            Console.WriteLine();
            Console.WriteLine("---Network Buffer---");

            ReceiveBuffer rec = new ReceiveBuffer(MessageReceivedHandle);
            PacketGenerator pg = new PacketGenerator(rec);
            Console.WriteLine("Write messages. Write exit for exit.");
            Console.Write("Message to send: ");
            string inputMessage = Console.ReadLine();
            while(!inputMessage.Equals("exit"))
            {
                pg.SendMessage(inputMessage);
                Console.Write("Message to send: ");
                inputMessage = Console.ReadLine();
            }
        }
示例#7
0
        public void FirstOrDefault_ObservableSourceFirstItemAdded_NoUpdateWhenDetached()
        {
            var update = false;
            var coll = new NotifyCollection<string>();

            var test = Observable.Expression(() => coll.FirstOrDefault());

            test.ValueChanged += (o, e) => update = true;

            Assert.IsNull(test.Value);
            Assert.IsFalse(update);

            test.Detach();
            update = false;

            coll.Add("42");

            Assert.IsFalse(update);

            test.Attach();

            Assert.IsTrue(update);
            Assert.AreEqual("42", test.Value);
            update = false;

            coll.Insert(0, "23");

            Assert.IsTrue(update);
        }