I having trouble to understand some of the features in the RoutedCommand.
The RoutedCommand can be created and some input gesture can be attached:
HomeZoomPanCommand = new RoutedCommand();
HomeZoomPanCommand.InputGestures.Add(new KeyGesture(Key.F10, ModifierKeys.None));
Now I have to add the command to the CommandBindings collection which is quite clear to me.
Normally I would assume, that if I want to have F10 working on this command, I have to add a KeyBinding also to the InputBindings. This seems also clear to me.
But what happens in detail when I have the InputGestures already assigned to the RoutedCommand? I assumed that the gesture is automatically added to the InputBindings collection but it is obviously not.
For what reason are there two different ways. Does the InputGesture on the command really work or is it only to show it in the menu with any function?
Related
One of the useful thing when doing xaml in either Xamarin or Maui is having the ICommand interface; even better with the CommunityToolkits.Mvvm.
The ICommand has execut and canExecute to make coding the press of a button very easy.
In Blazor you can try and use ViewModels in the same way, as services,
I'm not talking just about items in a list to decouple from the model and have more props like an isSelected.
But it's not really the best thing and you don't have ICommand.
What do you do then when managing buttons?
<button #onclick="OnButtonClicked" disabled="#(canButtonExecute || isButtonExecuting)>
click me
</button>
You still have to make two properties, and a method to properly handle a button click or some combination of these, but the ICommand is not really a thing in Blazor, nor Microsoft suggest Mvvm for this framework.
There is a guy that plans to port the CommunityToolkit to Blazor but it's not like people are begging for it.
So how you all solve this issue? Not mentioning also that the disabled attribute in html can be just removed with the developer console.
Also it can be for any other case where ICommand is usefull and buttons arent just a way to submit forms. ( i know with the EditForm component you can use some properties of the model to make sure a button isnt clicked twice but still! )
Can you even make a custom directive to use in html components to not make a custom button element (imagine doing a blazor component as a wrapper for every html element come on)?
Like the #onclick but like #command="MyCommand"?
Blazorise supports ICommand on buttons https://blazorise.com/docs/components/button
When should I use the Command and when to use the Click event?
F.e. if I have a Button in my UWP app what should I use?
When should I use the Command and when to use the Click event?
Yours is a broad question and I would simply answer with: "It depends".
Because:
The Command implements the ICommand interface and this means more code to add to your application but usually this won't change. Instead, the event handler doesn't require any interface implementation.
For every command you want, you have to provide the code that will handle the click and the CanExecute logic, to say when the command can execute. This is not requested in a simple event handler (like MyButton_Click). This means that, using a Command, you will have more control over the elements of your UI (the button won't execute anything if CanExecute is false).
When you want to add a Command, you will bind it to your DataContext (the ViewModel, if you implement the MVVM pattern). Instead, when you add a simple event handler (like MyButton_Click), the code will be placed in your code-behind that is the logic behind your main window. This means that implementing a Command, according to me, you'll have everything you need to modify in just one place (the ViewModel) instead of logic scattered everywhere in your project.
Of course, you can use whatever you want and my points are there just to give you an insight about these different implementations and you have to consider which solution is suitable for you, considering also the requirements you have been given (like: "Don't use event handlers" or "The Command is too advanced, let's just use something simple", etc.) and/or other constraints in your project.
I'm relatively new to UWP, but I have good WPF background.
What I need is a simple context menu for the application. User "right taps" anywhere, menu opens, user taps an item and things happen.
It looks basic and simple. First I add MenuFlyout element to my Application.Resources. Then, in MainPage I just show it with ShowAt method. Works.
To my greatest surprise when I tried to add events to menu items, VS told me it's invalid, events cannot be added in App.xaml.
So here's my assignment (in App.xaml.cs):
MainContextMenu = (MenuFlyout)Resources["MainContextMenu"];
MainContextMenu.Items.First(i => i.Name == "NavToCalibration").Tapped += NavToCalibration_Tapped;
The problem is - the handler is never called. I run my app, open the menu, click on the item and nothing happens. The assigned method is not called. What am I doing wrong? Why the handler is not called?
The assignment IS executed on application launch.
I'm also surprised I haven't been able to find any example or tutorial on doing such a simple and basic thing.
There is a good reason I use app-wide context menu instead of other controls. The app displays test images, it has to be full-screen (or maximized window) without interfering elements.
Now I will try to move my menu to the page resources, my pages will have different context menus anyway. But I'm really curious what's wrong in MenuFlyout defined in App.xaml?
Whoa. I tried to move my MenuFlyout to MainPage. I was able to assign Tapped event in XAML. And it also is not triggered! Now I'm completely lost. Any ideas?
I have an application using cefSharp and some KeyGestures to open some forms. We were initially using the WPF ChromiumWebBrowser, but required the use of touch scrolling, which is not supported. As a result, we changed the control to the WinForms ChromiumWebBrowser inside a WindowsFormHost.
After the switch, KeyGestures bound to our RoutedCommands no longer fire.
After reviewing here, here, and here, I have tried several different solutions, but to no avail.
As per the above, when the window is deactivated, then activated the KeyGestures are routed appropriately (as discussed regarding breakpoints 'causing' the commands to work).
I have tried using the CommandManager.InvalidateRequerySuggested method on a timer or being called on loaded, after transfering focus to the window, after transfering forcus to another WPF control and after focusing the WindowsFormsHosts.
My command declaration is as follows:
RoutedCommand ShowAdmin = new RoutedCommand();
ShowAdmin.InputGestures.Add(new KeyGesture(Key.F1, ModifierKeys.Shift | ModifierKeys.Alt | ModifierKeys.Control));
CommandBindings.Add(new CommandBinding(ShowAdmin, ShowAdminForm));
I essentially have two questions:
1) Why am I not receiving the command. Is it because the WinForms control doesn't 'bubble' the keypress events?
2) What can I do to capture the keygesture without resorting to opening another window, only to close it again
Thanks in advance for your questions, comments and answers!
If you implement IKeyboardHandler you should be handle custom key press combinations, that's probably your easiest and cleanest solution.
https://github.com/cefsharp/CefSharp/blob/master/CefSharp/IKeyboardHandler.cs
I've bound a command to a button on a Ribbon control. The CanExecute method on the button gets called as expected but clicking on the button doesn't cause the Execute method to be called. The CanExecute sets the CanExecute property to true - the button in question is enabled and clickable.
Has anyone else seen this behaviour before? If so how do I fix it!
EDIT:
CommandBinding commandBinding = new CommandBinding(StaticCommands.ThisCommand, ThisCommandExecutedHandler, ThisCommandCanExecuteHandler);
CommandManager.RegisterClassCommandBinding(this.GetType(), commandBinding);
CommandBindingList.Add(commandBinding);
StaticCommands.ThisCommand is a RoutedCommand with an input gesture of F5.
Unfortunately I can't post any xaml because everything is wrapped up in another team's libraries. I am assuming that is correct for now. Also, using the keyboard gesture associated with the command (pressing F5) causes the execute method to be called.
There are no exceptions thrown, no messages in the output window, and snoop shows everything bound correctly. I'm really stumped.
This usually happens if the parameters dont match in type correctly... are you binding CommandParameter of one type and accepting a different type parameter in the Command.Execute() call?
Fixed this by wrapping the RoutedCommands in a RelayCommand. I have no idea why that worked, but assuming there's a problem in the other team's assembly.