I got another problem with MVVM. I want to click on a button and a new window containing a mediaelement as contentcontrol. The window should open and start the video from the link passed to the constructor. Here is my relevant code:
ApplicationView.xaml:
<Window.Resources>
<DataTemplate DataType="{x:Type local:ApplicationViewModel}">
<local:ApplicationView/>
</DataTemplate>
<DataTemplate DataType="{x:Type local:MediaPlayerViewModel}">
<local:MediaPlayerView/>
</DataTemplate>
</Window.Resources>
EDIT: It also does not change anything if I don't use the UserControl... was a try to fix the warning i got here.
Here I have to say that I got a warning, but it's working. Since my views are inheriting from Window it says that i can't pass window element to a style element. Don't know if this has something to do with it:P
MediaPlayerView.xaml:
<Grid HorizontalAlignment="Left" Width="500" Margin="0,0,-208,0" RenderTransformOrigin="0.50,0.50">
<ContentControl Content="{Binding MediaElementObject}"/>
</Grid>
Now I found many solutions to open window in MVVM pattern but the only one I liked was this one I tried to implement:
public class WindowHandler
{
public void ShowWindow(object dataContext, int height, int width)
{
Window window = new Window()
{
Content = dataContext,
Width = width,
Height = height
};
window.Show();
}
}
The code of the command which is bound to my button is:
var windowHandler = new WindowHandler();
windowHandler.ShowWindow(new MediaPlayerViewModel(directLink), 500, 500);
And the Constructor of the MediaPlayerViewModel is:
class MediaPlayerViewModel : INotifyPropertyChanged
{
private Uri mediaSource;
public MediaPlayerViewModel()
{
mediaElementObject = new MediaElement();
mediaElementObject.LoadedBehavior = MediaState.Manual;
mediaElementObject.MediaOpened += OnMediaOpened;
}
public MediaPlayerViewModel(string directLink) : this()
{
mediaSource = new Uri(directLink);
mediaElementObject.Source = mediaSource;
mediaElementObject.Play();
OnPropertyChanged("MediaElementObject");
}
/*...........*/
}
Last my MediaPlayerView.xaml.cs
public partial class MediaPlayerView : Window
{
private MediaPlayerViewModel viewModel;
public MediaPlayerView()
{
InitializeComponent();
viewModel = new MediaPlayerViewModel();
this.DataContext = viewModel;
}
}
EDIT: It doesn't change anything if i set
this.DataContext = viewModel;
or
this.Content = viewModel;
but I think the second one is the right to go here right?
Now if I click my button in the main view, a new window opens but it only shows
myNamespace.MediaPlayerViewModel
instead of my mediaElement playing... I guess it has something to do with the content control as mentioned earlier but I don't know how to fix it. I mean if i can't pass window to style element, what should i pass then? Shouldn't my new window inherit from window? And what does that x:Type thing in the contentcontrol mean? I read something about dynamic content control but I think I don't need this in my new window.
Hope I wrote an understandable post and someone can help me :)
EDIT: Well, I'm pretty sure now that my problem is related to the way I bind my ViewModel to my View... As I said and mentioned in the comments I tried to do the way like in Opening new window in MVVM WPF. But I think it does not recognize the connection from my viewmodel with the view.... But why? :S
If I got the idea right, my windowHandler with this code
windowHandler.ShowWindow(new MediaPlayerViewModel(), 500, 500);
calls ShowWindow and creates a new Window with
Content = dataContext
Since we bound the MediaPlayerViewModel to MediaPlayerView it should automatically draw the right view... but it doesn't :S
EDIT: I still don't know why it didn't work.... tried it now with an IWindowFactory Interface as in another thread here to create the view and open window like one would usually do... Would've been better with this approach... maybe someone can help me some day:D