关闭主窗口时,在ViewModel中放置一个计时器 [英] Disposing a timer inside a ViewModel, upon closing the main window

查看:160
本文介绍了关闭主窗口时,在ViewModel中放置一个计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前在我的应用程序中,我有一个RelayCommand,它调用一个启动Timer的动作.我有一个单独的命令,该命令调用Dispose()方法来摆脱/释放计时器.所有这些都位于项目的ViewModel对应项中.

Currently in my application, I have a RelayCommand which calls an action which starts a Timer. I have a separate command which the calls the Dispose() method to get rid of/release the timer. This all sits in the ViewModel counterpart of the project.

使用MVVM模式,在关闭窗口时如何处理Timer,以及执行关闭窗口的常规/默认操作?

Using the MVVM pattern, how would I dispose of this Timer upon closing the window, as well as carrying out the regular/default operation of closing the window?

如果有帮助,我还将使用MVVM-Light工具包.

I am also using the MVVM-Light toolkit, if this is of any help.

下面是我当前解决方案的一个示例:

An example of my current solution is shown below:

private static Timer dispatchTimer;

public MainViewModel()
{
    this.StartTimerCommand = new RelayCommand(this.StartTimerAction);
    this.StopTimerCommand = new RelayCommand(this.StopTimerAction);
}

public RelayCommand StartTimerCommand { get; private set; }
public RelayCommand StopTimerCommand { get; private set; }

private void StartTimerAction()
{
    dispatchTimer = new Timer(new TimerCallback(this.IgnoreThis), null, 0, 15);
}

private void StopTimerAction()
{
    dispatchTimer.Dispose();
}

查看(xaml)

....
Height="896"
Width="1109"
DataContext="{Binding Main, Source={StaticResource Locator}}">

    <Grid x:Name="mainGrid"
          Width="Auto"
          Height="Auto">              
          <Button x:Name="startTimerButton"
                  Content="Start Timer"
                  Command="{Binding StartTimerCommand}"/>
           <Button x:Name="stopTimerButton"
                   Content="Stop Timer"
                   Command="{Binding StopTimerCommand}"/>
    </Grid>

</Window>

查看(隐藏代码)

public MainWindow()
{
    this.InitializeComponent();

    //Would a 'Closing +=' event be called here?
}

推荐答案

关闭窗口可能会也可能不会处置您的视图模型,这取决于您如何注册它.一种方法是将绑定到视图中的Load/Unloaded事件并在那里进行工作:

Closing the windows may or may not dispose your viewmodel, depending how you register it. One approach would be bind to the Loaded/Unloaded events from the view and do the work there:

查看:

xmlns:i ="clr-namespace:System.Windows.Interactivity; assembly = System.Windows.Interactivity"

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

<i:Interaction.Triggers>
    <i:EventTrigger EventName="Loaded">
        <i:InvokeCommandAction Command="{Binding PageLoadedCommand}"/>
    </i:EventTrigger>
    <i:EventTrigger EventName="Unloaded">
        <i:InvokeCommandAction Command="{Binding PageUnLoadedCommand}"/>
    </i:EventTrigger>
</i:Interaction.Triggers>

ViewModel:

public RelayCommand PageUnLoadedCommand { get; private set; }
...
 PageUnLoadedCommand = new RelayCommand(() => OnPageUnLoadedCommand());

...

    private void OnPageUnLoadedCommand()
    {
        //Unsubscribe and dispose here
    }

这篇关于关闭主窗口时,在ViewModel中放置一个计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆