PDA

View Full Version : command ها



hamidreza.m
شنبه 19 شهریور 1390, 21:53 عصر
سلام به همه ی برنامه نویسان عزیز

راستش من خیلی در مورد command ها در WPF در اینترنت گشتم ( البته در سایت های فارسی زبان ) ولی چیز به درد بخوری پیدا نکردم . مخواستم بدونم کلا تعریف command ها چیه و چجوری میشه از اون ها استفاده کرد و کلا چه جوری باید در برناممون از command ها استفاده کنیم ؟ اگر هم میخواین سایت انگلیسی بدید لطفا ابتدا به توضیح مختصری در مورد command ها بدید و سپس آدرس سایت انگلیسی بدید تا یه پیش زمینه داشته باشم و در اون سایت ها بقیه ی مطالب را بخونم .

ممنون از همگی

Hamishebahar
شنبه 19 شهریور 1390, 23:03 عصر
سلام.
به نظر من command ها به درد MVVM میخورن....
شما رویداد ها رو میتونید به صورت command به کنترل بایند کنید.
مثلاً میخواهید در زمان بخصوصی enable کنترل شما فعال باشه .
برای مثال command کنترل button همون رویداد کلیک میتونه باشه....
این دو کلاس RelayCommand برای استفاده از Command ها:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Diagnostics;

namespace Copy_My_Files.Businesses
{
// Summary:
// A generic command whose sole purpose is to relay its functionality to other
// objects by invoking delegates. The default return value for the CanExecute
// method is 'true'. This class allows you to accept command parameters in the
// Execute and CanExecute callback methods.
//
// Type parameters:
// T:
// The type of the command parameter.
[Serializable]
public class RelayCommand<T> : ICommand
{
#region Fields

readonly Action<T> _execute = null;
readonly Predicate<T> _canExecute = null;

#endregion // Fields

#region Constructors

public RelayCommand(Action<T> execute)
: this(execute, null)
{

}

/// <summary>
/// Creates a new command.
/// </summary>
/// <param name="execute">The execution logic.</param>
/// <param name="canExecute">The execution status logic.</param>
public RelayCommand(Action<T> execute, Predicate<T> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute");

_execute = execute;
_canExecute = canExecute;
}

#endregion // Constructors

#region ICommand Members

[DebuggerStepThrough]
public bool CanExecute(object parameter)
{
return _canExecute == null ? true : _canExecute((T)parameter);
}

public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}

public void Execute(object parameter)
{
_execute((T)parameter);
}

#endregion // ICommand Members
}

public class RelayCommand : ICommand
{
#region Fields

readonly Action _execute = null;
readonly Func<bool> _canExecute = null;

#endregion // Fields

#region Constructors

public RelayCommand(Action execute)
: this(execute, null)
{

}

/// <summary>
/// Creates a new command.
/// </summary>
/// <param name="execute">The execution logic.</param>
/// <param name="canExecute">The execution status logic.</param>
public RelayCommand(Action execute, Func<bool> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute");

_execute = execute;
_canExecute = canExecute;
}

#endregion // Constructors

#region ICommand Members

[DebuggerStepThrough]
public bool CanExecute(object parameter)
{
return _canExecute == null ? true : _canExecute();
}

public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}

public void Execute(object parameter = null)
{
_execute();
}

#endregion // ICommand Members
}
}



بعنوان مثال کد زیر در کلاسی قرار داده میشود که قرار است بایند شود:
public RelayCommand SelectFolderCommand
{
get;
private set;
}

کد xaml:

<Button Command="{Binding SelectFolderCommand}" Content="Select Folder" Height="23" HorizontalAlignment="Left" Margin="12,12,0,0" VerticalAlignment="Top" Width="103" />


و در انتها مثالی برای اجرای کامند:
SelectFolderCommand = new RelayCommand(() =>
{
//زمانی که کامند اجرا شد
},
() =>
{
//false برای غیر فعال کردن کامند مقدار
//true برای فعال کردن کامند مقدار
return false;
});

موفق و سربلند باشید.

hamidreza.m
پنج شنبه 07 مهر 1390, 15:33 عصر
دست شما درد نکنه :بوس: عالی بود.
فقط یه سوال داشتم : از کد های شما من چند تا چیز را نمیتونم باهاشون کار کنم یعنی بلد نیستم یه چیزایی دربارشون خوندم ولی نمیدونم باید کجاها و به چه صورت ازشون استفاده کنم :
یکی علامت لامبدا <= است و یکی هم این عبارات <T> و کلا <> این ها اگه بگید چی هستند یا یه منبع خوب برای آموزشش بگید ممنون میشم