Help with MVVM RelayCommand translation from C# to X#

This forum is meant for questions and discussions about the X# language and tools
Post Reply
boonnam
Posts: 88
Joined: Mon May 08, 2017 6:42 pm
Location: USA

Help with MVVM RelayCommand translation from C# to X#

Post 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
User avatar
wriedmann
Posts: 3644
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Help with MVVM RelayCommand translation from C# to X#

Post 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
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
Chris
Posts: 4562
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Help with MVVM RelayCommand translation from C# to X#

Post by Chris »

Hi Boonam,

You need to modify the 2nd constructor like this:

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

hth,
Chris
Chris Pyrgas

XSharp Development Team test
chris(at)xsharp.eu
Post Reply