Newer
Older
Correlator / Correlator / App.xaml.cs
using System;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
using Correlator.DataService;
using Correlator.Dialog;
using Correlator.Util;
using Correlator.ViewModels;
using Correlator.Views;
using Prism.DryIoc;
using Prism.Ioc;

namespace Correlator
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : PrismApplication
    {
        /// <summary>
        /// 全局log捕获
        /// </summary>
        public App()
        {
            Startup += delegate
            {
                //UI线程未捕获异常处理事件
                DispatcherUnhandledException += Dispatcher_UnhandledException;

                //Task线程内未捕获异常处理事件
                TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;

                //非UI线程未捕获异常处理事件
                AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            };
        }

        private void Dispatcher_UnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {
            "App".WriteLog($"{e.Exception.StackTrace}");
        }

        private void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
        {
            "App".WriteLog($"{e.Exception.StackTrace}");
        }

        private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            var builder = new StringBuilder();
            if (e.IsTerminating)
            {
                builder.Append("非UI线程发生致命错误");
            }

            if (e.ExceptionObject is Exception exception)
            {
                builder.Append(exception.Message);
            }
            else
            {
                builder.Append(e.ExceptionObject);
            }

            "App".WriteLog($"{builder}");
        }

        protected override Window CreateShell()
        {
            return Container.Resolve<MainWindow>();
        }

        protected override void InitializeShell(Window shell)
        {
            //可以实现登录
            var startupWindow = Container.Resolve<StartupWindow>();
            var result = startupWindow.ShowDialog();
            if (result == null)
            {
                Current.Shutdown();
                return;
            }

            if (result.Value)
            {
                base.OnInitialized();
            }
            else
            {
                Current.Shutdown();
            }
        }

        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            //Data
            containerRegistry.RegisterSingleton<IApplicationDataService, ApplicationDataServiceImpl>();
            containerRegistry.RegisterSingleton<ISerialPortService, SerialPortServiceImpl>();
            containerRegistry.RegisterSingleton<IAudioService, AudioServiceImpl>();
            containerRegistry.RegisterSingleton<ICommandService, CommandServiceImpl>();

            //Dialog or Window
            containerRegistry.RegisterDialog<ImageFileExplorerDialog, ImageFileExplorerDialogViewModel>();
            containerRegistry.RegisterDialog<BigPictureView, BigPictureViewModel>();
            containerRegistry.RegisterDialog<AudioFileExplorerDialog, AudioFileExplorerDialogViewModel>();
            containerRegistry.RegisterDialog<AudioVisualizeDialog, AudioVisualizeDialogViewModel>();
            containerRegistry.RegisterDialog<NoiseWaveDialog, NoiseWaveDialogViewModel>();
            containerRegistry.RegisterDialog<SoundSampleRateDialog, SoundSampleRateDialogViewModel>();
            containerRegistry.RegisterDialog<EditSoundSampleRateDialog, EditSoundSampleRateDialogViewModel>();
            containerRegistry.RegisterDialog<SoundSpeedDialog, SoundSpeedDialogViewModel>();
            containerRegistry.RegisterDialog<AddSoundSpeedDialog, AddSoundSpeedDialogViewModel>();
            containerRegistry.RegisterDialog<EditSoundSpeedDialog, EditSoundSpeedDialogViewModel>();
            containerRegistry.RegisterDialog<ImportHydrophoneDataDialog, ImportHydrophoneDataDialogViewModel>();
            containerRegistry.RegisterDialog<CheckResponseDialog, CheckResponseDialogViewModel>();
            containerRegistry.RegisterDialog<SimplyAuditionDialog, SimplyAuditionDialogViewModel>();
            containerRegistry.RegisterDialog<DetectNoiseDialog, DetectNoiseDialogViewModel>();
            containerRegistry.RegisterDialog<ApplicationInfoDialog, ApplicationInfoDialogViewModel>();
            containerRegistry.RegisterDialog<NumericKeypadDialog, NumericKeypadDialogViewModel>();
        }
    }
}