如何在“依赖属性已更改”上设置断点?

发布于 2024-12-05 06:24:39 字数 767 浏览 1 评论 0原文

我们的 Silverlight 应用程序包含一个第三方控件,其中包含一些 ScrollBar(除其他外)。为了解决问题,我希望每当第三方控件修改其任何滚动条的最小或最大属性时都能够在调试器中停止。然后我将能够查看堆栈跟踪并了解有关正在发生的情况的更多信息。

如果我对 ScrollBars 的 Value 属性感兴趣,那一切都很简单 - ScrollBar 有一个 ValueChanged 事件,因此我只需添加一些一次性代码来在 ScrollBar 上挂钩该事件,在事件处理程序中设置断点,然后进行调试离开。但是MinimumChanged 或MaximumChanged 没有对应的CLR 事件,所以事情不会那么简单。

我遇到了一篇博客文章,其中讨论了 使用 DependencyPropertyDescriptor 获取依赖属性更改事件,但不幸的是,DependencyPropertyDescriptor 在 Silverlight 中不存在。

如何才能设置一个断点,每当 ScrollBar 的 Maximum 和 Maximum 属性发生变化时就会触发该断点?

Our Silverlight app contains a third-party control, which contains some ScrollBars (among other things). In order to troubleshoot a problem, I want to be able to stop in the debugger whenever the third-party control modifies the Minimum or Maximum properties of any of its scrollbars. Then I'll be able to look at the stack trace and learn more about what's going on.

If I was interested in the ScrollBars' Value property, that would all be easy -- ScrollBar has a ValueChanged event, so I could just add some throwaway code that hooks that event on the ScrollBar, set a breakpoint inside my event handler, and debug away. But there are no corresponding CLR events for MinimumChanged or MaximumChanged, so it won't be that simple.

I ran across a blog post that talks about using DependencyPropertyDescriptor to get dependency property change events, but unfortunately, DependencyPropertyDescriptor doesn't exist in Silverlight.

How can I get to the point where I can set a breakpoint that fires whenever the ScrollBar's Minimum and Maximum properties change?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

非要怀念 2024-12-12 06:24:39

我想到了以下想法:

  • 创建一个具有依赖属性的用户控件。 (实际上不会使用用户控件中的 XAML,我们只需要依赖属性。)
  • 将用户控件的依赖属性绑定到第三方控件的 Maximum 或 Maximum 属性(假设它们也是依赖属性)。
  • 在用户控件的代码隐藏中,将 PropertyChangedCallback 添加到依赖项属性并在其中放置一个断点。

这种方法应该允许您设置一个断点,每当“最小值”或“最大值”属性发生更改时就会触发该断点。但是,我不能保证您会得到对您有帮助的堆栈跟踪。

用户控件的代码隐藏可能如下所示:

public partial class DPContainer : UserControl
{
    public static readonly DependencyProperty DebugValueProperty =
        DependencyProperty.Register("DebugValue", typeof(object), typeof(DPContainer), new PropertyMetadata(DebugValue_Changed));

    public DPContainer()
    {
        InitializeComponent();
    }

    public object DebugValue
    {
        get { return GetValue(DebugValueProperty); }
        set { SetValue(DebugValueProperty, value); }
    }

    private static void DebugValue_Changed(DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
        // Drop a breakpoint in this method.
    }

假设您有一个带有 x:Name="someScrollBar" 的 ScrollBar,则可以将如下内容添加到 XAML 中:

<local:DPContainer DebugValue="{Binding Path=Minimum, ElementName=someScrollBar}" />

The following idea springs to my mind:

  • Create a user control with a dependency property. (The XAML within the user control won't actually be used, we just need the dependency property.)
  • Bind the user control's dependency property to the Minimum or Maximum property of the third party control (assuming they are also dependency properties).
  • In the code-behind of the user control, add a PropertyChangedCallback to the dependency property and drop a breakpoint in that.

This approach should allow you to set a breakpoint that fires whenever the Minimum or Maximum property changes. However, I can't guarantee that you'll get a stacktrace that will help you.

The code-behind of the user control could look something like this:

public partial class DPContainer : UserControl
{
    public static readonly DependencyProperty DebugValueProperty =
        DependencyProperty.Register("DebugValue", typeof(object), typeof(DPContainer), new PropertyMetadata(DebugValue_Changed));

    public DPContainer()
    {
        InitializeComponent();
    }

    public object DebugValue
    {
        get { return GetValue(DebugValueProperty); }
        set { SetValue(DebugValueProperty, value); }
    }

    private static void DebugValue_Changed(DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
        // Drop a breakpoint in this method.
    }

Assuming that you have a ScrollBar with x:Name="someScrollBar", you could then add something like the following to your XAML:

<local:DPContainer DebugValue="{Binding Path=Minimum, ElementName=someScrollBar}" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文