How to bring window to front with wpf and using mvvm

前端 未结 3 1702
野的像风
野的像风 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: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(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
    {
    }
    

提交回复
热议问题