Windows窗体初始化(MVC)时触发事件/加载数据

时间:2019-05-29 15:57:31

标签: c# winforms events

好的,所以我为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”,但是不会触发。

1 个答案:

答案 0 :(得分:0)

可以将主表单的不透明度属性设置为0%。

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

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

完成所需的任何启动处理后,您可以关闭初始屏幕窗体并将主窗体的不透明度设置为100%,这将使其可见。