1

カスタム プロパティを UserControl に追加する方法を教えてください。Video Player UserControl を作成しましたが、それを別のアプリケーションに実装したいと考えています。UserControl に mediaElement コントロールがあり、UserControl があるアプリから mediaElement.Source にアクセスしたいと考えています。

私はこれを試しました:[Player.xaml.cs]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

    namespace VideoPlayer
    {
    public partial class Player : UserControl
    {
        public static readonly DependencyProperty VideoPlayerSourceProperty =
        DependencyProperty.Register("VideoPlayerSource", typeof(System.Uri), typeof(Player), null);

        public System.Uri VideoPlayerSource
        {
            get { return mediaElement.Source; }
            set { mediaElement.Source = value; }
        }


        public Player()
        {
            InitializeComponent();
        }

プロパティ ボックスにプロパティが見つからないようです。これについて何か助けはありますか?

4

2 に答える 2

0

DependencyProperty CLR ラッパー (getter/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;
}
于 2012-12-14T14:04:57.517 に答える
0

プロパティからgetsetを変更する必要があります。これを置き換えてみてください:

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); }
}
于 2012-12-14T14:05:16.550 に答える