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

ویرایش درون خطی

در پروژه این امکان را ایجاد خواهیم کرد که بتوان فیلد Comment را به صورت درون خطی (Inline Editing) ویرایش کرد. در اصل، قبلا این امکان فعال شده است! چون ما گزینه IsReadOnly را در هنگام تعریف ستون Comment فعال نکردیم (به صورت un-checked است). حالا تصمیم داریم که Comment های به روز شده را در دیتابیس ذخیره کنیم.
در ضمن، کاری می کنیم که GetRIAComments در Web Service فقط 25 کاراکتر اول فیلد Comment را نشان دهد. این کار موجب می شود که برنامه سریعتر اجرا شود چون داده های کمتری برای نمایش فراخوانی خواهند شد. وقتی کاربر Comment ی را ویرایش می کند، موجب فراخوانی متد Web Service شده و  کل Comment را بر میگرداند و آنرا برای ویرایش در اختیار کاربر قرار می دهد.
همچنین برای اطمینان از این که فقط رکوردی را که از آخرین زمان بازیابی آن تغییری نداشته است، به روزرسانی کنیم، متدی را پیاده سازی خواهیم کرد.

افزودن Errors Property


صفحه Central Silverlight Business Rules Validation نحوه افزودن Errors property به Partial Class یک جدول LINQ to SQL را آموزش داده است.


پوشه ای با نام Classes و فایلی تحت عنوان DataBaseParticalClass.cs را به پروژه اضافه کنید و کدهای زیر را در آن قرار دهید.

using System.Collections.Generic;
namespace RIADataGrid.Web
{
    #region public partial class RIAComment
    public partial class RIAComment
    {
        public List<string> Errors = new List<string>();
    }
    #endregion
}

 

 

 

 

 

 

 

 

تغییر Web Service

 


دو متد زیر را به وب سرویس اضافه کنید.

#region GetRIAComments
[WebMethod]
public List<RIAComment> GetRIAComments()
{
    // Create a collection to hold the results
    List<RIAComment> colResult = new List<RIAComment>();
    RIATasksDBDataContext DB = new RIATasksDBDataContext();
    var colRIAComments = from RIAComments in DB.RIAComments
                         select RIAComments;
    // Loop thru the Tasks
    foreach (var item in colRIAComments)
    {
        // Create a Task
        RIAComment OutputRIAComment = new RIAComment();
        // Get only the first 25 charaters of the comment
        OutputRIAComment.CommentID = item.CommentID;
        OutputRIAComment.Comment = item.Comment.Substring(0, 25) + " ...";
        OutputRIAComment.CommentUpdateDate = item.CommentUpdateDate;
        // Add to the final results
        colResult.Add(OutputRIAComment);
    }
    return colResult;
}
#endregion
#region GetRIAComment
[WebMethod]
public RIAComment GetRIAComment(int RIACommentID)
{
    RIATasksDBDataContext DB = new RIATasksDBDataContext();
    var result = (from RIAComments in DB.RIAComments
                  where RIAComments.CommentID == RIACommentID
                  select RIAComments).FirstOrDefault();
    return result;
}
#endregion
#region UpdateRIAComment
[WebMethod]
public RIAComment UpdateRIAComment(RIAComment objRIAComment)
{
    DateTime dtCurrentDate = DateTime.Now;
    RIATasksDBDataContext DB = new RIATasksDBDataContext();

    var result = (from RIAComments in DB.RIAComments
                  where RIAComments.CommentID == objRIAComment.CommentID
                  // This will only perform the update if the CommentUpdateDate matches
                  // the existing CommentUpdateDate in the database
                  where RIAComments.CommentUpdateDate == objRIAComment.CommentUpdateDate
                  select RIAComments).FirstOrDefault();
    if (result != null)
    {              
        result.Comment = objRIAComment.Comment.Substring(0, 10000);
        result.CommentUpdateDate = dtCurrentDate; 
        DB.SubmitChanges();
        // Update the CommentUpdateDate on the object that will be returned
        objRIAComment.CommentUpdateDate = dtCurrentDate;
    }
    else
    {
        // The record could not be found because the CommentUpdateDate did not match
        // Or the record was deleted
        objRIAComment.Errors.Add("The record was not updated");
    }
    // Update comments to only show 25 characters
    objRIAComment.Comment = objRIAComment.Comment.Substring(0, 25) + " ...";
    return objRIAComment;
}
#endregion

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

در زیر یک نمای کلی از متدهای وب ارائه شده است:
•    GetRIAComments – طوری تغییر داده شد که 25 کاراکتر اول به اضافه "..." که به انتهای Comment ها افزوده می شود را بر می گرداند.
•    GetRIAComment- این متد تنهای یک Comment را برمی گرداند. از این متد جهت به دست آوردن کامل یک Comment وقتی کاربری در حال ویرایش درون خطی است استفاده می شود.
•    UpdateRIAComment- Comment ی را به روز رسانی می کند که فیلد CommentUpdateDate آن مطابق با Date همان رکورد موجود در Database باشد. همچنین در صورتی که عملیات به روز رسانی با موفقیت به انجام برسد، Comment به روز رسانی شده را برمی گرداند و در صورتی که عملیات به روزرسانی به درستی انجام نشود، یک پیغام خطا به لیست Errors اضافه خواهد شد.

به روز رسانی Web Reference


روی مرجع وب (Web Reference)wsRIARIAComments  کلیک راست کرده و گزینه Update Service Reference را انتخاب کنید.

به روزرسانی Model


متدهای زیر به مدل موجود اضافه کنید.

#region GetRIAComment
public static void GetRIAComment(int RIACommentID,
       EventHandler<GetRIACommentCompletedEventArgs> eh)
{
    // Set up web service call
    WebServiceSoapClient WS = new WebServiceSoapClient();
    // Set the EndpointAddress
    WS.Endpoint.Address = new EndpointAddress(GetBaseAddress());
    WS.GetRIACommentCompleted += eh;
    WS.GetRIACommentAsync(RIACommentID);
}
#endregion
#region UpdateRIAComment
public static void UpdateRIAComment(RIAComment objRIAComment,
       EventHandler<UpdateRIACommentCompletedEventArgs> eh)
{
    // Set up web service call
    WebServiceSoapClient WS = new WebServiceSoapClient();

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

    WS.UpdateRIACommentCompleted += eh;
    WS.UpdateRIACommentAsync(objRIAComment);
}
#endregion

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

متدها صرفاً توابع Web Service را که اضافه کرده بودید را فراخوانی خواهند کرد.
نکته: چون پارامترهای تابع GetRIAComments موجود در Web Service هیچ تغییری نداشتند لذا نیازی به تغییر این متد در Model نبود.

The View Model

حالا باید View Model را به روزرسانی کرد. در ابتدا اجازه بدهید یک Helper Class به پروژه اضافه خواهیم کرد که به ما در پیاده سازی ICommand ها کمک خواهد کرد. ICommand  ها جهت بالا آوردن رویدادها در View Model از طرف View به کار خواهند رفت.

The DelegateCommand Helper Class


در پوشه Classes کلاسی تحت عنوان DelegateCommand.cs ایجاد کنید.
کدهای زیر را جایگزین کدهای موجود کنید.

using System.Windows.Input;
using System;
// From http://johnpapa.net/silverlight/
//          5-simple-steps-to-commanding-in-silverlight/
namespace RIADataGrid
{
    public class DelegateCommand : ICommand
    {
        Func<object, bool> canExecute;
        Action<object> executeAction;
        bool canExecuteCache;
        public DelegateCommand(Action<object> executeAction,
                               Func<object, bool> canExecute)
        {
            this.executeAction = executeAction;
            this.canExecute = canExecute;
        }
        #region ICommand Members
        public bool CanExecute(object parameter)
        {
            bool temp = canExecute(parameter);
            if (canExecuteCache != temp)
            {
                canExecuteCache = temp;
                if (CanExecuteChanged != null)
                {
                    CanExecuteChanged(this, new EventArgs());
                }
            }
            return canExecuteCache;
        }
        public event EventHandler CanExecuteChanged;
 
        public void Execute(object parameter)
        {
            executeAction(parameter);
        }
        #endregion
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

این کلاس این اجازه را به ما می دهد که به سادگی به ICommand ها احضار کنیم. برای اطلاعات بیشتر می توانید در خصوص این کلاس می توانید به لینک زیر مراجعه کنید:

http://johnpapa.net/silverlight/5-simple-steps-to-commanding-in-silverlight

View Model


Property های زیر را به View Model اضافه کنید.


#region MessageVisibility
private Visibility _MessageVisibility
    = Visibility.Collapsed;
public Visibility MessageVisibility
{
    get { return _MessageVisibility; }
    private set
    {
        if (_MessageVisibility == value)
        {            return;        }
        _MessageVisibility = value;
        this.NotifyPropertyChanged("MessageVisibility");
    }
}
#endregion
#region Errors
private ObservableCollection<string> _Errors
    = new ObservableCollection<string>();
public ObservableCollection<string> Errors
{
    get { return _Errors; }
    private set
    {
        if (_Errors == value)
        {            return;        }
        _Errors = value;
        this.NotifyPropertyChanged("Errors");
    }
}
#endregion

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

کد فوق یک property جهت نگهداری تمامی خطاهای برگشت داده شده است و property دیگر اجازه نمایش (یا عدم نمایش) لیست خطا را می دهد.

کدهای زیر را نیز به کلاس اضافه کنید:

#region GetRIAComment
private void GetRIAComment(RIAComment Comment)
{
    // Call the Model to get the full RIAComment
    RIACommentsModel.GetRIAComment(Comment.CommentID, (Sender, EventArgs) =>
    {
        if (EventArgs.Error == null)
        {
            // Find the comment in the colRIAComments collection
            var CommentInCollection = (from comment in colRIAComments
                                       where comment.CommentID == EventArgs.Result.CommentID
                                       select comment).FirstOrDefault();

            if (CommentInCollection != null)
            {
                CommentInCollection.Comment = EventArgs.Result.Comment;
            }
        }
    });
}
#endregion

#region UpdateRIAComment
private void UpdateRIAComment(RIAComment objRIAComment)
{
    // Call the Model to UpdateRIAComment the RIAComment
    RIACommentsModel.UpdateRIAComment(objRIAComment, (Sender, EventArgs) =>
    {
        if (EventArgs.Error == null)
        {
            // Find the comment
            var CommentInCollection = (from comment in colRIAComments
                                       where comment.CommentID == EventArgs.Result.CommentID
                                       select comment).FirstOrDefault();

            if (CommentInCollection != null)
            {
                // Update the Comment
                CommentInCollection.Comment = EventArgs.Result.Comment;
                CommentInCollection.CommentUpdateDate = EventArgs.Result.CommentUpdateDate;

            }

            // Show any errors
            Errors = EventArgs.Result.Errors;
            // Set the visibility of the Message ListBox
            MessageVisibility = (Errors.Count > 0) ?
                                   Visibility.Visible : Visibility.Collapsed;

        }
    });
}
#endregion

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

این متدها محتوی کامل فیلد Comments را استخراج می کند و یک Comment را با فراخوانی متدهای مربوطه در Model به روزرسانی می کند.
کدهای زیر را به کلاس اضافه کنید:

#region GetRIACommentsCommand
public ICommand GetRIACommentsCommand { get; set; }
public void GetRIAComments(object param)
{
    GetRIAComments();
}
private bool CanGetRIAComments(object param)
{
    return true;
}
#endregion
#region GetRIACommentCommand
public ICommand GetRIACommentCommand { get; set; }
public void GetRIAComment(object param)
{
    GetRIAComment((RIAComment)param);
}
private bool CanGetRIAComment(object param)
{
    return true;
}
#endregion
#region UpdateRIACommentCommand
public ICommand UpdateRIACommentCommand { get; set; }
public void UpdateRIAComment(object param)
{
    // This is an Update
    UpdateRIAComment((RIAComment)param);
}
private bool CanUpdateRIAComment(object param)
{
    // Do not allow if there is no Current RIAComment
    return (param as RIAComment != null);
}
#endregion
  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

این کد ICommand ها را پیاده سازی می کند. این ICommand ها از طریق View با استفاده از Behavior ها فراخوانی خواهند شد.
کدهای زیر را به constructor کلاس View Model اضافه کنید:

GetRIACommentsCommand = new DelegateCommand(GetRIAComments, CanGetRIAComments);
GetRIACommentCommand = new DelegateCommand(GetRIAComment, CanGetRIAComment);
UpdateRIACommentCommand = new DelegateCommand(UpdateRIAComment, CanUpdateRIAComment);

 

 

 

 

 

 

این کد ها با استفاده از کلاس کمکیDelegateCommand ، ICommand ها را راه اندازی می کنند.
برای ساخت View به قسمت پنجم مقاله مراجعه کنید.


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

 حذف ردیف...   

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

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

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

لوگوی دوستان



ویرایش

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