gpt4 book ai didi

wpf - 检测UI

转载 作者:行者123 更新时间:2023-12-04 07:15:17 25 4
gpt4 key购买 nike

您如何检测UI?过去我读过人们已经对他们的用户界面进行了检测,但是我还没有找到有关如何对UI进行检测的示例或提示。

通过检测,我的意思是收集有关系统使用和性能的数据。关于Instrumentation的MSDN文章为http://msdn.microsoft.com/en-us/library/x5952w0c.aspx。我想捕获用户单击的按钮,他们使用的键盘快捷键,他们使用的搜索词等等。

  • 您如何检测UI?
  • 您存储仪器什么格式?
  • 您如何处理检测到的数据?
  • 使用这种检测逻辑如何保持UI代码的整洁?

  • 具体来说,我正在WPF中实现我的UI,因此与检测基于Web的应用程序相比,这将带来更多挑战。 (即,需要将检测到的数据传输回中心位置等)。就是说,我觉得这项技术可以通过诸如附加属性之类的概念提供一种更容易实现的仪器实现。
  • 您是否已检测到WPF应用程序?您对如何实现这一目标有任何建议吗?


  • 编辑:以下博客文章提出了一个有趣的解决方案: Pixel-In-Gene Blog: Techniques for UI Auditing on WPF apps

    最佳答案

    这是一个示例,说明了如何使用简单的事件管理器来挂接UI事件并提取事件的关键信息,例如UI元素的名称和类型,事件的名称以及父窗口的类型名称。对于列表,我还提取所选项目。

    此解决方案仅监听来自ButtonBase(按钮,ToggleButton等)的控件的单击,以及监听来自Selector的控件(ListBox,TabControl等)的选择更改。应该容易扩展到其他类型的UI元素或实现更细粒度的解决方案。该解决方案的灵感来自Brad Leach's answer

    public class UserInteractionEventsManager
    {
    public delegate void ButtonClickedHandler(DateTime time, string eventName, string senderName, string senderTypeName, string parentWindowName);
    public delegate void SelectorSelectedHandler(DateTime time, string eventName, string senderName, string senderTypeName, string parentWindowName, object selectedObject);

    public event ButtonClickedHandler ButtonClicked;
    public event SelectorSelectedHandler SelectorSelected;

    public UserInteractionEventsManager()
    {
    EventManager.RegisterClassHandler(typeof(ButtonBase), ButtonBase.ClickEvent, new RoutedEventHandler(HandleButtonClicked));
    EventManager.RegisterClassHandler(typeof(Selector), Selector.SelectionChangedEvent, new RoutedEventHandler(HandleSelectorSelected));
    }

    #region Handling events

    private void HandleSelectorSelected(object sender, RoutedEventArgs e)
    {
    // Avoid multiple events due to bubbling. Example: A ListBox inside a TabControl will cause both to send the SelectionChangedEvent.
    if (sender != e.OriginalSource) return;

    var args = e as SelectionChangedEventArgs;
    if (args == null || args.AddedItems.Count == 0) return;

    var element = sender as FrameworkElement;
    if (element == null) return;

    string senderName = GetSenderName(element);
    string parentWindowName = GetParentWindowTypeName(sender);
    DateTime time = DateTime.Now;
    string eventName = e.RoutedEvent.Name;
    string senderTypeName = sender.GetType().Name;
    string selectedItemText = args.AddedItems.Count > 0 ? args.AddedItems[0].ToString() : "<no selected items>";

    if (SelectorSelected != null)
    SelectorSelected(time, eventName, senderName, senderTypeName, parentWindowName, selectedItemText);
    }

    private void HandleButtonClicked(object sender, RoutedEventArgs e)
    {
    var element = sender as FrameworkElement;
    if (element == null) return;

    string parentWindowName = GetParentWindowTypeName(sender);
    DateTime time = DateTime.Now;
    string eventName = e.RoutedEvent.Name;
    string senderTypeName = sender.GetType().Name;
    string senderName = GetSenderName(element);

    if (ButtonClicked != null)
    ButtonClicked(time, eventName, senderName, senderTypeName, parentWindowName);
    }

    #endregion

    #region Private helpers

    private static string GetSenderName(FrameworkElement element)
    {
    return !String.IsNullOrEmpty(element.Name) ? element.Name : "<no item name>";
    }


    private static string GetParentWindowTypeName(object sender)
    {
    var parent = FindParent<Window>(sender as DependencyObject);
    return parent != null ? parent.GetType().Name : "<no parent>";
    }

    private static T FindParent<T>(DependencyObject item) where T : class
    {
    if (item == null)
    return default(T);

    if (item is T)
    return item as T;

    DependencyObject parent = VisualTreeHelper.GetParent(item);
    if (parent == null)
    return default(T);

    return FindParent<T>(parent);
    }

    #endregion
    }

    为了进行实际的日志记录,我使用log4net并创建了一个单独的名为“Interaction”的记录器来记录用户交互。这里的“Log”类只是我自己的log4net静态包装器。
    /// <summary>
    /// The user interaction logger uses <see cref="UserInteractionEventsManager"/> to listen for events on GUI elements, such as buttons, list boxes, tab controls etc.
    /// The events are then logged in a readable format using Log.Interaction.Info().
    /// </summary>
    public class UserInteractionLogger
    {
    private readonly UserInteractionEventsManager _events;
    private bool _started;

    /// <summary>
    /// Create a user interaction logger. Remember to Start() it.
    /// </summary>
    public UserInteractionLogger()
    {
    _events = new UserInteractionEventsManager();

    }

    /// <summary>
    /// Start logging user interaction events.
    /// </summary>
    public void Start()
    {
    if (_started) return;

    _events.ButtonClicked += ButtonClicked;
    _events.SelectorSelected += SelectorSelected;

    _started = true;
    }

    /// <summary>
    /// Stop logging user interaction events.
    /// </summary>
    public void Stop()
    {
    if (!_started) return;

    _events.ButtonClicked -= ButtonClicked;
    _events.SelectorSelected -= SelectorSelected;

    _started = false;
    }

    private static void SelectorSelected(DateTime time, string eventName, string senderName, string senderTypeName, string parentWindowTypeName, object selectedObject)
    {
    Log.Interaction.Info("{0}.{1} by {2} in {3}. Selected: {4}", senderTypeName, eventName, senderName, parentWindowTypeName, selectedObject);
    }

    private static void ButtonClicked(DateTime time, string eventName, string senderName, string senderTypeName, string parentWindowTypeName)
    {
    Log.Interaction.Info("{0}.{1} by {2} in {3}", senderTypeName, eventName, senderName, parentWindowTypeName);
    }
    }

    然后,输出将看起来像这样,省略不相关的日志条目。

    04/13 08:38:37.069信息Iact ToggleButton.Click通过MyMainWindow中的AnalysisButton
    04/13 08:38:38.493信息通过MyMainWindow中的ListView更改ListBox.SelectionChanged。选项:安德烈亚斯·拉尔森(Andreas Larsen)
    04/13 08:38:44.587信息Iact Button。在MyMainWindow中由EditEntryButton单击
    04/13 08:38:46.0​​68信息Iact Button。单击EditEntryDialog中的OkButton
    04/13 08:38:47.395信息Iact ToggleButton.Click通过MyMainWindow中的ExitButton

    关于wpf - 检测UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8214/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com