简体   繁体   English

Win32事件+ WPF + MVVM

[英]Win32 events + WPF + MVVM

SystemEvents.SessionSwitch += new SessionSwitchEventHandler(SystemEvents_SessionSwitch); SystemEvents.SessionSwitch + = new SessionSwitchEventHandler(SystemEvents_SessionSwitch);

I'm building a wpf application, that times the time between workstation locks and unlocks But I'm having a hard time implementing it without putting in in mainwindow codebehind 我正在构建一个wpf应用程序,它将工作站锁定和解锁之间的时间计算在一起但是我很难实现它而不需要在主窗口代码中放入

the code im using for starting and stopping the timer 用于启动和停止计时器的代码

SystemEvents.SessionSwitch +=
                new SessionSwitchEventHandler(SystemEvents_SessionSwitch);
private static void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e)
        {
            if (e.Reason == SessionSwitchReason.SessionLock)
            {
                //Start Timer
            }
            else if (e.Reason == SessionSwitchReason.SessionUnlock)
            {
                //Stop Timer -> show window
            }
        }

problem is, this event resides in Microsoft.Win32 - which I cannot seem to reference in XAML (if i could I would have hooked it up to an ICommand instead) 问题是,这个事件驻留在Microsoft.Win32中 - 我似乎无法在XAML中引用(如果我可以将它连接到ICommand)

so all you MVVM experts out there, what do I do about this? 那么你所有的MVVM专家,我该怎么办? Do i keep it in mainwindow codebehind? 我是否将它保存在mainwindow代码隐藏中? or can i actually reference Win32 in XAML 或者我可以在XAML中实际引用Win32

and a side question... the timer logic - do i keep this in seperate class and store the value in the model or? 和一个侧面问题......计时器逻辑 - 我是否将它保存在单独的类中并将值存储在模型中? needless to say - I'm fairly new to MVVM 不用说 - 我对MVVM很新

The core principal behind MVVM is unit-testability and separation of responsibility. MVVM背后的核心原则是单元可测性和责任分离。 This is typically enforced by making sure that the ViewModel has no knowledge of the layers above it (ie the "View") nor should it interact directly with any platform-specific classes (ie Microsoft.Win32.SystemEvents ). 这通常是通过确保ViewModel不知道它上面的层(即“视图”)来实现的,也不应该直接与任何特定于平台的类(即Microsoft.Win32.SystemEvents )交互。

One approach I might suggest would be be to create your own ISystemEvents interface, which exposes only the events you'd like your ViewModel to handle. 我可能建议的一种方法是创建自己的ISystemEvents接口,该接口仅公开您希望ViewModel处理的事件。 The implementation of this interface could be considered to be part of your 'Model' layer, and would essentially wrap the desired events from Microsoft.Win32.SystemEvents . 该接口的实现可以被认为是“模型”层的一部分,并且实际上将从Microsoft.Win32.SystemEvents包装所需的事件。 Your application would ' inject ' the interface as part of the ViewModel's initialization. 您的应用程序将“ 注入 ”接口作为ViewModel初始化的一部分。

public interface ISystemEvents
{
    event EventHandler<SessionSwitchEventArgs> SessionSwitch;
}

//Pass this implementation to your viewmodel, via the constructor
public class MySystemEvents : ISystemEvents
{
    public event EventHandler<SessionSwitchEventArgs> SessionSwitch
    {
        add { Microsoft.Win32.SystemEvents.SessionSwitch += value; }
        remove { Microsoft.Win32.SystemEvents.SessionSwitch -= value; }
    }
}

public class MyViewModel
{
    public MyViewModel(ISystemEvents systemEvents)
    {
        //Store the instance of your object here, and subscribe to the desired events
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM