سفارش تبلیغ
صبا ویژن

ساخت مدل


به منظور فراخوانی متد موجود در Web Service یک Model در پروژه کاربردی Silverlight ایجاد کنید.

یک پوشه با نام Model ایجاد کرده و یک کلاس تحت عنوان RIACommentsModel.cs که شامل کدهای زیر باشد به آن اضافه کنید.

using Microsoft.VisualBasic;
using System.Linq;
using System;
using System.Collections.Generic;
using System.ServiceModel;
using RIADataGrid.wsRIARIAComments;
using RIADataGrid;

namespace RIADataGrid
{
    public class RIACommentsModel
    {
        #region GetRIAComments
        public static void GetRIAComments(
           EventHandler<GetRIACommentsCompletedEventArgs> eh)
        {
            // Set up web service call
            WebServiceSoapClient WS = new WebServiceSoapClient();

            // Set the EndpointAddress
            WS.Endpoint.Address = new EndpointAddress(GetBaseAddress());

            WS.GetRIACommentsCompleted += eh;
            WS.GetRIACommentsAsync();
        }
        #endregion

        // Utility

        #region GetBaseAddress
        private static Uri GetBaseAddress()
        {
            // Get the web address of the .xap that launched this application    
            string strBaseWebAddress = App.Current.Host.Source.AbsoluteUri;
            // Find the position of the ClientBin directory       
            int PositionOfClientBin =
                App.Current.Host.Source.AbsoluteUri.ToLower().IndexOf(@"/clientbin");
            // Strip off everything after the ClientBin directory        
            strBaseWebAddress = Strings.Left(strBaseWebAddress, PositionOfClientBin);
            // Create a URI
            Uri UriWebService = new Uri(String.Format(@"{0}/WebService.asmx",
                                        strBaseWebAddress));
            // Return the base address         
            return UriWebService;
        }
        #endregion
    }
}

 

این کلاس به ما این اجازه را خواهد داد که متد وب سرویس GetRIAComment را در View Model فراخوانی کنیم.

ساخت View Model

سپس یک View Model ایجاد می کنیم که منطق برنامه نویسی ما را شامل خواهد شد.

پوشه ای با نام ViewModels ایجاد کرده و کلاسی تحت عنوان MainPageModel.cs که شامل کدهای زیر باشد به آن اضافه کنید:

using System;
using System.Linq;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Input;
using System.Windows;
using RIADataGrid.wsRIARIAComments;
using Microsoft.VisualBasic;
using System.Windows.Controls;
using System.Windows.Data;
using System.Collections.Specialized;
namespace RIADataGrid
{
    public class MainPageModel : INotifyPropertyChanged
    {
        public MainPageModel()
        {
           // The following line prevents Expression Blend
            // from showing an error when in design mode
            if (!DesignerProperties.IsInDesignTool)
            {
                // Get the RIAComments
                GetRIAComments();
           }
        }
        // Operations
         #region GetRIAComments
        private void GetRIAComments()
        {
            // Call the Model to get the collection of RIAComments
            RIACommentsModel.GetRIAComments((Sender, EventArgs) =>
            {
                if (EventArgs.Error == null)
                {
                    // Clear the current RIAComments
                    colRIAComments.Clear();
                     // loop thru each item
                    foreach (var RIAComment in EventArgs.Result)
                    {
                        // Add to the colRIAComments collection
                        colRIAComments.Add(RIAComment);                    }
                }
            });
        }
        #endregion
        // Collections
         #region colRIAComments
        private ObservableCollection<RIAComment> _colRIAComments
            = new ObservableCollection<RIAComment>();
        public ObservableCollection<RIAComment> colRIAComments
        {
            get { return _colRIAComments; }            private set
            {
                if (colRIAComments == value)
                {  return;
  }
                _colRIAComments = value;
                this.NotifyPropertyChanged("colRIAComments");
            }
        }
        #endregion
        // Utility
        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
        #endregion
    }
}

 

 

ما این کلاس و سایر کلاسها را در بخش های بعدی گسترش می دهیم. ولی اکنون فقط یک collection با نام colRIAComments را از نتایج به دست آمده از Web Service پر می کنیم. حال پروژه را Build کنید.

در قسمت سوم View را ایجاد می کنیم.


اولین دیدگاه را شما بگذارید Silverlight ، MVVM ، Expression Blend ،

 حذف ردیف...   

مشخصات مدیر وبلاگ

محمد محمدی پیروز [33]

دل نوشته ها و تجربه های یک برنامه نویس
ویرایش

لوگوی دوستان



ویرایش

طراحی پوسته توسط تیم پارسی بلاگ