Asked by:
Command binding using a CommandMap

Question
-
Hello everyone,
I created a support class to help me reduce the coding when I've to bind command in a XAML view. The class is quite easy to understand:
- there's a dictionary (string, ICommand) where I store the DelegateCommands;
- I used, or to be honest I copied, the TypeDescriptionProvider to expose the commands as a property of the class;
So, basically, using an example:
MyClass MyClass = new MyClass(); [...] MyClass.AddCommand("NameOfTheCommand")
in the WPF I bind like this:
<Button Command="{Binding MyClass.NameOfTheCommand}"></Button>
Everything is working fine but in the error list panel I see one error for each command: "XLS0432 The property 'NameOfTheCommand' was not fount in type 'MyClass'". The solution can be builded/debugged, the software works fine, but how can I remove these errors? They're so bad to see in the error list panel!- Moved by Yong LuMicrosoft contingent staff Monday, July 22, 2019 8:22 AM
Tuesday, July 16, 2019 2:50 PM
All replies
-
Hi Andrea M83,
I also refer the following links and make a test on my side. I can run it without errors.
Simplifying commands in MVVM and WPF
CommandMap
Besides, you have a problem with the coded naming convention.MyClass MyClass = new MyClass();
You may can change it like below.
MyClass myClass = new MyClass(); <Button Command="{Binding myClass.NameOfTheCommand}"></Button>
Best regards
Yong Lu
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.Wednesday, July 17, 2019 7:15 AM -
You might find caliburn.micro useful.
Personally.
I prefer clear explicit commands in viewmodels.
So i'm not so keen on auto wireup mechanisms.
But, if you want to cut down on your code.
Caliburn micro matches public methods by name to name of control.
Similarly with properties.
Wednesday, July 17, 2019 7:33 AM -
Hi Yong Lu,
thanks for the reply, I also use the code you linked and I want to share a new method AddCommand that allow to use naming convention to relay the method On/Can:
public void AddCommand(string commandName, object caller) { Type thisType = caller.GetType(); MethodInfo theMethod = thisType.GetMethod("On" + commandName); if (theMethod is null) { Exception ex = new Exception("Method On" + commandName + " not found."); throw ex; } MethodInfo theCanMethod = thisType.GetMethod("Can" + commandName); if (theCanMethod is null) { Exception ex = new Exception("Method Can" + commandName + " not found."); throw ex; } this.Commands[commandName] = new DelegateCommand(param => theMethod.Invoke(caller, new object[] { param }), x => (bool)theCanMethod.Invoke(caller, null)); }
in this way I can use a naming convention and when I need to add a command I write:
public CommandMap.CommandMap Commands { get; set; } public MainWindowViewModel() { this.Commands = new CommandMap.CommandMap(); Commands.AddCommand("ChangeView", this); } public void OnChangeView(string parameter) { //Do something } public bool CanChangeView() { return true; }
Regarding my problem the error is visible only in Visual Studio 2019, in other PC with VS2017 the error is not showing so I think it a problem of VS2019.
Thursday, July 18, 2019 7:21 AM -
Hello Andy,
thanks for the information, it seems to be a nice framework to reduce the code in XAML applications, I'll take a look on it!
Thursday, July 18, 2019 7:24 AM -
Hi Andrea M83,
>> Regarding my problem the error is visible only in Visual Studio 2019, in other PC with VS2017 the error is not showing so I think it a problem of VS2019.
You can try to visit the Developer Community and report a problem about Visual Studio.
Best regards
Yong LuMSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.Friday, July 19, 2019 1:02 AM