Windows Phone - 在MediaElement中使用DependencyProperty

时间:2013-02-13 16:40:09

标签: windows silverlight xaml

这是我在自定义UserControl中的代码:

generic.xaml:

<UserControl x:Class="SoundControl.SoundClass"
             x:Name="Uc"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <StackPanel HorizontalAlignment="Stretch">
    <Grid x:Name="mygrid"
          Background="Transparent"
          Width="Auto">
      <Grid.RowDefinitions>
        <RowDefinition Height="70*" />
      </Grid.RowDefinitions>
      <Grid.ColumnDefinitions>
        <ColumnDefinition Width="350*" />
        <ColumnDefinition Width="70*" />
      </Grid.ColumnDefinitions>
      <Button x:Name="SoundButton"
              Content="{Binding MyName, ElementName=Uc}"
              Grid.Column="0"
              Grid.Row="0"
              Click="RingtoneButton_Click" />
      <Button x:Name="RingtoneButton"
              Grid.Column="1"
              Grid.Row="0"
              HorizontalAlignment="Stretch"
              VerticalAlignment="Stretch"
              Click="RingtoneButton_Click">
        <Image Source="/Images/note.png"
               Stretch="Fill"
               Height="30"
               Width="30" />
      </Button>
      <MediaElement x:Name="SoundContainer"
                    Source="{Binding MySound, ElementName=Uc}"
                    AutoPlay="False" />
    </Grid>
  </StackPanel>
</UserControl>

SoundControl.cs

namespace SoundControl
{
    public partial class SoundClass : UserControl
    {
        SaveRingtoneTask saveRingtoneChooser;

        public SoundClass()
        {
            // For ringtone
            saveRingtoneChooser = new SaveRingtoneTask();
            saveRingtoneChooser.Completed += new EventHandler<TaskEventArgs>(saveRingtoneChooser_Completed);
        }

        public static readonly DependencyProperty SoundSourceProperty =
            DependencyProperty.Register("MySound", typeof(MediaElement), typeof(SoundClass), null);

        public static readonly DependencyProperty SoundNameProperty =
            DependencyProperty.Register("MyName", typeof(string), typeof(SoundClass), null);

        public MediaElement MySound
        {
            get { return (MediaElement)this.GetValue(SoundSourceProperty); }
            set { base.SetValue(SoundSourceProperty, value); }
        }

        public string MyName
        {
            get { return (string)this.GetValue(SoundNameProperty); }
            set { base.SetValue(SoundNameProperty, value); }
        }

        public SoundClass()
        {
            InitializeComponent();
        }

        private void SoundButton_Click(object sender, RoutedEventArgs e)
        {
            SoundContainer.Play();
        }

        private void RingtoneButton_Click(object sender, RoutedEventArgs e)
        {
            saveRingtoneChooser.Source = new Uri(SoundContainer.Source.AbsoluteUri);
            saveRingtoneChooser.DisplayName = MyName;
            saveRingtoneChooser.Show();
        }

        void saveRingtoneChooser_Completed(object sender, TaskEventArgs e)
        {
            switch (e.TaskResult)
            {
                //Logic for when the ringtone was saved successfully
                case TaskResult.OK:
                    MessageBox.Show("Ringtone saved.");
                    break;

                //Logic for when the task was cancelled by the user
                case TaskResult.Cancel:
                    MessageBox.Show("Save cancelled.");
                    break;

                //Logic for when the ringtone could not be saved
                case TaskResult.None:
                    MessageBox.Show("Ringtone could not be saved.");
                    break;
            }
        }

    }
}

和我的MainPage.xaml

<SoundControl:SoundClass
                        HorizontalAlignment="Left" 
                        Grid.Row="0" 
                        VerticalAlignment="Top" 
                        Grid.ColumnSpan="2" 
                        Width="456" 
                        MyName="TEST123"
                        MySound="/project/test.mp3"

/>

问题是主页中的XAML。 MySound属性给我错误

The TypeConverter for "MediaElement" does not support converting from a string.

非常感谢您提供的任何帮助!

1 个答案:

答案 0 :(得分:0)

Source property of the MediaElement type采用Uri,而不是另一个MediaElement对象。将依赖项属性MySound的类型更改为Uri,而不是MediaElement。 (不要忘记在typeof(MediaElement)内将typeof(Uri)替换为SoundSourceProperty。)

顺便提一下,我还建议将您拥有的两个依赖项属性标识符(SoundSourcePropertySoundNameProperty)分别重命名为MySoundPropertyMyNameProperty。有一个约定,依赖项属性的标识符(即public static readonly字段)的名称是使用它的属性的名称(在您的情况下为MySoundMyName),后跟{ {1}}。有些工具会期望遵循这一惯例。