简体   繁体   中英

MediaElement.Source throws 'System.InvalidOperationException'

I'm writing a WPF application and I have a MediaElement -Property called VideoPlayer in a class called ViewmodelVideos .

When I say

ViewModelVideos.VideosPlayer.Source = new Uri(text);

I get an exception:

Exception thrown: 'System.InvalidOperationException' in WindowsBase.dll

I can't figure out why thats not working. Any ideas?

Edit:

"text" is a valid url which I can put into my browser and it works. No https, simple http://...

@Clemens comment did the trick:

The problem was, that I was doing the call outside the UI thread.

Solution was

ViewModelVideos.VideosPlayer.Dispatcher.Invoke(() => ViewModelVideos.VideosPlayer.Source = new Uri(text)); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM