我正在尝试创建一个UserControl来公开其他两个控件,UI和Host,这样我就可以使用MediaPlayerWpf并在设计器中设置属性,如UI.AllowPause和Host.Volume
我理解Attached Properties是我需要的。此外,我需要允许设置主机,而UI只显示其属性但不能更改。
我无法在设计器中显示属性UI和主机。我做错了什么?
public partial class MediaPlayerWpf {
public MediaPlayerWpf() {
InitializeComponent();
SetUI(this, MediaUI);
}
public static DependencyPropertyKey UIPropertyKey = DependencyProperty.RegisterAttachedReadOnly("UI", typeof(PlayerControls), typeof(MediaPlayerWpf), new PropertyMetadata(null));
public static DependencyProperty UIProperty = UIPropertyKey.DependencyProperty;
public PlayerControls UI { get => (PlayerControls)base.GetValue(UIProperty); private set => base.SetValue(UIPropertyKey, value); }
[AttachedPropertyBrowsableForType(typeof(MediaPlayerWpf))]
public static PlayerControls GetUI(UIElement element) {
return (PlayerControls)element.GetValue(UIProperty);
}
public static void SetUI(UIElement element, PlayerControls value) {
element.SetValue(UIPropertyKey, value);
}
public static DependencyProperty HostProperty = DependencyProperty.RegisterAttached("Host", typeof(PlayerBase), typeof(MediaPlayerWpf), new PropertyMetadata(null, OnHostChanged));
public PlayerBase Host { get => (PlayerBase)base.GetValue(HostProperty); set => base.SetValue(HostProperty, value); }
private static void OnHostChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
MediaPlayerWpf P = d as MediaPlayerWpf;
if (e.OldValue != null)
P.HostGrid.Children.Remove(e.OldValue as PlayerBase);
if (e.NewValue != null) {
P.HostGrid.Children.Add(e.NewValue as PlayerBase);
GetUI(P).PlayerHost = e.NewValue as PlayerBase;
}
}
[AttachedPropertyBrowsableForType(typeof(MediaPlayerWpf))]
public static PlayerBase GetHost(UIElement element) {
return (PlayerBase)element.GetValue(HostProperty);
}
public static void SetHost(UIElement element, PlayerBase value) {
element.SetValue(HostProperty, value);
}
}