2013-02-13 123 views
1

這使用的DependencyProperty是我在我的自定義用戶控件的代碼:的Windows Phone - 內的MediaElement

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" 

/> 

問題是Mainpage中的XAML。 MySound屬性給我錯誤

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

任何幫助,您可以提供將不勝感激!

回答

0

Source property of the MediaElement type需要一個Uri,而不是另一個MediaElement對象。將依賴項屬性MySound的類型更改爲Uri,而不是MediaElement。 (也別忘內SoundSourcePropertytypeof(Uri)更換typeof(MediaElement)

順便說一句,我也建議分別重命名你有(SoundSourcePropertySoundNameProperty)兩個依賴屬性標識符MySoundPropertyMyNameProperty。有一個約定,依賴項屬性標識符的名稱(即public static readonly字段)是使用它的屬性的名稱(在您的案例中爲MySoundMyName),後面跟着Property。一些工具會期望遵循這個慣例。

+0

工作出色。我也採納了你的建議,並用屬性標識符重新命名。謝謝! – Tibblez 2013-02-13 18:05:22

+0

@ user2069143:很高興聽到它的工作。 – 2013-02-13 18:22:23

相關問題