c# - How to track WPF commands? -
in wpf application, want have user tracking system keep statistics on way users using application. in other words, i'm looking way track commands being executed , how have been triggered user (by clicking on toolbar button, using keyboard shortcuts, etc). far, haven't found nice way while using wpf command pattern...
do have ideas/suggestions on how achieve/design without overriding every control used in application?
for discussion purposes, created basic wpf application containing toolbar single save button, textbox , listbox. added keybinding trigger save command when pressing ctrl+s.
the first challenge determine device (mouse or keyboard) used trigger command.
the second challenge determine control used trigger command (the command source). i'm not interested know control had keyboard focus when command triggered, know control used trigger command (usually it's button, hyperlink, menuitem contextmenu, etc.)
mainwindow.xaml
<window x:class="trackingcommands.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" title="mainwindow" x:name="me" height="480" width="600"> <window.commandbindings> <commandbinding command="save" executed="onsavecommandexecuted" canexecute="onsavecommandcanexecute" /> </window.commandbindings> <window.inputbindings> <keybinding command="save" gesture="ctrl+s"/> </window.inputbindings> <grid> <grid.rowdefinitions> <rowdefinition height="auto" /> <rowdefinition /> </grid.rowdefinitions> <toolbartray grid.row="0"> <toolbar> <button command="save" content="save"/> </toolbar> </toolbartray> <textbox grid.row="1" textwrapping="wrap" acceptsreturn="true"/> </grid> </window>
mainwindow.xaml.cs
public partial class mainwindow { public mainwindow() { initializecomponent(); } private void onsavecommandexecuted(object sender, executedroutedeventargs e) { e.handled = true; } private void onsavecommandcanexecute(object sender, canexecuteroutedeventargs e) { e.canexecute = true; e.handled = true; } }
edit
i realized original question bit vague, apologize. try give more information , ask more precise question.
i know simple enough store list of commands have been executed. challenge here retrieve device used trigger command initially: mouse or keyboard?
by putting tracking logic in "executed" handler, there no way @ point determine if user triggered command clicking button mouse, pressing enter on button or if used keyboard shortcut. in example, same command can triggered clicking toolbar button or pressing ctrl+s on keyboard. how can track these separate actions trigger same command?
can achieve in viewmodel layer? when reach command handler, it's late: have lost information. place know device used in view itself. how pass information command handler? way override button control intercept click , keydown events in order provide additional context command handler?
if use mvvm pattern command bound view command instance in view model. use create icommand implementation provided event when executed details itself. maybe use command provider/factory/whatever create each command , wire logger/tracker.
Comments
Post a Comment