如何使用(如果可能)tap事件在ContextMenu按钮单击中使用的相同来源?

时间:2014-05-08 12:25:52

标签: c# windows-phone-8 viewmodel

我正在调整一个声音播放器应用程序,尝试包含另存为铃声功能。应用程序使用Tiles和Viewmodel。点击每个图块会发出声音。我在数据模板中添加了一个上下文菜单,在“点击并暂停”事件中提供选项,将该声音保存为铃声。我有一些问题想弄清楚如何使用与Tiles用于播放声音相同的源。下面,第一个代码是Mainpage.xaml的一部分。然后是c#代码。我在MainPage.cs底部为_customRingtone Source设置的是错误的。模拟器在" SoundData data = selector.SelectedItem停止为SoundData;"我无法弄清楚如何以类似的方式使用Tile taps获取音频来播放它。我没有发布ViewModel,但如果你想要我可以。这就是加载Tile Groups和声音的地方。

<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>

 public partial class MainPage : PhoneApplicationPage
{
    private readonly SaveRingtoneTask
        _CustomRingtone;
    // Constructor
    public MainPage()
    {
        InitializeComponent();

        // Set the data context of the listbox control to the sample data
        DataContext = App.ViewModel;

        _CustomRingtone = new SaveRingtoneTask();
        _CustomRingtone.Completed +=
        customeRingtone_Completed;

        BuildLocalizedApplicationBar();
    }

    // Load data for the ViewModel Items
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        if (!App.ViewModel.IsDataLoaded)
        {
            App.ViewModel.LoadData();
        }
    }

    private void LongListSelector_SelectionChanged(object sender,
        SelectionChangedEventArgs e)
    {
        LongListSelector selector = sender as LongListSelector;

        // verifying our sender is actually a LongListSelector
        if (selector == null)
            return;

        SoundData data = selector.SelectedItem as SoundData;

        // verifying our sender is actually SoundData
        if (data == null)
            return;

private void customeRingtone_Completed(object sender, TaskEventArgs e)
    {
        if (e.TaskResult == TaskResult.OK)
        {
            MessageBox.Show(@"Saved");
        }
        else if (e.TaskResult == TaskResult.Cancel)
        {
            MessageBox.Show(@"Canceled");
        }
        else
        {
            MessageBox.Show(@"Not Saved");
        }

    }
    private void Save_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        LongListSelector selector = sender as LongListSelector;

        SoundData data = selector.SelectedItem as SoundData;

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

1 个答案:

答案 0 :(得分:1)

您的Save_Click事件处理程序未传递LLS,而是传递上下文MenuItem。 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();
}