HELP - with X# syntax - Relay Command ...

Public forum to share code snippets, screen shorts, experiences, etc.
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

HELP - with X# syntax - Relay Command ...

Post by Phil Hepburn »

Hi guys,

I need your help - PLEASE !

I am currently trying to add a RelayCommand class to my 'Stock' app for Cologne.

The C# class came from an article in C#Corner and I have the code working in one of my earlier apps. Now I want to translate it all to X#, and the central class is the 'RelayCommand' class. I am not using a MVVM framework doing things this way.

I have done some of the translating so here is my imperfect attempt (so far) :-
PearlsRelayHelp_01.jpg
PearlsRelayHelp_01.jpg (130.04 KiB) Viewed 651 times
And here is the image of the C# code for comparison :-
PearlsRelayHelp_02.jpg
PearlsRelayHelp_02.jpg (131.8 KiB) Viewed 651 times
And I suppose you guys want the text code too :-

====================================================================
public class RelayCommand implements ICommand

//----- from C# Corner -----
private execute as Action<object>
private canExecute as Func<object, Boolean>

//----- events -----
public event CanExecuteChanged as EventHandler
add ( CommandManager.RequerySuggested += value )
remove ( CommandManager.RequerySuggested -= value )
return

//--- constructor ---
constructor(execute as Action<object>, canExecute := null as Func<object, Boolean> )
self:execute := execute
self:canExecute := canExecute
return

//--- methods ---
public method CanExecute(parameter as object) as Boolean
return ( self:canExecute == null || self:canExecute(parameter) )

public method Execute(parameter as object) as void
self:execute(parameter)
return

end class
======================================================================

Any suggestions welcomed - TIA.

Regards,
Phil.
Wales, UK.
User avatar
robert
Posts: 4520
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

HELP - with X# syntax - Relay Command ...

Post by robert »

Phil,

Looks ok with one Exception: Func is a keyword in our language (that is why it is colored Blue). Change that to @@Func and I think it will work.
There are a few hardcoded abbreviations in our language (because they are also in Vulcan):

FUNC
PROC
LONG
SHORT


Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

HELP - with X# syntax - Relay Command ...

Post by Phil Hepburn »

Thanks Robert,

I will follow up what you suggest - BUT - to let you all know that I did try to help myself, I attach an image of the Reflector 9 conversion using Fabrice's plug-in for Vulcan. This will help to cross-check the rest of the stuff I tried out in X#. @@ is still missing however.
PearlsRelayHelp_11.jpg
PearlsRelayHelp_11.jpg (86.3 KiB) Viewed 651 times
Does anyone know why I seem NOT to be able to copy the text from Reflector any more. I am sure I remember in the past copying / pasting lots, and lots of similar code. I have a fully paid up licenced version of Reflector 9. Should it work ?

Oh! and before I go, let me say that when I get the 'RelayCommand' stuff working I will make all bits of the stuff available to you - a small app perhaps. This allows us to do some basic (and important) MVVM stuff to keep event handlers out of the code behind the XWP / XAML form - will keep Frank happy as it good practise for testing ;-0) And the code does not get complicated.

Bed now, and try this stuff in the morning.
Weather getting colder and whiter in the UK.

Cheers,
Phil.
User avatar
Chris
Posts: 4906
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

HELP - with X# syntax - Relay Command ...

Post by Chris »

Phil,

I can spot also another thing, the event in X# must be defined in x# syntax:

EVENT myEvent AS EventHandler
ADD
CommandManager.RequerySuggested += VALUE
END ADD
REMOVE
CommandManager.RequerySuggested += VALUE
END REMOVE
END EVENT

Chris
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
User avatar
wriedmann
Posts: 3755
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

HELP - with X# syntax - Relay Command ...

Post by wriedmann »

Hi Phil,

the EVENT ADD REMOVE functionality is another sign how the X# team works: Robert added it shortly after I had requested it because I needed it to move my C# RelayCommand class to X#.

If you look at my X# MVVM sample in XIDE, you'll find this class there.

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

HELP - with X# syntax - Relay Command ...

Post by Phil Hepburn »

Thanks Chris, and all you other guys too ;-0)

I have implemented your ideas and suggestions as well as a couple of my own. I have attached the version which now compiles fine - testing will come a little later today.

I like the Add / Remove syntax.
PearlsRelayHelp_21.jpg
PearlsRelayHelp_21.jpg (106.54 KiB) Viewed 651 times
One issue which hit me was caused by me translating C# code where the code designer had used just the different case sensitivity of a member name to distinguish it from another. (Not a good idea in my books.) Hence the changes you may see to clarify things, me using '_execute' and also 'in_execute' with the method name 'Execute' staying as it is.

This suggests to me that we will need to be VERY careful in what code we translate and use from C# examples, I have not been hit by this before now, I think!?

Has anyone got an answer for me on the problem / issue of copying text from 'Reflector 9' - in my Windows 10 set-up I can't highlight the code for pasting.

TIA, and thanks once again for all the tips ;-0)
Cheers,
Phil.
User avatar
wriedmann
Posts: 3755
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

HELP - with X# syntax - Relay Command ...

Post by wriedmann »

Hi Phil,

you have an error in your EVENT REMOVE code. Instead of

Code: Select all

public event CanExecuteChanged as EventHandler
	add
		CommandManager.RequerySuggested += VALUE
	end add
	remove
		CommandManager.RequerySuggested += VALUE
	end remove
end event
you should write

Code: Select all

public event CanExecuteChanged as EventHandler
	add
		CommandManager.RequerySuggested += VALUE
	end add
	remove
		CommandManager.RequerySuggested -= VALUE
	end remove
end event
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

HELP - with X# syntax - Relay Command ...

Post by Phil Hepburn »

Thanks Wolfgang,

I think I just copied and pasted it from Chris's post - blame someone else ;-0)
Changes now made, and I have the whole code working (even with the error).

So far just a single Button for click response - this should do my whole Cologne demo app.

I have included a few images of working code in case anyone else is interested.

Thanks once again to all who helped me 'get there' !
Cheers,
Phil.
PearlsRelayHelp_31.jpg
PearlsRelayHelp_31.jpg (18.93 KiB) Viewed 651 times
PearlsRelayHelp_32.jpg
PearlsRelayHelp_32.jpg (46.25 KiB) Viewed 651 times
PearlsRelayHelp_33.jpg
PearlsRelayHelp_33.jpg (46.83 KiB) Viewed 651 times
PearlsRelayHelp_34.jpg
PearlsRelayHelp_34.jpg (44.74 KiB) Viewed 651 times
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

HELP - with X# syntax - Relay Command ...

Post by Phil Hepburn »

Sorry guys,

Although a simple bit of code, it is needed to complete the picture - fits with the four I sent earlier.

Now all my code is in the VM (view model) and not in the code behind of the View itself. Keep you happy Frank !

Regards,
Phil.
PearlsRelayHelp_35.jpg
PearlsRelayHelp_35.jpg (18.94 KiB) Viewed 651 times
User avatar
wriedmann
Posts: 3755
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

HELP - with X# syntax - Relay Command ...

Post by wriedmann »

Hi Phil,

sorry for blaming the wrong person!

This is only needed if you need to activate/deactivate buttons (again a piece of code from my sample):

Code: Select all

self:OkButton := RelayCommand{ SaveData, CanSaveData }

method SaveData( oObject as object ) as void

  Application.Current:MainWindow:Close()
	
  return               

virtual method CanSaveData( oObject as object ) as logic
  local lReturn			as logic	
                    
  if String.IsNullOrEmpty( _oServer:Value1 ) .or. String.IsNullOrEmpty( _oServer:Value2 )
    lReturn := false
  else
    lReturn := true
  endif
	
  return lReturn
valid.png
valid.png (3.41 KiB) Viewed 651 times
nonvalid.png
nonvalid.png (4.64 KiB) Viewed 651 times


Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Post Reply