如何把窗口前与WPF和MVVM使用 [英] How to bring window to front with wpf and using mvvm

查看:96
本文介绍了如何把窗口前与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我的视图模型不具有窗口的引用。

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财产以下主窗口

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);
    }

}



然后视图模型可以只火事件:

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是:

The XAML would be:

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



于是按钮调用命令,这反过来触发该视图(主窗口)处理该事件并显示一个消息框。通过这种方式,观看/ 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...

WPF MVVM新手 - 应如何在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天全站免登陆