Click here to Skip to main content
15,891,316 members
Articles / Programming Languages / C#

Silverlight Application with MVVM WCF and EF

Rate me:
Please Sign up or sign in to vote.
3.82/5 (16 votes)
17 Jun 2012CPOL4 min read 78.2K   3.6K   21  
How to create a simple Silverlight application using the MVVM pattern with WCF and Entity Framework.
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Collections.ObjectModel;
using SilverlightMVVMwithWCFandLinq.MyServiceReference;

namespace SilverlightMVVMwithWCFandLinq
{
    public class MainViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private ObservableCollection<SchoolClass> _SchoolClasses;
        private ObservableCollection<string> _Classes;
        public ObservableCollection<SchoolClass> SchoolClasses
        {
            get
            {
                return _SchoolClasses;
            }
            set
            {
                _SchoolClasses = value;
                NotifyPropertyChanged("SchoolClasses");
            }
        }


        public ObservableCollection<string> Classes
        {
            get
            {
                return _Classes;
            }
            set
            {
                _Classes = value;
                NotifyPropertyChanged("Classes");
            }

        }

        public MainViewModel()
        {
            MyServiceReference.Service1Client client = new MyServiceReference.Service1Client();

            client.GetClassesCompleted += new EventHandler<MyServiceReference.GetClassesCompletedEventArgs>(client_GetClassesCompleted);
            client.GetClassesAsync();
            client.GetSchoolClassesCompleted += new EventHandler<GetSchoolClassesCompletedEventArgs>(client_GetSchoolClassesCompleted);
            client.GetSchoolClassesAsync();
        }

        void client_GetSchoolClassesCompleted(object sender, GetSchoolClassesCompletedEventArgs e)
        {
            SchoolClasses = e.Result;
        }

        void client_GetClassesCompleted(object sender, MyServiceReference.GetClassesCompletedEventArgs e)
        {
            Classes = e.Result;
        }


        protected void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }


    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead EF (Education First)
India India
I graduated as Production Engineer and started my career as Software Developer then worked as tester for a while before moving into Windows application development using Microsoft Technologies. But for the last few years i am working on javascript, React, Node, AWS, Azure Chatbots

Comments and Discussions