简体   繁体   English

如何使用wpf和使用mvvm将窗口带到前面

[英]How to bring window to front with wpf and using mvvm

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. 当计时器达到0时,我想将窗口置于前面,以便它可见并且不会隐藏在其他应用程序后面。

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. 从我可以收集的内容中我只需要调用window.activate()来完成此操作但是使用mvvm我的视图模型没有对窗口的引用。

A "purist" MVVM solution is to use a behavior. “纯粹的”MVVM解决方案是使用行为。 Below is a behavior for a Window with an Activated property. 以下是具有Activated属性的Window的行为。 Setting the property to true will activate the window (and restore it if it is minimized): 将该属性设置为true将激活该窗口(如果它被最小化,则恢复它):

public class ActivateBehavior : Behavior<Window> {

  Boolean isActivated;

  public static readonly DependencyProperty ActivatedProperty =
    DependencyProperty.Register(
      "Activated",
      typeof(Boolean),
      typeof(ActivateBehavior),
      new PropertyMetadata(OnActivatedChanged)
    );

  public Boolean Activated {
    get { return (Boolean) GetValue(ActivatedProperty); }
    set { SetValue(ActivatedProperty, value); }
  }

  static void OnActivatedChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e) {
    var behavior = (ActivateBehavior) dependencyObject;
    if (!behavior.Activated || behavior.isActivated)
      return;
    // The Activated property is set to true but the Activated event (tracked by the
    // isActivated field) hasn't been fired. Go ahead and activate the window.
    if (behavior.AssociatedObject.WindowState == WindowState.Minimized)
      behavior.AssociatedObject.WindowState = WindowState.Normal;
    behavior.AssociatedObject.Activate();
  }

  protected override void OnAttached() {
    AssociatedObject.Activated += OnActivated;
    AssociatedObject.Deactivated += OnDeactivated;
  }

  protected override void OnDetaching() {
    AssociatedObject.Activated -= OnActivated;
    AssociatedObject.Deactivated -= OnDeactivated;
  }

  void OnActivated(Object sender, EventArgs eventArgs) {
    this.isActivated = true;
    Activated = true;
  }

  void OnDeactivated(Object sender, EventArgs eventArgs) {
    this.isActivated = false;
    Activated = false;
  }

}

The behavior requires a reference to System.Windows.Interactivity.dll . 该行为需要引用System.Windows.Interactivity.dll Fortunately, this is now available on NuGet in the Blend.Interactivity.Wpf package. 幸运的是,现在可以在Blend.Interactivity.Wpf包中的NuGet上使用

The behavior is attached to a Window in XAML like this: 该行为附加到XAML中的窗口,如下所示:

<Window ...>
  <i:Interaction.Behaviors>
    <Behaviors:ActivateBehavior Activated="{Binding Activated, Mode=TwoWay}"/>
  </i:Interaction.Behaviors>

The view-model should expose a boolean Activated property. 视图模型应该公开一个布尔值的Activated属性。 Setting this property to true will activate the window (unless it is already activated). 将此属性设置为true将激活窗口(除非它已被激活)。 As an added bonus it will also restore a minimized window. 作为额外的奖励,它还将恢复最小化的窗口。

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 您可以通过以下几种方式进行操作 - 添加对窗口的引用可能会起作用,因为视图模型未与视图耦合但与之相关,但我不太喜欢这种方法,因为它几乎可以结合您的视图到你的viewmodel - 这不是MVVM的重点

A better approach may be to have your viewmodel raise an event or a command which the view can handle. 更好的方法可能是让viewmodel引发视图可以处理的事件或命令。 This way the view gets to decide what UI action is associated with the command/event 通过这种方式,视图可以决定与命令/事件关联的UI操作

eg simply 比如简单

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 当然,你如何连接它取决于你,但我可能会尝试让路由命令发生

Edit: 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 ... eg imagine the following MainWindow with a ViewModel property 一个简单的基于事件的示例只是将事件添加到viewmodel并直接处理它...例如想象以下带有ViewModel属性的MainWindow

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: 然后ViewModel可以触发事件:

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

The XAML would be: XAML将是:

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

So the button invokes the command, which in turn fires the event which the view (MainWindow) handles and shows a messagebox. 因此该按钮调用该命令,该命令又触发视图(MainWindow)处理的事件并显示消息框。 This way the view/UI decides on the course of action based on the type of event raised. 这样,视图/ UI根据引发的事件类型决定操作过程。 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... 您可以随时查看更为复杂的路线,例如此问题的一些答案......

How should the ViewModel close the form? 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! 但说实话,这取决于你是否真的需要它 - 一个简单的事件很有效 - 有些人为了优雅而过于复杂化,但却不利于简单和生产力!

I would go this way: 我会这样走:

using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;    
using GalaSoft.MvvmLight.Messaging; 

// View

public partial class TestActivateWindow : Window
{
    public TestActivateWindow() {
        InitializeComponent();
        Messenger.Default.Register<ActivateWindowMsg>(this, (msg) => Activate());
    }
}

// View Model

public class MainViewModel: ViewModelBase
{
    ICommand _activateChildWindowCommand;

    public ICommand ActivateChildWindowCommand {
        get {
            return _activateChildWindowCommand?? (_activateChildWindowCommand = new RelayCommand(() => {
                Messenger.Default.Send(new ActivateWindowMsg());
        }));
        }
    }
}

public class ActivateWindowMsg
{
}

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

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