2

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

Community
  • 1
  • 1
syc
  • 73
  • 1
  • 8
  • 1
    I believe the Content shouldn't hold the ViewModel, it should hold the controls hierarchy that you want to show... The ViewModel should be set as the DataContext – Yitzchak Dec 13 '16 at 07:03
  • The content holds the mediaelement or what do you mean? And I read this on stackoverflow. This xaml code should assigns the view to the viewmodel so that the program automatically uses the right view for the used viewmodel. And of course I set the view model as Datacontext in my Media Player view^^ – syc Dec 13 '16 at 07:12
  • I can't see from your code if the ViewModel is a view class that can be displayed. The fact that it's creating an instance of a view doesn't mean that it is a view! So if you do Content = ViewModel; I don't think it will be displayed! It should be something like Content = YourMediaElement; – Yitzchak Dec 13 '16 at 07:16
  • Hey I added the class body of MediaPlayerViewModel and it inherits from INotifyPropertyChanged if you mean that with being displayable. I also added the code of MediaPlayerView. And I assigned the datacontext instead of content.... Maybe it is that? I will try :P – syc Dec 13 '16 at 07:30
  • You should never use `System.Windows.Controls` in your viewModel. This is a sign of wrong implementation, and `MediaElement` is in that namespace. Your new window should be a new ViewModel with View as Window, and then you would instantiate viewModel and bind it to your view – FCin Dec 13 '16 at 07:35
  • I don'the get it... my new window is a Viewmodel with a View as Windows and I try to bind it to my view through the code with xaml code. I just tried with User control because of the warning. I tried to use a kind of this solution from stackoverflow which is often mentioned here, so I don't understand what you mean with not using controls... here the link:http://stackoverflow.com/questions/11431612/binding-medialelement-from-the-view-xaml-to-a-viewmodels-property – syc Dec 13 '16 at 07:44
  • edited my answer with things I tried now. Did not change anything :S – syc Dec 13 '16 at 07:50
  • You instantiate your `mediaElementObject = new MediaElement();` in your viewModel which is a wpf framework element. If you would want in the future to change your `MediaElement` to something else you would need to modify your viewModel. This is exactly why `MVVM` was created. So you don't need to modify your viewModel. You should either bind play event to a command or create an event in your viewModel and access it in your view. – FCin Dec 13 '16 at 08:16
  • I got this from a stackoverflow question where was said that this won't affect mvvm because I can bind my mediaelement to any view with contentcontrol.... just easier as firing events for anything... I didn't know which one was the better approach but this looked much easier :S besides that my program is made to play video streams per link, which will ever be made by mediaelement or custom thing inheriting mediaelement. Besides I thought that it is also mvvm that the view don't has much logic/code. But the question is here why this is not working :P thx for your post anyway. – syc Dec 13 '16 at 08:35
  • I'll use events and bind mediaelement if I have a project where this will eventually be changed;) – syc Dec 13 '16 at 08:36
  • I don't think you need that `` in your `DataTemplate`. Look at this: http://stackoverflow.com/questions/25845689/opening-new-window-in-mvvm-wpf – FCin Dec 13 '16 at 08:56
  • Yeah I tried without and didn't change. In this thread isn't mentioned what the view inherits from. Look at my post. I get an warning that window element can't be placed in style element. The usercontrol was a try to fix it. – syc Dec 13 '16 at 09:03
  • Edited my post... the content binding is no problem at first I think. The View is not drawed although it is bound to my ViewModel. – syc Dec 13 '16 at 11:10

0 Answers0