xsharp.eu • Help with MVVM RelayCommand translation from C# to X#
Page 1 of 1

Help with MVVM RelayCommand translation from C# to X#

Posted: Mon Jun 19, 2017 10:32 pm
by boonnam
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, https://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

Help with MVVM RelayCommand translation from C# to X#

Posted: Tue Jun 20, 2017 4:00 am
by wriedmann
Hi Boonam,

this is my X# version of the RelayCommand class:

Code: Select all

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

Help with MVVM RelayCommand translation from C# to X#

Posted: Tue Jun 20, 2017 9:50 am
by Chris
Hi Boonam,

You need to modify the 2nd constructor like this:

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

hth,
Chris