1

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

通用的.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>

声音控制.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.

您能提供的任何帮助将不胜感激!

4

1 回答 1

0

SourceMediaElement 类型的属性采用 Uri,而不是另一个 MediaElement 对象。将依赖属性的类型更改MySoundUri,而不是MediaElement。(不要忘记也替换typeof(MediaElement)typeof(Uri)within SoundSourceProperty。)

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

于 2013-02-13T17:12:36.610 回答