C#如何保证一个WPF控件的满载

标签 c# wpf dependency-properties

我有一个添加了自定义行为的控件。

该行为有两个 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/

相关文章:

c# - 解决 C# 利息问题,如何计算储蓄余额达到预期目标余额所需的年数?

c# - 如何确定在 WPF 中使用哪个集合

wpf - WPF依赖属性设计如何节省内存消耗?

c# - 如何使用多个依赖属性绑定(bind)到计算属性

wpf - 自定义控件数据绑定(bind) wpf

C# Lock 语法 - 2 个问题

C# 返回类型推断不好的做法?

wpf - 无法通过引用找到绑定(bind)源

wpf - 使用值转换器时如何更新 View 模型的属性?

c# - 通过 linq 在 iphone 中的 Monotouch/Xamarin 上获取 IP 地址