Question

Im tweaking a Sound Player app to try and include a Save as Ringtone function. App uses Tiles and a Viewmodel. Tapping each tile plays a sound. I added a Context Menu to the Data Template to give the option on a Tap and Hold event to save that sound as a Ringtone. I am having some issues figuring out just how to use the same source as the Tiles use to play the sound. Below, first code is portion of the Mainpage.xaml. Then the c# code. What I have set at the bottom of the MainPage.cs for the _customRingtone Source is wrong. The emulator stops at "SoundData data = selector.SelectedItem as SoundData;" I cant figure out how to do the source in a similar way the Tile taps get the audio for playing it. I didnt post the ViewModel but can if you want me to. That is where the Tile Groups and sounds are loaded.

<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();
    }
Was it helpful?

Solution

You Save_Click event handler is not passed a LLS, but a context MenuItem. The DataContext of the MenuItem is the object you are after.

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();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top