首页 > 解决方案 > 在 Windows 窗体初始化 (MVC) 时触发事件/加载数据

问题描述

好的,所以我为我的 Windows 窗体应用程序设置了基本的 MVC。我在启动应用程序时尝试做的是在单独的线程上启动启动屏幕,当启动屏幕显示时,触发一个事件以使控制器从模型加载到我的静态数据库中,并在完成后关闭飞溅并启动主要形式。

但是,我了解到您不能从构造函数手动调用事件....有没有人有解决方法?

这是我的初始表单

public partial class SplashScreen : Form
    {
        //Delegate for cross thread call to close
        private delegate void CloseDelegate();

        //The type of form to be displayed as the splash screen
        static SplashScreen splashScreen = null;

        public SplashScreen()
        {
            InitializeComponent();
        }

        // A static entry point to launch SplashScreen.
        static private void ShowForm()
        {
            splashScreen = new SplashScreen();
            Application.Run(splashScreen);
        }

        static public void ShowSplashScreen()
        {
            // Make sure it is only launched once.
            if (splashScreen != null)
                return;
            Thread thread = new Thread(new ThreadStart(SplashScreen.ShowForm));
            thread.IsBackground = true;
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
        }

        // A static method to close the SplashScreen
        static public void CloseForm()
        {
            if (splashScreen != null)
            {
                splashScreen.Invoke(new CloseDelegate(SplashScreen.CloseFormInternal));
            }
        }

        static private void CloseFormInternal()
        {
            splashScreen.Close();
            splashScreen = null;
        }
    }

这是我的主要形式

public partial class Map : Form, IMapView
    {

        // Dictionary to hold overlays
        private static List<GMapOverlay> overlays = new List<GMapOverlay>();

        // global variables to track status of buttons
        private bool closedButtonStatus;
        private bool titleButtonStatus;

        /// <summary>
        /// Fired upon starting application
        /// </summary>
        public event Action StartupEvent;


        /// <summary>
        /// view constructor
        /// Creates a new real estate data map and loads in the county boundary data
        /// </summary>
        public Map()
        {
            SplashScreen.ShowSplashScreen();
            StartupEvent?.Invoke();
            SplashScreen.CloseForm();
            closedButtonStatus = false;
            titleButtonStatus = false;
            InitializeComponent();
            loadMap();
        }


        /// <summary>
        /// Loads the map and centers it over the united states, with desired default size metrics
        /// </summary>
        private void loadMap()
        {

            // Initialize map:
            gmap.MapProvider = GMap.NET.MapProviders.GoogleMapProvider.Instance;
            GMap.NET.GMaps.Instance.Mode = GMap.NET.AccessMode.ServerOnly;

            // Center map over the US
            gmap.Position = new PointLatLng(40, -98);
            this.Size = new Size(1360, 665);
            this.MinimumSize = new Size(1000, 600);
        }

所以我试图调用的事件是 Map 构造函数中的“StartupEvent”,但是它不会触发。

标签: c#winformsevents

解决方案


可以将主窗体不透明度属性设置为 0%。

当您启动程序时,主窗体将启动但不可见。

然后,您可以从主要表单事件(例如“加载”事件)显示初始屏幕。您还可以启动数据库负载等。

一旦你完成了你需要的任何启动处理,你就可以关闭你的初始屏幕表单并将你的主表单的不透明度设置为 100%,这将使它变得可见。


推荐阅读