fbpx
Welcome, Guest
Username: Password: Remember me
Share your code snippets, screen shots etc. here
  • Page:
  • 1
  • 2

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

HELP - with X# syntax - Relay Command ... 2 years 2 months ago #1

  • Phil Hepburn
  • Phil Hepburn's Avatar Topic Author
  • Offline
  • Posts: 737
  • Karma: 7
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) :-



And here is the image of the C# code for comparison :-



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.
Attachments:

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

HELP - with X# syntax - Relay Command ... 2 years 2 months ago #2

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
This email address is being protected from spambots. You need JavaScript enabled to view it.

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

HELP - with X# syntax - Relay Command ... 2 years 2 months ago #3

  • Phil Hepburn
  • Phil Hepburn's Avatar Topic Author
  • Offline
  • Posts: 737
  • Karma: 7
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.



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.
Attachments:

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

HELP - with X# syntax - Relay Command ... 2 years 2 months ago #4

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
XSharp Development Team
chris(at)xsharp.eu

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

HELP - with X# syntax - Relay Command ... 2 years 2 months ago #5

  • wriedmann
  • wriedmann's Avatar
  • Offline
  • Posts: 1404
  • Karma: 6
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
This email address is being protected from spambots. You need JavaScript enabled to view it.
www.riedmann.it - docs.xsharp.it

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

HELP - with X# syntax - Relay Command ... 2 years 2 months ago #6

  • Phil Hepburn
  • Phil Hepburn's Avatar Topic Author
  • Offline
  • Posts: 737
  • Karma: 7
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.



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.
Attachments:

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

HELP - with X# syntax - Relay Command ... 2 years 2 months ago #7

  • wriedmann
  • wriedmann's Avatar
  • Offline
  • Posts: 1404
  • Karma: 6
Hi Phil,

you have an error in your EVENT REMOVE code. Instead of
public event CanExecuteChanged as EventHandler
	add
		CommandManager.RequerySuggested += VALUE
	end add
	remove
		CommandManager.RequerySuggested += VALUE
	end remove
end event

you should write
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
This email address is being protected from spambots. You need JavaScript enabled to view it.
www.riedmann.it - docs.xsharp.it

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

HELP - with X# syntax - Relay Command ... 2 years 2 months ago #8

  • Phil Hepburn
  • Phil Hepburn's Avatar Topic Author
  • Offline
  • Posts: 737
  • Karma: 7
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.







Attachments:

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

HELP - with X# syntax - Relay Command ... 2 years 2 months ago #9

  • Phil Hepburn
  • Phil Hepburn's Avatar Topic Author
  • Offline
  • Posts: 737
  • Karma: 7
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.
Attachments:

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

HELP - with X# syntax - Relay Command ... 2 years 2 months ago #10

  • wriedmann
  • wriedmann's Avatar
  • Offline
  • Posts: 1404
  • Karma: 6
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):
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




Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
This email address is being protected from spambots. You need JavaScript enabled to view it.
www.riedmann.it - docs.xsharp.it
Attachments:

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

HELP - with X# syntax - Relay Command ... 2 years 2 months ago #11

  • wriedmann
  • wriedmann's Avatar
  • Offline
  • Posts: 1404
  • Karma: 6
Hi Phil,

me again: MVVM purists would say that you should never call a MessageBox.Show() from a ViewModel.

In my test application I have a central locator class where the view registers itself, and the ViewModel sends the message through the locator:
method DisplayMessageBox( cCaption as string, cMessage as string ) as System.Windows.MessageBoxResult
  local oMessageDisplay as IMessageDisplay
  local oResult as System.Windows.MessageBoxResult
	
  oMessageDisplay := self:MessageDisplay
  if oMessageDisplay != null
    oResult := oMessageDisplay:DisplayMessageBox( cCaption, cMessage )
  else
    oResult := System.Windows.MessageBoxResult.OK
  endif
	
  return oResult

property MessageDisplay as IMessageDisplay
  get 
    var oMessageDisplay := ServiceContainer.GetInstance():GetService<IMessageDisplay>()
    if oMessageDisplay == null
      Debug.WriteLine( "no IMessageDisplay registered!" )
    endif 
    return oMessageDisplay
  end get
end property

My code is very far from being perfect, and I suspect people like Nick would have much more to say, but I try to do things the best way I'm able to.

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
This email address is being protected from spambots. You need JavaScript enabled to view it.
www.riedmann.it - docs.xsharp.it

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

HELP - with X# syntax - Relay Command ... 2 years 2 months ago #12

Hi Phil,

You could also do your property like this...
private _doTestCommand as ICommand
property DoTestCommand as ICommand
   get
      if _doTestCommand == null
         _doTestCommand := RelayCommand{Action<Object>{ self, @DoTest() }, null}
      endif
      return _doTestCommand
   end get
end property
With this pattern you don't need a setter, and you don't have to manually create the RelayCommand in your initialisation code.

It's still a fair bit of code, and when you have several commands it starts to add up, so I must admit that in my more recent code I now do this (not sure how auto getters and setters are working in X#, so this is in C#)...
private override void InitialiseCommands()
{
   DoTestCommand = new RelayCommand(DoTest);
}
public ICommand DoTestCommand {get; private set;}
InitialiseCommands is called automatically in my ViewModelBase class.

Nick

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

HELP - with X# syntax - Relay Command ... 2 years 2 months ago #13

Hi Phil,

you're saying you have it decoupled. But I see calling messageBox from ViewModel. This implies that you have a reference from your VM app to the PresentationFramework.dll. Is this right? Then you are wrong. That makes your VM dependend from the GUI system.

I avoid RelayCommand and RoutedEvent.

I trigger a method that sends out a message 'This butten is clicked' or whatever. Some module in your app can react on this. Your view should not know about that. With intelligent binding to IsEnabled you can achieve the same what a command does with CanExecute.

Then you make me happy :-).

Frank

PS: See my example from cologne last year.

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

HELP - with X# syntax - Relay Command ... 2 years 2 months ago #14

;-)Come on guys, lighten up, Phil's only put the message box in there so the method does something visible to show it's working!!

Frank, what's wrong with RelayCommand?... does just what's needed, send a notification from the UI to a ViewModel.

Nick

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

HELP - with X# syntax - Relay Command ... 2 years 2 months ago #15

Hi Nick,

nothing other than I don't like it :-). Maybe I didn't understood it when I began working on V / VM . Then I found the trigger way, and it worked.

And of course it is only a small example app it does not matter using a messagebox there. In general I would not do it. I think Phil does understand me comments.

Cheers
Frank

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

HELP - with X# syntax - Relay Command ... 2 years 2 months ago #16

  • Phil Hepburn
  • Phil Hepburn's Avatar Topic Author
  • Offline
  • Posts: 737
  • Karma: 7
Quite right Nick,

Lets steady down a bit ........

And just before I went to pay a visit to the dentist ;-0((

Anyway, now I have returned home and done a little more work on the 'ViewModel' we can have all our messages, not in a dialog box, but in a public property on the VM called 'Messages'. This way we don't hog or grab control away from the WPF form thread, and also it is so easy to do with data binding.

Also, remember I am a teacher, and designing stuff for possible conference new guys to a lot of stuff like LINQ, EF6, WPF/XAML, the list goes on - AND SO - I must make my demo apps as correct as possible, but also as simple to follow as possible.

And remember - there is no right way as everyone keeps saying !! Possibly just 'better ways'.

Nick, the 'RelayCommand' example I am following is a standard one from C# Corner. I will look at the changes you suggest for my own use after Cologne, but what is working now seems just fine to me for the purpose.

Also the point of this code for RC is that I then don't need employ an MVVM framework, which is good for newbies to some or all of this stuff.

Here are a few small images to show what I have done, post dentist :-















Regards to all, and MANY thanks for your suggestions, keep them coming ;-0)
Phil.

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

HELP - with X# syntax - Relay Command ... 2 years 2 months ago #17

  • Phil Hepburn
  • Phil Hepburn's Avatar Topic Author
  • Offline
  • Posts: 737
  • Karma: 7
Thanks Wolfgang,

I will try to apply this later with my Add-New and Save stuff.

I did use it some time ago for session material for Cologne, last year? the year before that ??

If I hit any snags / issues I will come back to you.

Cheers,
Phil.

OOO! Pain killer from the dentist is starting to ware off !!!

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

HELP - with X# syntax - Relay Command ... 2 years 2 months ago #18

  • wriedmann
  • wriedmann's Avatar
  • Offline
  • Posts: 1404
  • Karma: 6
Hi Phil,

you are welcome!

As you I don't use any external references in my samples, so anyone can use them without problems.
And since I use XIDE for programming, I can create single application export files that make it very easy to import them and delete them when not needed anymore.

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
This email address is being protected from spambots. You need JavaScript enabled to view it.
www.riedmann.it - docs.xsharp.it

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

HELP - with X# syntax - Relay Command ... 2 years 2 months ago #19

  • Phil Hepburn
  • Phil Hepburn's Avatar Topic Author
  • Offline
  • Posts: 737
  • Karma: 7
Good morning Wolfgang, and all,

What I am about to say was on the top of my head the other day, before I visited the dentist, but did not have time to say it or write it.

As .NET coders / designers we must embrace the world of .NET completely, not just the bits we fancy or like, ignoring the bits we dislike. Particularly if we have a teaching role.

Since 'Frameworks' are quite a big part of .NET then we have to use then sometimes, in the appropriate places, I feel. Personally, I can't do the Entity work without using the EF6 Framework from Microsoft. And there are other examples as well.

There are a couple of points about 'Frameworks' as I can see things, one is why employ a Framework when the task to solve is a small one ? A bit like my use of 'RelayCommand' just to move the Button click event code into the ViewModel? When I can code a class to do this and use it, quite easily?

The other (to my mind) is where does the Framework come from. Who made it, who maintains it? I personally feel less keen on using assemblies created by individuals or small teams, than I do about using Microsoft supported Frameworks.

So if I was to design and create a full blown MVVM app then I am much more likely to try and use Prism (from MS) than the Galasoft MVVMlight, even though I have used this one successfully in the past.

Just my 2 cents worth on a warmish Sunday morning here in Newport. My thoughts on XAML with WPF will be posted in a separate post ;-0)

Best regards,
Phil.
Wales, UK.

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

HELP - with X# syntax - Relay Command ... 2 years 2 months ago #20

  • wriedmann
  • wriedmann's Avatar
  • Offline
  • Posts: 1404
  • Karma: 6
Hi Phil,

since I have not a teaching role (even if I think I should contribute my code to the guys who need it), I'm a bit more pragmatic about the use of frameworks.

First of all, I have to maintain my software for at least 15 or 20 years (ok, in 20 years I will be 73 years old - but someone other in my company should be able to maintain my code).
Therefore I have to take much more caution when selecting a framework or even 3rd party lib.

Often it is better to not use a 3rd party lib, but write (and understand!) the code itself. Frameworks often have a lot of functionality I don't need and make it harder to understand and maintain code - and since code readability and maintenability is a top priority for me this is another point why I prefer to write code of my own.

But of course it is very important to look at frameworks (and others applications) to see good and bad ideas.....

Of course writing own code is harder and takes more time, but in the long run it is better, I think - at least for me and my small company.
And sometimes it is better to decide things that seem strange to other - specially for us that whe come from VO and heave learned to be more disciplinated when coding than most other programmers (name based data binding in VO was a great thing - and forced us to think about naming instead of binding "Edit1" to the "CustName" field and "Edit2" to the "Address" field like many Delphi and VB programmers have done).

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
This email address is being protected from spambots. You need JavaScript enabled to view it.
www.riedmann.it - docs.xsharp.it

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

  • Page:
  • 1
  • 2