我有一个添加了自定义行为的控件。
该行为有两个 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));
每个都绑定(bind)到 WPF 中的某个对象。
问题在于,在具有该行为的控件完全加载之前,Handle 属性的 OnHandleChanged
被激活,这会在尝试向控件添加 HwndHost 时引发异常。
在尝试添加 HwndHost 之前如何确保控件已完全加载?
P.s: 尝试使用 OnAttached()
,但这没有用...
最佳答案
试试这个:
public MyControl()
{
this.Loaded += MyControl_Loaded;
}
private void MyControl_Loaded(object sender, RoutedEventArgs e)
{
// Control is full loaded
}
关于C#如何保证一个WPF控件的满载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16218278/