fbpx

Help with MVVM RelayCommand translation from C# to X#

More
1 year 6 months ago #1 by Boonnam S
I've been trying to translate a sample MVVM "RelayCommand" I've found online. Here is the URL of a great example I'm using as a guide, msdn.microsoft.com/en-us/magazine/dd419663.aspx .

I am trying to translate the following:

readonly Action<object> _execute;
readonly Predicate<object> _canExecute;

public RelayCommand(Action<object> execute) : this(execute, null) { }
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute");
_execute = execute; _canExecute = canExecute;
}

Here is my current attempt, which I'm not sure if I'm on the right path:

private _execute as Action<object>
private _canExecute as Predicate<object>

CONSTRUCTOR()
RETURN

CONSTRUCTOR (execute as Action<object>) : self(execute, null)
return

CONSTRUCTOR (execute as Action<object>, canExecute as Predicate<object>)
return

This doesn't compile. Any suggestion?

Thanks
Boonnam

Please Log in or Create an account to join the conversation.

More
1 year 6 months ago - 1 year 6 months ago #2 by Wolfgang Riedmann
Replied by Wolfgang Riedmann on topic Help with MVVM RelayCommand translation from C# to X#
Hi Boonam,

this is my X# version of the RelayCommand class:
using System
using System.Windows.Input
using System.Diagnostics    

begin namespace rdm.MVVMHelper

public class RelayCommand implements ICommand
	protect _oExecute as Action<object>
	protect _oCanExecute as Predicate<object>

public constructor( oExecute as Action<object> )
	
	_oExecute := oExecute
	_oCanExecute := null
	
	return
	
public constructor( oExecute as Action<object>, oCanExecute as Predicate<object> )
	
	_oExecute := oExecute
	_oCanExecute := oCanExecute
	
	return

[DebuggerStepThrough];
public method CanExecute( oParameter as object ) as logic
	local lReturn as logic
	
	if _oCanExecute == null
		lReturn := true
	else
		lReturn	:= _oCanExecute( oParameter )
	endif
	
	return lReturn
		
public event CanExecuteChanged as EventHandler
	add
		CommandManager.RequerySuggested += VALUE
	end add
	remove
		CommandManager.RequerySuggested -= VALUE
	end remove
end event
          
public method Execute( oParameter as object ) as void

	_oExecute( oParameter )
	
	return
	
public method RaiseCanExecuteChanged() as void
	
	if _oCanExecute != null
		self:CanExecute( self )
	endif
	
	return

end class

end namespace

Wolfgang
Last edit: 1 year 6 months ago by Wolfgang Riedmann. Reason: better formatting

Please Log in or Create an account to join the conversation.

More
1 year 6 months ago #3 by Chris Pyrgas
Hi Boonam,

You need to modify the 2nd constructor like this:

CONSTRUCTOR (execute as Action<object>)
self(execute, null)
return

hth,
Chris

XSharp Development Team
chris(at)xsharp.eu

Please Log in or Create an account to join the conversation.