I’m using MVVM Light Toolkit and in my View, I have a function that takes a screenshot and returns byte array of that screenshot. Since taking an screenshot (using UIElements) is related to view not ViewModel.
byte[] TakeScreenShot(Canvas sourceUiElement)
I need to get the return value of the function in my ViewModel but I can’t come up with a proper way of doing it.
I other hand if I wanted to do move this function to my ViewModel, I need to have access to that element in view but without referencing the View in my ViewModel (maybe as argument or something to a Command?)
Since this question is tagged as MvvmLight, then here is an MvvmLight Toolkit answer. Use said toolkit’s
Messengerclass. Simply define the following message classes somewhere in your application:In your code-behind’s constructor, register for the TakeScreenshotMessage like this:
And in your view model, register for the ScreenshotTakenMessage like this:
Now you can take a screen shot at any time simply by calling the following from anywhere in your application (i.e. view models, views, helper classes, etc.):