【问题标题】:MVVM method for window to appear in front窗口出现在前面的MVVM方法
【发布时间】:2014-10-02 04:16:27
【问题描述】:

我有一个 wpf 应用程序,它需要根据某些条件在所有其他窗口的前面弹出。我正在尝试以符合 MVVM 的方式完成此操作。经过大量研究,最好的解决方案是使用窗口的激活事件和界面的行为。我能够使用在 mvvm activate 上找到的第二个解决方案使激活功能工作。但是,一旦我使用基于quickstart guide 的 Mahapp Metro 控件“MetroWindow”来设置我的 GUI 样式,GUI 就会出现混乱。每当添加 XAML 交互行为部分时,就会出现 GUI 的原始 Window 对象,从而产生 2 个标题栏(一个来自 Window,另一个来自 MetroWindow)。我尝试使用 Metro 样式设计自己的窗口,但到目前为止无法实现 Metro 标题栏(找不到任何指南或文档)。 Visual Studio 还不断抱怨“对象引用未设置为对象的实例”(有时在

<controls:MetroWindow
...

其他时间在

<i:Interaction.Behaviors>
    <Behaviors:ActivateBehavior ...

但应用程序仍在运行。

我如何保持 Mahapps Metro 样式(包括标题栏样式)并按照我的业务逻辑以符合 MVVM 的方式使窗口显示在前面?

【问题讨论】:

    标签: c# wpf mvvm mahapps.metro attachedbehaviors


    【解决方案1】:

    这不是你问题本质的答案,但也许你可以使用这样的东西来激活一个窗口:

    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 ActivateWindowMsg
    {
    }
    
    public class MainViewModel: ViewModelBase
    {
        ICommand _activateChildWindowCommand;
    
        public ICommand ActivateChildWindowCommand {
            get {
                return _activateChildWindowCommand?? (_activateChildWindowCommand = new RelayCommand(() => {
                    Messenger.Default.Send(new ActivateWindowMsg());
            }));
            }
        }
    }
    

    【讨论】:

    • 这不是不符合 mvvm 的吗?
    • @SILENT ViewModel 对视图一无所知,所以我认为它不违反 MVVM 模式。其实TestActivateWindowMainViewmodel可以放在不同的dll-s中,可以通过绑定和消息相互通信。
    • ActivateWindowMsg 不是 View 的一部分,VM 必须直接引用它吗? MVVM 让我更加困惑。
    • @SILENT 不,ActivateWindowMsg 类是 VM 的一部分。我已经澄清了代码。
    猜你喜欢
    • 2022-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多