[聚合文章] MVVMLight的流程简单分析(二)

.Net 1900-01-01 30 阅读
APP分部类

首先关注的肯定是APP类启动的地方,我们从app.xaml.cs开始着手。我们看到有好几个函数,因为是讲流程,所以就不每个函数都介绍过去了,这里重点关注那个OnLaunched函数。这个是App一开始启动调用的函数。

从注释也可以看出,这是在用户启动应用是触发调用。

        protected override void OnLaunched(LaunchActivatedEventArgs e)        {            //TODO: 中文为自己添加的注释说明            //首先获取当前窗口的内容            Frame rootFrame = Window.Current.Content as Frame;            // Do not repeat app initialization when the Window already has content,            // just ensure that the window is active            //如果内容也就是框架Frame为空,则创建一个新的,并放置到窗口中            if (rootFrame == null)            {                // Create a Frame to act as the navigation context and navigate to the first page                rootFrame = new Frame();                rootFrame.NavigationFailed += OnNavigationFailed;                if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)                {                    //TODO: Load state from previously suspended application                }                // Place the frame in the current Window                Window.Current.Content = rootFrame;            }            //全新应用启动时,会进入这个if条件中            if (e.PrelaunchActivated == false)            {                //判断框架中是否存在内容,主要是页面Page类                if (rootFrame.Content == null)                {                    // When the navigation stack isn't restored navigate to the first page,                    // configuring the new page by passing required information as a navigation                    // parameter                    //初始时,导航栈肯定为空,我们需要导航到第一页,即放置内容到框架中。                    //这个页面就是我们的首页,或者说主页面。                    rootFrame.Navigate(typeof(MainPage), e.Arguments);                }                // Ensure the current window is active                Window.Current.Activate();            }            //这里是MVVMLight框架封装的一个调度工具            //主要的用处是让我们可以在非UI线程中,可以委托调度器帮我们去完成UI的操作            //在此处初始化            DispatcherHelper.Initialize();            //MVVMLight在此处注册了一个消息,            //我们可以看到上图中的那个函数就是消息的回调            //自己暂时没用过这个封装的消息工具            Messenger.Default.Register<NotificationMessageAction<string>>(                this,                HandleNotificationMessage);        }

当然我们知道这个是App的分部类,那么其他的部分在app.xaml文件中。而在那里,声明了很重要的东西。我们接着往下看。

注:本文内容来自互联网,旨在为开发者提供分享、交流的平台。如有涉及文章版权等事宜,请你联系站长进行处理。