MVVM MediaElement.Play()没有工作/没有异常(在ViewModel中)

时间:2017-03-15 19:24:21

标签: c# wpf xaml mvvm uwp

所以我有UWP应用程序,在xaml中我将媒体元素作为参数传递给命令(点击)。一切都没有例外地执行,但它没有播放媒体。我的问题是为什么它不起作用?

的Xaml

 <MediaElement Name="media" AutoPlay="True"  Source="{Binding Song.SongToPlay}"></MediaElement>
 <Button x:Name="play"  Content="button" Command="{Binding OnPlayButton}" CommandParameter="{Binding ElementName=media , Mode=OneTime}" />

视图模型

public ICommand OnPlayButton { get; set; }

public MainViewModel(IViewModelService iService)
    {
        Song = new Song();
        _iVmService = iService;
        OnPlayButton = new CommandBase(Play); 


        public void Play(object o)
    {
        try
        { //just testing
            _uiControls= new UIControls();
            _uiControls.MediaElement = o as MediaElement;
            _uiControls.MediaElement.Source = Song.SongToPlay = new Uri(@"C:\Users\blabla\Desktop\ZHU.mp3", UriKind.Absolute);
            _uiControls.MediaElement.Play(); //doesnt do anything
         }
        catch (Exception exception) //never hit breakpoing here
        {

        }


  public class UIControls : ModelBase
{


    private MediaElement _mediaElement;

    public MediaElement MediaElement
    {
        get { return _mediaElement; }
        set { _mediaElement = value; OnPropertyChanged(); }
    }
}

1 个答案:

答案 0 :(得分:1)

您无法在应用程序的存储空间C:\Users\blabla\Desktop\ZHU.mp3之外播放文件(仍允许使用内部网址)。您需要使用FileOpenPicker播放不属于该应用程序的音乐文件。