这是我在自定义 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.
您能提供的任何帮助将不胜感激!