Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将自定义属性添加到UserControl_C#_Silverlight_User Controls_Expression Blend 4_Custom Properties - Fatal编程技术网

C# 将自定义属性添加到UserControl

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使用了不正确的语法。 使用以下正确

我需要向UserControl添加自定义属性的帮助。我创建了一个视频播放器UserControl,我想在另一个应用程序中实现它。我的UserControl中有一个mediaElement控件,我想从应用程序中访问mediaElement.Source,我的UserControl将位于何处

我试过:[Player.xaml.cs]


我似乎在属性框中找不到属性。有什么帮助吗

您对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); }
}