由于这个问题被标记为 MvvmLight,所以这里有一个 MvvmLight Toolkit 答案。使用所述工具包的Messenger
类。只需在应用程序的某处定义以下消息类:
public class TakeScreenshotMessage : MessageBase { }
public class ScreenshotTakenMessage : GenericMessage<byte[]>
{
public ScreenshotTakenMessage (byte[]content) : base(content) { }
public ScreenshotTakenMessage (object sender, byte[]content) : base(sender, content) { }
public ScreenshotTakenMessage (object sender, object target, byte[]content) : base(sender, target, content) { }
}
在代码隐藏的构造函数中,像这样注册 TakeScreenshotMessage:
Messenger.Default.Register<TakeScreenshotMessage>(this, (msg) =>
{
byte[] bytes = this.TakeScreenShot(someCanvas);
Messenger.Default.Send(new ScreenshotTakenMessage(bytes));
});
在您的视图模型中,像这样注册 ScreenshotTakenMessage:
Messenger.Default.Register<ScreenshotTakenMessage>(this, (msg) =>
{
byte[] bytes = msg.Content.
//do something with your bytes
});
现在,您可以随时通过在应用程序中的任何位置调用以下命令(即视图模型、视图、助手类等)来截取屏幕截图:
Messenger.Default.Send(new TakeScreenshotMessage());