C# 将自定义属性添加到UserControl
我需要向UserControl添加自定义属性的帮助。我创建了一个视频播放器UserControl,我想在另一个应用程序中实现它。我的UserControl中有一个mediaElement控件,我想从应用程序中访问mediaElement.Source,我的UserControl将位于何处 我试过:[Player.xaml.cs]C# 将自定义属性添加到UserControl,c#,silverlight,user-controls,expression-blend-4,custom-properties,C#,Silverlight,User Controls,Expression Blend 4,Custom Properties,我需要向UserControl添加自定义属性的帮助。我创建了一个视频播放器UserControl,我想在另一个应用程序中实现它。我的UserControl中有一个mediaElement控件,我想从应用程序中访问mediaElement.Source,我的UserControl将位于何处 我试过:[Player.xaml.cs] 我似乎在属性框中找不到属性。有什么帮助吗 您对DependencyProperty CLR wrappersgetter/setter使用了不正确的语法。 使用以下正确
我似乎在属性框中找不到属性。有什么帮助吗 您对DependencyProperty CLR wrappersgetter/setter使用了不正确的语法。 使用以下正确代码:
public static readonly DependencyProperty VideoPlayerSourceProperty = DependencyProperty.Register("VideoPlayerSource",
typeof(System.Uri), typeof(Player),
new PropertyMetadata(null, (dObj, e) => ((Player)dObj).OnVideoPlayerSourceChanged((System.Uri)e.NewValue)));
public System.Uri VideoPlayerSource {
get { return (System.Uri)GetValue(VideoPlayerSourceProperty); } // !!!
set { SetValue(VideoPlayerSourceProperty, value); } // !!!
}
void OnVideoPlayerSourceChanged(System.Uri source) {
mediaElement.Source = source;
}
您需要更改属性中的get和set。尝试替换此:
public System.Uri VideoPlayerSource
{
get { return mediaElement.Source; }
set { mediaElement.Source = value; }
}
为此:
public System.Uri VideoPlayerSource
{
get { return (System.Uri)GetValue(VideoPlayerSourceProperty); }
set { SetValue(VideoPlayerSourceProperty, value); }
}
你能再加一些代码吗?就像该属性所在的类声明一样?如果将该属性更改为字符串,它会显示吗?我编辑了这个问题。现在检查代码
public System.Uri VideoPlayerSource
{
get { return (System.Uri)GetValue(VideoPlayerSourceProperty); }
set { SetValue(VideoPlayerSourceProperty, value); }
}