예제 #1
0
파일: Forms.cs 프로젝트: zmtzawqlp/maui
        public static void Init(IActivatedEventArgs launchActivatedEventArgs, IEnumerable <Assembly> rendererAssemblies = null)
        {
            if (IsInitialized)
            {
                return;
            }

            var accent = (SolidColorBrush)global::Windows.UI.Xaml.Application.Current.Resources["SystemColorControlAccentBrush"];

            Color.SetAccent(accent.ToFormsColor());

#if UWP_14393
            Log.Listeners.Add(new DelegateLogListener((c, m) => Debug.WriteLine(LogFormat, c, m)));
#else
            Log.Listeners.Add(new DelegateLogListener((c, m) => Trace.WriteLine(m, c)));
#endif
            if (!global::Windows.UI.Xaml.Application.Current.Resources.ContainsKey("RootContainerStyle"))
            {
                global::Windows.UI.Xaml.Application.Current.Resources.MergedDictionaries.Add(GetTabletResources());
            }

            try
            {
                global::Windows.UI.Xaml.Application.Current.Resources.MergedDictionaries.Add(new Microsoft.UI.Xaml.Controls.XamlControlsResources());
            }
            catch
            {
                Log.Warning("Resources", "Unable to load WinUI resources. Try adding System.Maui nuget to UWP project");
            }

            Device.SetIdiom(TargetIdiom.Tablet);
            Device.SetFlowDirection(GetFlowDirection());
            Device.PlatformServices = new WindowsPlatformServices(Window.Current.Dispatcher);
            Device.SetFlags(s_flags);
            Device.Info = new WindowsDeviceInfo();

            switch (global::Windows.System.Profile.AnalyticsInfo.VersionInfo.DeviceFamily)
            {
            case "Windows.Desktop":
                if (global::Windows.UI.ViewManagement.UIViewSettings.GetForCurrentView().UserInteractionMode ==
                    global::Windows.UI.ViewManagement.UserInteractionMode.Touch)
                {
                    Device.SetIdiom(TargetIdiom.Tablet);
                }
                else
                {
                    Device.SetIdiom(TargetIdiom.Desktop);
                }
                break;

            case "Windows.Mobile":
                Device.SetIdiom(TargetIdiom.Phone);
                break;

            case "Windows.Xbox":
                Device.SetIdiom(TargetIdiom.TV);
                break;

            default:
                Device.SetIdiom(TargetIdiom.Unsupported);
                break;
            }

            ExpressionSearch.Default = new WindowsExpressionSearch();

            Registrar.ExtraAssemblies = rendererAssemblies?.ToArray();

            Registrar.RegisterAll(new[] { typeof(ExportRendererAttribute), typeof(ExportCellAttribute), typeof(ExportImageSourceHandlerAttribute), typeof(ExportFontAttribute) });

            IsInitialized = true;
            s_state       = launchActivatedEventArgs.PreviousExecutionState;

            Platform.UWP.Platform.SubscribeAlertsAndActionSheets();
        }
예제 #2
0
        static void SetupInit(
            Context activity,
            Assembly resourceAssembly,
            InitializationOptions?maybeOptions = null
            )
        {
            Profile.FrameBegin();

            if (!IsInitialized)
            {
                // Only need to get this once; it won't change
                ApplicationContext = activity.ApplicationContext;
            }

#pragma warning disable 618 // Still have to set this up so obsolete code can function
            Context = activity;
#pragma warning restore 618

            if (!IsInitialized)
            {
                // Only need to do this once
                Profile.FramePartition("ResourceManager.Init");
                ResourceManager.Init(resourceAssembly);
            }

            Profile.FramePartition("Color.SetAccent()");
            // We want this to be updated when we have a new activity (e.g. on a configuration change)
            // This could change if the UI mode changes (e.g., if night mode is enabled)
            Color.SetAccent(GetAccentColor(activity));
            _ColorButtonNormalSet = false;

            if (!IsInitialized)
            {
                // Only need to do this once
                Profile.FramePartition("Log.Listeners");
                Internals.Log.Listeners.Add(new DelegateLogListener((c, m) => Trace.WriteLine(m, c)));
            }

            // We want this to be updated when we have a new activity (e.g. on a configuration change)
            // because AndroidPlatformServices needs a current activity to launch URIs from
            Profile.FramePartition("Device.PlatformServices");
            Device.PlatformServices = new AndroidPlatformServices(activity);

            // use field and not property to avoid exception in getter
            if (Device.info != null)
            {
                ((AndroidDeviceInfo)Device.info).Dispose();
                Device.info = null;
            }

            // We want this to be updated when we have a new activity (e.g. on a configuration change)
            // because Device.Info watches for orientation changes and we need a current activity for that
            Profile.FramePartition("create AndroidDeviceInfo");
            Device.Info = new AndroidDeviceInfo(activity);

            Profile.FramePartition("setFlags");
            Device.SetFlags(s_flags);

            Profile.FramePartition("AndroidTicker");
            Ticker.SetDefault(null);

            Profile.FramePartition("RegisterAll");

            if (!IsInitialized)
            {
                if (maybeOptions.HasValue)
                {
                    var options      = maybeOptions.Value;
                    var handlers     = options.Handlers;
                    var flags        = options.Flags;
                    var effectScopes = options.EffectScopes;

                    //TODO: ExportCell?
                    //TODO: ExportFont

                    // renderers
                    Registrar.RegisterRenderers(handlers);

                    // effects
                    if (effectScopes != null)
                    {
                        for (var i = 0; i < effectScopes.Length; i++)
                        {
                            var effectScope = effectScopes[0];
                            Registrar.RegisterEffects(effectScope.Name, effectScope.Effects);
                        }
                    }

                    // css
                    Registrar.RegisterStylesheets(flags);
                }
                else
                {
                    // Only need to do this once
                    Registrar.RegisterAll(new[] {
                        typeof(ExportRendererAttribute),
                        typeof(ExportCellAttribute),
                        typeof(ExportImageSourceHandlerAttribute),
                        typeof(ExportFontAttribute)
                    });
                }
            }

            Profile.FramePartition("Epilog");

            var currentIdiom = TargetIdiom.Unsupported;

            // first try UIModeManager
            using (var uiModeManager = UiModeManager.FromContext(ApplicationContext))
            {
                try
                {
                    var uiMode = uiModeManager?.CurrentModeType ?? UiMode.TypeUndefined;
                    currentIdiom = DetectIdiom(uiMode);
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine($"Unable to detect using UiModeManager: {ex.Message}");
                }
            }

            if (TargetIdiom.Unsupported == currentIdiom)
            {
                // This could change as a result of a config change, so we need to check it every time
                int minWidthDp = activity.Resources.Configuration.SmallestScreenWidthDp;
                Device.SetIdiom(minWidthDp >= TabletCrossover ? TargetIdiom.Tablet : TargetIdiom.Phone);
            }

            if (SdkInt >= BuildVersionCodes.JellyBeanMr1)
            {
                Device.SetFlowDirection(activity.Resources.Configuration.LayoutDirection.ToFlowDirection());
            }

            if (ExpressionSearch.Default == null)
            {
                ExpressionSearch.Default = new AndroidExpressionSearch();
            }

            IsInitialized = true;
            Profile.FrameEnd();
        }