2

我有一个控件,我向其中添加了自定义行为。

该行为有两个 DependencyProperties:

public SomeObject Player {
    get { return (e)GetValue(PlayerProperty); }
    set { SetValue(PlayerProperty, value); }
}

// Using a DependencyProperty as the backing store for Player.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty PlayerProperty =
    DependencyProperty.Register("Player", typeof(IVideoPlayerDTO), typeof(PlayerStreamBehavior), new UIPropertyMetadata(null));

public IntPtr Handle {
    get { return (IntPtr)GetValue(HandleProperty); }
    set { SetValue(HandleProperty, value); }
}

// Using a DependencyProperty as the backing store for Handle.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty HandleProperty =
        DependencyProperty.Register("Handle", typeof(IntPtr), typeof(PlayerStreamBehavior), new UIPropertyMetadata(IntPtr.Zero, OnHandleChanged));

每个都绑定到 WPF 中的某个对象。问题是在OnHandleChanged具有行为的控件完全加载之前激活了 Handle 属性,这会在尝试将 HwndHost 添加到控件时引发异常。在尝试添加 HwndHost 之前,如何确保控件已完全加载?Ps: 试过用OnAttached(),但是没用。。。

4

1 回答 1

1

尝试这个:

public MyControl()
{
    this.Loaded += MyControl_Loaded;
}

private void MyControl_Loaded(object sender, RoutedEventArgs e)
{
   // Control is full loaded
}
于 2013-04-25T15:11:01.803 回答