代码之家  ›  专栏  ›  技术社区  ›  steschwa

关闭模式对话框后应用程序退出

  •  0
  • steschwa  · 技术社区  · 7 年前

    设置视图模型 . 我想展示 主视图模型 演出结束后立即 作为模式对话框,用户可以在应用程序启动之前对其进行配置。

    当我启动应用程序时 主视图模型 不显示,但会弹出模式。当我关闭对话框时 TryClose(true) in Start() 关闭,以便我可以继续 Handle(Settings message) in MainViewModel

    AppBootstrapper:

    protected override void OnStartup(object sender, StartupEventArgs e)
    {
         DisplayRootViewFor<MainViewModel>();
    }
    

    设置视图模型:

    using Caliburn.Micro;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace AbrechnungPSA.ViewModels
    {
        class SettingsViewModel : Screen
        {
            private IEventAggregator _eventAggregator;
    
            public SettingsViewModel(IEventAggregator eventAggregator)
            {  
                _eventAggregator = eventAggregator;
            }
    
            /// <summary>
            /// Starts the Main Application
            /// </summary>
            public void Start()
            {
                TryClose(true); 
    
                _eventAggregator.PublishOnUIThread(new Settings());           
            }
        }
    }
    

    主视图模型:

    using Caliburn.Micro;
    using System;
    using System.Collections.Generic;
    using System.Dynamic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    
    namespace AbrechnungPSA.ViewModels
    {
        public class MainViewModel : PropertyChangedBase, IHandle<Settings>
        {
            private Settings _settings;
    
            private readonly IEventAggregator _eventAggregator;
            private readonly IWindowManager _windowManager;
    
            public MainViewModel(IEventAggregator eventAggregator, IWindowManager windowManager)
            {
                _eventAggregator = eventAggregator;
                _eventAggregator.Subscribe(this);
    
                _windowManager = windowManager;
    
                dynamic settings = new ExpandoObject();
                settings.WindowStartupLocation = WindowStartupLocation.CenterOwner;
                settings.ResizeMode = ResizeMode.NoResize;
    
                var result = _windowManager.ShowDialog(new SettingsViewModel(_eventAggregator), null, settings);
            }
    
            /// <summary>
            /// Receive User adjusted Settings and start Application
            /// </summary>
            /// <param name="message"></param>
            public void Handle(Settings message)
            {
                _settings = message;
    
                // Continue here ...
            }
        }
    }
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   mm8    7 年前

    ShowDialog 方法在关闭设置对话框之前不会返回。

    OnStartup 方法:

    protected override void OnStartup(object sender, StartupEventArgs e)
    {
        base.OnStartup(sender, e);
        DisplayRootViewFor<MainViewModel>();
    
        dynamic settings = new ExpandoObject();
        settings.WindowStartupLocation = WindowStartupLocation.CenterOwner;
        settings.ResizeMode = ResizeMode.NoResize;
    
        var settingsVm = new SettingsViewModel(_container.GetInstance<IEventAggregator>());
        var result = _container.GetInstance<IWindowManager>()
            .ShowDialog(settingsVm, null, settings);
    
        settingsVm.Start();
    }
    

    主视图模型:

    class MainViewModel : PropertyChangedBase, IHandle<Settings>
    {
        private Settings _settings;
    
        private readonly IEventAggregator _eventAggregator;
        private readonly IWindowManager _windowManager;
    
        public MainViewModel(IEventAggregator eventAggregator, IWindowManager windowManager)
        {
            _eventAggregator = eventAggregator;
            _eventAggregator.Subscribe(this);
            _windowManager = windowManager;
        }
    
        public void Handle(Settings message)
        {
            _settings = message;
            // Continue here ...
        }
    }