Delegatecommand. CS

Source: Internet
Author: User

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. Windows. input;

Namespace SC
{
/// <Summary>
/// Delegate command for view Model
/// </Summary>
Public class delegatecommand: icommand
{
# Region members
/// <Summary>
/// Can execute function
/// </Summary>
Private readonly func <bool> canexecute;

/// <Summary>
/// Execute function
/// </Summary>
Private readonly action execute;

# Endregion

/// <Summary>
/// Initializes a new instance of the delegatecommand class.
/// </Summary>
/// <Param name = "execute"> indicate an execute function </param>
Public delegatecommand (Action execute)
: This (execute, null)
{
}

/// <Summary>
/// Initializes a new instance of the delegatecommand class.
/// </Summary>
/// <Param name = "execute"> Execute function </param>
/// <Param name = "canexecute"> can execute function </param>
Public delegatecommand (Action execute, func <bool> canexecute)
{
This.exe cute = execute;
This. canexecute = canexecute;
}

/// <Summary>
/// Can executes event handler
/// </Summary>
Public event eventhandler canexecutechanged
{
Add {commandmanager. requerysuggested + = value ;}
Remove {commandmanager. requerysuggested-= value ;}
}

/// <Summary>
/// Implement of icommand can execute Method
/// </Summary>
/// <Param name = "parameter"> parameter by default of icomand interface </param>
/// <Returns> can execute or not </returns>
Public bool canexecute (object parameter)
{
If (this. canexecute = NULL)
{
Return true;
}

Return this. canexecute ();
}

/// <Summary>
/// Implement of icommand interface execute Method
/// </Summary>
/// <Param name = "parameter"> parameter by default of icomand interface </param>
Public void execute (object parameter)
{
This.exe cute ();
}
}

/// <Summary>
/// Delegate command for view Model
/// </Summary>
Public class delegatecommand <t>: icommand
{
# Region members
/// <Summary>
/// Can execute function
/// </Summary>
Private readonly func <t, bool> canexecute;

/// <Summary>
/// Execute function
/// </Summary>
Private readonly action <t> execute;

# Endregion

/// <Summary>
/// Initializes a new instance of the delegatecommand class.
/// </Summary>
/// <Param name = "execute"> indicate an execute function </param>
Public delegatecommand (Action <t> execute)
: This (execute, null)
{
}

/// <Summary>
/// Initializes a new instance of the delegatecommand class.
/// </Summary>
/// <Param name = "execute"> Execute function </param>
/// <Param name = "canexecute"> can execute function </param>
Public delegatecommand (Action <t> execute, func <t, bool> canexecute)
{
This.exe cute = execute;
This. canexecute = canexecute;
}

/// <Summary>
/// Can executes event handler
/// </Summary>
Public event eventhandler canexecutechanged
{
Add {commandmanager. requerysuggested + = value ;}
Remove {commandmanager. requerysuggested-= value ;}
}

/// <Summary>
/// Implement of icommand can execute Method
/// </Summary>
/// <Param name = "parameter"> parameter by default of icomand interface </param>
/// <Returns> can execute or not </returns>
Public bool canexecute (object parameter)
{
If (this. canexecute = NULL)
{
Return true;
}

Return this. canexecute (t) parameter );
}

/// <Summary>
/// Implement of icommand interface execute Method
/// </Summary>
/// <Param name = "parameter"> parameter by default of icomand interface </param>
Public void execute (object parameter)
{
This.exe cute (t) parameter );
}
}
}

Delegatecommand. CS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.