internal static FrameworkElement ToFrameworkElement(this VisualElement visualElement) { if (!Forms.IsInitialized) { throw new InvalidOperationException("call Forms.Init() before this"); } var root = new Microsoft.UI.Xaml.Window(); // Yes, this looks awkward. But the page needs to be Platformed or several things won't work new WindowsPlatform(root); var renderer = visualElement.GetOrCreateRenderer(); if (renderer == null) { throw new InvalidOperationException($"Could not find or create a renderer for {visualElement}"); } var frameworkElement = renderer.ContainerElement; frameworkElement.Loaded += (sender, args) => { visualElement.Layout(new Rectangle(0, 0, frameworkElement.ActualWidth, frameworkElement.ActualHeight)); }; return(frameworkElement); }
Task SetupWindowForTests <THandler>(IWindow window, Func <Task> runTests, IMauiContext mauiContext = null) where THandler : class, IElementHandler { mauiContext ??= MauiContext; return(InvokeOnMainThreadAsync(async() => { var applicationContext = mauiContext.MakeApplicationScope(UI.Xaml.Application.Current); var appStub = new MauiAppNewWindowStub(window); UI.Xaml.Application.Current.SetApplicationHandler(appStub, applicationContext); WWindow newWindow = null; try { ApplicationExtensions.CreatePlatformWindow(UI.Xaml.Application.Current, appStub, new Handlers.OpenWindowRequest()); newWindow = window.Handler.PlatformView as WWindow; await runTests.Invoke(); } finally { window.Handler.DisconnectHandler(); await Task.Delay(10); newWindow?.Close(); appStub.Handler.DisconnectHandler(); } })); }
internal Platform(Microsoft.UI.Xaml.Window page) { if (page == null) { throw new ArgumentNullException(nameof(page)); } _page = page; var current = Microsoft.UI.Xaml.Application.Current; if (!current.Resources.ContainsKey("RootContainerStyle")) { Microsoft.UI.Xaml.Application.Current.Resources.MergedDictionaries.Add(Forms.GetTabletResources()); } if (!current.Resources.ContainsKey(ShellRenderer.ShellStyle)) { var myResourceDictionary = new Microsoft.UI.Xaml.ResourceDictionary(); myResourceDictionary.Source = new Uri("ms-appx:///Microsoft.Maui.Controls.Compatibility/WinUI/Shell/ShellStyles.xbf"); Microsoft.UI.Xaml.Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary); } _container = new Canvas { Style = (Microsoft.UI.Xaml.Style)current.Resources["RootContainerStyle"] }; _page.Content = _container; _container.SizeChanged += OnRendererSizeChanged; MessagingCenter.Subscribe(this, Page.BusySetSignalName, (Page sender, bool enabled) => { Microsoft.UI.Xaml.Controls.ProgressBar indicator = GetBusyIndicator(); indicator.Visibility = enabled ? Visibility.Visible : Visibility.Collapsed; }); _toolbarTracker.CollectionChanged += OnToolbarItemsChanged; UpdateBounds(); InitializeStatusBar(); if (!NativeVersion.IsDesktop) { SystemNavigationManager.GetForCurrentView().BackRequested += OnBackRequested; } // TODO WINUI: This event is only available on UWP // Microsoft.UI.Xaml.Application.Current.Resuming += OnResumingAsync; }
//internal static Platform Current //{ // get // { // var frame = UI.Xaml.Window.Current?.Content as Microsoft.UI.Xaml.Controls.Frame; // var wbp = frame?.Content as WindowsBasePage; // return wbp?.Platform; // } //} internal Platform(Microsoft.UI.Xaml.Window page) { if (page == null) { throw new ArgumentNullException(nameof(page)); } _page = page; var current = Microsoft.UI.Xaml.Application.Current; _container = new Canvas { Style = (Microsoft.UI.Xaml.Style)current.Resources["RootContainerStyle"] }; _page.Content = _container; _container.SizeChanged += OnRendererSizeChanged; MessagingCenter.Subscribe(this, Page.BusySetSignalName, (Page sender, bool enabled) => { Microsoft.UI.Xaml.Controls.ProgressBar indicator = GetBusyIndicator(); indicator.Visibility = enabled ? WVisibility.Visible : WVisibility.Collapsed; }); _toolbarTracker.CollectionChanged += OnToolbarItemsChanged; UpdateBounds(); InitializeStatusBar(); // https://docs.microsoft.com/en-us/windows/winui/api/microsoft.ui.xaml.window.current?view=winui-3.0 // The currently activated window for UWP apps. Null for Desktop apps. if (Microsoft.UI.Xaml.Window.Current != null) { SystemNavigationManager.GetForCurrentView().BackRequested += OnBackRequested; } // TODO WINUI: This event is only available on UWP // Microsoft.UI.Xaml.Application.Current.Resuming += OnResumingAsync; }
protected override void OnLaunched(LaunchActivatedEventArgs args) { var value = DependencyProperty.UnsetValue; var button = new Button { Content = "Click me to load MainPage", HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center }; button.Click += Button_Click; var window = new Microsoft.UI.Xaml.Window { Content = button }; window.Activate(); myWindow = window; }