0

我正在检测我的 WPF 项目处于非活动状态。我一直在使用这个代码。

 readonly DispatcherTimer activityTimer;


    public MainWindow()
    {
        InitializeComponent();

        InputManager.Current.PreProcessInput += Activity;

        activityTimer = new DispatcherTimer
        {
            Interval = TimeSpan.FromSeconds(5),
            IsEnabled = true
        };
        activityTimer.Tick += Inactivity;

    }

    #region
    void Inactivity(object sender, EventArgs e)
    {


        FrameBody.Source = new Uri("Pages/Body.xaml", UriKind.Relative);


    }

    void Activity(object sender, PreProcessInputEventArgs e)
    {
        activityTimer.Stop();
        activityTimer.Start();


    }
    #endregion

它确实有效,但只有一个问题。当用户不对系统做任何事情时,它会改变框架的来源 FrameBody.Source = new Uri("Pages/Body.xaml", UriKind.Relative); 但它也涉及到最近的框架来源。

在我的系统中,我使用 1 个带有 Frame 的窗口。并且该帧的来源会根据用户与系统的交互而变化。(例如,当用户单击按钮时,该帧的来源会发生变化,当没有点击或鼠标突然移动几秒钟/分钟时,帧的来源将发生变化。)

谁能告诉我我的代码有什么问题?上面的代码是基于这个WPF 不活动和活动

4

0 回答 0