0

I'm building a pos system that has a main ContentControl to display different screens of the application. I use DataTemplates to map my viewmodels to views. To navigate between the different views displayed in the ContentControl I'd like to store a screenshot of the UserControl in the viewmodel when the UserControl is unloaded (or the ContentControl changes).

I posted a related question here WPF Binding FrameworkElement event to command in which I attempted to bind a command to FrameworkElement.Unloaded but that doesn't work (see answer at that link)

Is this possible without breaking the MVVM pattern?

Community
  • 1
  • 1
Chris Klepeis
  • 9,783
  • 16
  • 83
  • 149
  • 2
    The only time you "break" mvvm is when the viewmodel is doing UI work. MVVM != no codebehind. Codebehind is fine for *UI code*. –  Jul 12 '12 at 13:10
  • when doing viewmodel first why you need event to command at all? your viemwodel handle the navigation and so the viewmodel know when someone want a new view and can save a screenshot from the old one. – blindmeis Jul 12 '12 at 13:13
  • @blindmeis - Yes, the viewmodel handles the navigation, and so I can determine when it changes, however, getting the screenshot is what I'm not sure how to handle. – Chris Klepeis Jul 12 '12 at 13:23
  • @Will - Good point. That may very well be the route I go... I just wanted to make sure I wasn't missing something obvious – Chris Klepeis Jul 12 '12 at 13:24

1 Answers1

1

here is a nice link to how to do a screenshot in wpf.

here is what i would do:

my mainviewmodel which handle the navigation should expose an event and raise this event before you set the new contentviewmodel. the old contentviewmodel should be in the eventargs. in your mainwindow codebehind you subscribe to the event(not breaking mvvm here). when ever this event is raise you can call the screenshot method and put the result to the oldviewmodel.

edit:

mainwindow codebehind

void NavigationChangingEvent(object sender, NavChangingArgs args)
{
   var oldvm = args.ChangingViewmodel;
   oldvm.Screenshoot = this.mycontentcontrolwheremyviewmodelareshown.GetJpgImage(1, 90);
}
Community
  • 1
  • 1
blindmeis
  • 22,175
  • 7
  • 55
  • 74
  • Thanks, that's the exact function I found as well and was planning on using. I'll try out your suggestion. – Chris Klepeis Jul 12 '12 at 13:27
  • Using that function, wouldn't the viewmodel event have to accept a UIElement? I guess that is where I thought it would break MVVM – Chris Klepeis Jul 12 '12 at 13:31
  • well no. the viewmodel expose an event with the oldviewmodel in the eventargs. and the codebehind from your mainview subscribe to the event and take the screenshot. – blindmeis Jul 12 '12 at 14:12