How to bring window to front with wpf and using mvvm

前端 未结 3 1675
野的像风
野的像风 2021-01-18 06:46

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 applica

相关标签:
3条回答
  • 2021-01-18 07:20

    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

    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

    e.g. 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.

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

    The XAML would be:

    <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. 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...

    How should the ViewModel close the form?

    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!

    0 讨论(0)
  • 2021-01-18 07:39

    A "purist" MVVM solution is to use a behavior. Below is a behavior for a Window with an Activated property. Setting the property to true will activate the window (and restore it if it is minimized):

    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. Fortunately, this is now available on NuGet in the Blend.Interactivity.Wpf package.

    The behavior is attached to a Window in XAML like this:

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

    The view-model should expose a boolean Activated property. Setting this property to true will activate the window (unless it is already activated). As an added bonus it will also restore a minimized window.

    0 讨论(0)
  • 2021-01-18 07:43

    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
    {
    }
    
    0 讨论(0)
提交回复
热议问题