如何使用 wpf 和使用 mvvm 将窗口置于前面 [英] How to bring window to front with wpf and using mvvm

查看:44
本文介绍了如何使用 wpf 和使用 mvvm 将窗口置于前面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基本上运行计时器的窗口.当计时器达到 0 时,我想将窗口放在前面,以便它可见而不是隐藏在其他应用程序后面.

I have a window that essentially runs a timer. When the timer hits 0 I want to bring the window to the front so that it is visible and not hidden behind some other application.

据我所知,我会简单地调用 window.activate() 来完成此操作,但使用 mvvm 我的视图模型没有对 window 的引用.

From what I can gather I would simply call window.activate() to accomplish this but with mvvm my view model doesn't have a reference to window.

推荐答案

您可以通过多种方式进行处理 - 添加对窗口的引用可能会起作用,因为视图模型与视图无关,但与其相关,但我真的不喜欢这种方法,因为它确实将您的视图与您的视图模型结合起来——这并不是 MVVM 的真正重点

You could go about it in a couple of ways - adding a reference to the window could work since the viewmodel is not coupled with the view but related to it, but I don't really like that approach since it pretty much does couple your view to your viewmodel - which is not really the point of MVVM

更好的方法可能是让您的视图模型引发视图可以处理的事件或命令.通过这种方式,视图可以决定与命令/事件关联的 UI 操作

A better approach may be to have your viewmodel raise an event or a command which the view can handle. This way the view gets to decide what UI action is associated with the command/event

例如简单

class SomeView 
{
    void HandleSomeCommandOrEvent() 
    {
        this.Activate();
    }
}

当然你如何连接这取决于你,但我可能会尝试让路由命令发生

Of course how you wire this up is up to you but I'd probably try and get routed commands happening

你不能真正绑定"一个简单的事件,因为它是从视图模型调用的.

You can't really 'bind' a simple event, since it's invoked from the viewmodel.

一个简单的基于事件的示例只是将事件添加到视图模型并直接处理它......例如想象以下带有 ViewModel 属性的 MainWindow

A simple event based example is just to add the event to the viewmodel and handle it directly ... e.g. imagine the following MainWindow with a ViewModel property

public partial class MainWindow : Window
{
    MainWindowViewModel ViewModel { get; set; }

    public MainWindow()
    {
        InitializeComponent();

        ViewModel = new MainWindowViewModel();
        ViewModel.ShowMessage += ViewModel_ShowMessage;
        this.DataContext = ViewModel;
    }

    void ViewModel_ShowMessage(object sender, ShowMessageEventArgs e)
    {
        MessageBox.Show(e.Message, "Some caption", MessageBoxButton.OK);
    }

}

然后 ViewModel 可以触发事件:

Then the ViewModel can just fire the event:

// The view model
public class MainWindowViewModel
{
    // The button click command
    public RelayCommand ButtonClickCommand { get; set; }

    // The event to fire
    public event EventHandler<ShowMessageEventArgs> ShowMessage;

    public MainWindowViewModel()
    {            
        ButtonClickCommand = new RelayCommand(ButtonClicked);            
    }

    void ButtonClicked(object param)
    {
        // This button is wired up in the view as normal and fires the event
        OnShowMessage("You clicked the button");
    }

    // Fire the event - it's up to the view to decide how to implement this event and show a message
    void OnShowMessage(string message)
    {
        if (ShowMessage != null) ShowMessage(this, new ShowMessageEventArgs(message));
    }
}

public class ShowMessageEventArgs : EventArgs
{
    public string Message { get; private set; }

    public ShowMessageEventArgs(string message)
    {
        Message = message;
    }
}

XAML 将是:

<Button Command="{Binding ButtonClickCommand}">Click me!</Button>

因此按钮调用命令,该命令依次触发视图 (MainWindow) 处理的事件并显示消息框.这样,视图/UI 会根据引发的事件类型决定操作过程.当然,它可能是您的计时器触发了事件

So the button invokes the command, which in turn fires the event which the view (MainWindow) handles and shows a messagebox. This way the view/UI decides on the course of action based on the type of event raised. Of course it could be your timer which fired the event

你总是可以走更复杂的路线,比如这个问题的一些答案......

You can always go down the more involved route such as some of the answers on this question...

ViewModel 应该如何关闭表单?

但老实说,这取决于您是否真的需要它 - 一个简单的事件效果很好 - 有些人为了优雅而将事情过度复杂化,但却损害了简单性和生产力!

but to be honest, it depends if you really need it - a simple event works well - some people overcomplicate things for the sake of elegance, but at the detriment of simplicity and productivity!

这篇关于如何使用 wpf 和使用 mvvm 将窗口置于前面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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