C# 如何使用(如果可能)点击事件在上下文菜单按钮点击中使用的相同源?

C# 如何使用(如果可能)点击事件在上下文菜单按钮点击中使用的相同源?,c#,windows-phone-8,viewmodel,C#,Windows Phone 8,Viewmodel,我正在调整一个声音播放器应用程序,尝试加入另存为铃声功能。应用程序使用瓷砖和Viewmodel。敲击每一块瓷砖都会发出声音。我在数据模板中添加了一个上下文菜单,在点击并保持事件中提供选项,将声音保存为铃声。我在弄清楚如何使用与瓷砖相同的音源来播放声音时遇到了一些问题。下面,第一个代码是Mainpage.xaml的一部分。然后是c代码。我在MainPage.cs底部为_customRingtone源设置的内容是错误的。仿真器在“SoundData data=selector.SelectedIte

我正在调整一个声音播放器应用程序,尝试加入另存为铃声功能。应用程序使用瓷砖和Viewmodel。敲击每一块瓷砖都会发出声音。我在数据模板中添加了一个上下文菜单,在点击并保持事件中提供选项,将声音保存为铃声。我在弄清楚如何使用与瓷砖相同的音源来播放声音时遇到了一些问题。下面,第一个代码是Mainpage.xaml的一部分。然后是c代码。我在MainPage.cs底部为_customRingtone源设置的内容是错误的。仿真器在“SoundData data=selector.SelectedItem as SoundData”处停止;我不知道如何以类似的方式执行源代码,平铺点击可以获得播放它的音频。我没有发布ViewModel,但如果您愿意,我可以发布。这就是加载磁贴组和声音的地方

<phone:PhoneApplicationPage.Resources>
    <DataTemplate x:Key="SoundTileDataTemplate">
        <StackPanel>
        <toolkit:ContextMenuService.ContextMenu>
            <toolkit:ContextMenu>
                <toolkit:MenuItem Click="Save_Click" Header="Save as Ringtone" />
            </toolkit:ContextMenu>
        </toolkit:ContextMenuService.ContextMenu>
        <Grid Background="{StaticResource PhoneAccentBrush}"
              Margin="0, 0, 12, 12">
            <Grid VerticalAlignment="Top"
                                  HorizontalAlignment="Right"
                                  Width="40"
                                  Height="40"
                                  Margin="0, 6, 6, 0">
                <Ellipse Stroke="{StaticResource PhoneForegroundBrush}" 
                                         StrokeThickness="3" />
                <Image Source="/Assets/AppBar/Play.png" />
            </Grid>
            <StackPanel VerticalAlignment="Bottom">
                <TextBlock Text="{Binding Title}" Margin="6, 0, 0, 6" />
            </StackPanel>
        </Grid>
        </StackPanel>
    </DataTemplate>

</phone:PhoneApplicationPage.Resources>


<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">

    <MediaElement
        Name="AudioPlayer"
        Volume="1" />

    <!--Pivot Control-->
    <phone:Pivot Title="{Binding Path=LocalizedResources.ApplicationTitle, 
                                    Source={StaticResource LocalizedStrings}}">

        <phone:PivotItem Header="{Binding Animals.Title}">
            <phone:LongListSelector x:Name="Animal"
                SelectionChanged="LongListSelector_SelectionChanged"
                Margin="0,0,-12,0" 
                ItemsSource="{Binding Animals.Items}"
                LayoutMode="Grid"
                GridCellSize="150,150"
                ItemTemplate="{StaticResource SoundTileDataTemplate}"
                />
        </phone:PivotItem>

保存时,单击事件处理程序不会传递LLS,而是传递上下文菜单项。MenuItem的DataContext是您要查找的对象

private void Save_Click(object sender, System.Windows.RoutedEventArgs e)
{
    var element = (FrameworkElement)sender;

    SoundData data = element.DataContext as SoundData;

    _CustomRingtone.Source = new Uri(data.FilePath, UriKind.RelativeOrAbsolute**);
    _CustomRingtone.DisplayName = "Ring";
    _CustomRingtone.Show();
}
private void Save_Click(object sender, System.Windows.RoutedEventArgs e)
{
    var element = (FrameworkElement)sender;

    SoundData data = element.DataContext as SoundData;

    _CustomRingtone.Source = new Uri(data.FilePath, UriKind.RelativeOrAbsolute**);
    _CustomRingtone.DisplayName = "Ring";
    _CustomRingtone.Show();
}