如何链接MediaPlayerElement和ComboBox

时间:2019-04-10 18:07:32

标签: c# xaml uwp

我正忙于一个项目,用户可以在其中将媒体文件导入ComboBox并选择要播放的媒体文件。我可以选择该项目,但不会将其加载到MediaPlayerElement中进行播放。我确定它与我的组合框“ SelectionChanged”有关,但是我不知道如何实现它。

XAML代码

asm("nop");

XAML.CS代码

<Grid>
    <Button x:Name="loadbtn" Content="Button" HorizontalAlignment="Left" Margin="92,186,0,0" VerticalAlignment="Top" Height="87" Width="658" Click="Loadbtn_Click"/>
    <ComboBox IsTextSearchEnabled="True" x:Name="comboBox" Header="Media Library" Height="129" Width="658" HorizontalAlignment="Left" Margin="92,324,0,0" VerticalAlignment="Top" IsDropDownOpen="True" SelectionChanged="ComboBox_SelectionChanged"/>
    <MediaPlayerElement x:Name="mediaPlayerElement"  Width="658" Height="129" AutoPlay="True" AreTransportControlsEnabled="True" Margin="92,473,0,0" FocusVisualSecondaryBrush="White" FocusVisualPrimaryBrush="White" VerticalAlignment="Top" HorizontalAlignment="Left"/>
</Grid>

1 个答案:

答案 0 :(得分:0)

根据您的要求,您可以创建MediaItem模型类来保存选择的文件,并将MediaItem集合传递给comboBox数据源。

MediaItem

public class MediaItem
{
    public string FileName { get; set; }
    public StorageFile File { get; set; }
}

Xaml

<Grid>
    <Button
        x:Name="loadbtn"
        Width="658"
        Height="87"
        Margin="92,186,0,0"
        HorizontalAlignment="Left"
        VerticalAlignment="Top"
        Click="Loadbtn_Click"
        Content="Button"
        />
    <ComboBox
        x:Name="comboBox"
        Width="658"
        Height="129"
        Margin="92,324,0,0"
        HorizontalAlignment="Left"
        VerticalAlignment="Top"
        DisplayMemberPath="FileName"
        Header="Media Library"
        IsDropDownOpen="True"
        IsTextSearchEnabled="True"  
        SelectionChanged="ComboBox_SelectionChanged"
        />
    <MediaPlayerElement
        x:Name="mediaPlayerElement"
        Width="658"
        Height="129"
        Margin="92,473,0,0"
        HorizontalAlignment="Left"
        VerticalAlignment="Top"
        AreTransportControlsEnabled="True"
        AutoPlay="True"
        FocusVisualPrimaryBrush="White"
        FocusVisualSecondaryBrush="White"
        />
</Grid>

隐藏代码

private async void Loadbtn_Click(object sender, RoutedEventArgs e)
{
    var picker = new Windows.Storage.Pickers.FileOpenPicker();
    picker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
    picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.MusicLibrary;
    picker.FileTypeFilter.Add(".mp3");
    picker.FileTypeFilter.Add(".wav");
    picker.FileTypeFilter.Add(".mp4");

    var files = await picker.PickMultipleFilesAsync();
    if (files.Count > 0)
    {

        foreach (StorageFile file in files)
        {
            var item = new MediaItem { File = file, FileName = file.Name };
            comboBox.Items.Add(item);
        }

    }
    else
    {
        throw new Exception("Operation cancelled");
    }
}

private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    var mediaitem = comboBox.SelectedItem as MediaItem;
    mediaPlayerElement.MediaPlayer.SetFileSource(mediaitem.File);
}