MediaElement.play() from within ViewModel

前端 未结 3 2138
我寻月下人不归
我寻月下人不归 2021-02-09 21:45

I\'m struggling with the following issue:

I am building a WP8 application using the MVVM patern. I have a media element on my view.xaml and the logic to control this med

3条回答
  •  眼角桃花
    2021-02-09 21:53

    The answer of asitis is great.And I show you my detail code:(use caliburn.micro so don't need to bind name)

    View

        
             
        
    
        
             

    ViewModel

        private MediaElement _mediaElementObject = new MediaElement();
        public MediaElement MediaElementObject
        {
            get { return _mediaElementObject; }
            set
            {
                _mediaElementObject = value;
                NotifyOfPropertyChange(() => MediaElementObject);
            }
        }
    
        public void ButtonPlay()
        {
            MediaElementObject.Source =new Uri( @"C:\Users\admin\Videos\XXXXXX.wmv");
            MediaElementObject.LoadedBehavior = MediaState.Manual;
            MediaElementObject.UnloadedBehavior = MediaState.Manual;
            MediaElementObject.Play();
        }     
    
        public void ButtonStop()
        {
            MediaElementObject.Stop();
        }
        public void ButtonForward()
        {
            MediaElementObject.Position = _mediaElementObject.Position + TimeSpan.FromSeconds(30);
        }
        public void ButtonBack()
        {
            MediaElementObject.Position = _mediaElementObject.Position - TimeSpan.FromSeconds(30);
        }
    

    May can help someone:)

提交回复
热议问题