/// <summary> /// Initializes a new instance of the <see cref="WindowsXamlHostBase"/> class. /// </summary> public WindowsXamlHostBase() { SetStyle(ControlStyles.ContainerControl, true); SetStyle(ControlStyles.SupportsTransparentBackColor, true); SetStyle(ControlStyles.UserPaint, true); SetStyle(ControlStyles.AllPaintingInWmPaint, true); // Must be a container control with TabStop == false to allow nested UWP XAML Focus // BUGBUG: Uncomment when nested Focus is available // TabStop = false; // Respond to size changes on this Control SizeChanged += OnWindowXamlHostSizeChanged; // Windows.UI.Xaml.Application object is required for loading custom control metadata. If a custom // Application object is not provided by the application, the host control will create one (XamlApplication). // Instantiation of the application object must occur before creating the DesktopWindowXamlSource instance. // If no Application object is created before DesktopWindowXamlSource is created, DestkopWindowXamlSource // will create a generic Application object unable to load custom UWP XAML metadata. Microsoft.Toolkit.Win32.UI.XamlHost.XamlApplication.GetOrCreateXamlApplicationInstance(ref _application); // Create an instance of the WindowsXamlManager. This initializes and holds a // reference on the UWP XAML DXamlCore and must be explicitly created before // any UWP XAML types are programmatically created. If WindowsXamlManager has // not been created before creating DesktopWindowXamlSource, DesktopWindowXaml source // will create an instance of WindowsXamlManager internally. (Creation is explicit // here to illustrate how to initialize UWP XAML before initializing the DesktopWindowXamlSource.) _windowsXamlManager = Windows.UI.Xaml.Hosting.WindowsXamlManager.InitializeForCurrentThread(); // Create DesktopWindowXamlSource, host for UWP XAML content _xamlSource = new Windows.UI.Xaml.Hosting.DesktopWindowXamlSource(); // Hook up method for DesktopWindowXamlSource Focus handling _xamlSource.TakeFocusRequested += this.OnTakeFocusRequested; }
/// <summary> /// Initializes a new instance of the <see cref="WindowsXamlHostBase"/> class. /// Default constructor is required for use in WPF markup. /// (When the default constructor is called, object properties have not been set. Put WPF logic in OnInitialized.) /// </summary> public WindowsXamlHostBase() { // Create a custom UWP XAML Application object that implements reflection-based XAML metdata probing. // Instantiation of the application object must occur before creating the DesktopWindowXamlSource instance. // DesktopWindowXamlSource will create a generic Application object unable to load custom UWP XAML metadata. if (_application == null) { try { // global::Windows.UI.Xaml.Application.Current may throw if DXamlCore has not been initialized. // Treat the exception as an uninitialized global::Windows.UI.Xaml.Application condition. _application = Windows.UI.Xaml.Application.Current as XamlApplication; } catch { _application = new XamlApplication(); } } // Create an instance of the WindowsXamlManager. This initializes and holds a // reference on the UWP XAML DXamlCore and must be explicitly created before // any UWP XAML types are programmatically created. If WindowsXamlManager has // not been created before creating DesktopWindowXamlSource, DesktopWindowXaml source // will create an instance of WindowsXamlManager internally. (Creation is explicit // here to illustrate how to initialize UWP XAML before initializing the DesktopWindowXamlSource.) _windowsXamlManager = Windows.UI.Xaml.Hosting.WindowsXamlManager.InitializeForCurrentThread(); // Create DesktopWindowXamlSource, host for UWP XAML content desktopWindowXamlSource = new Windows.UI.Xaml.Hosting.DesktopWindowXamlSource(); // Hook OnTakeFocus event for Focus processing desktopWindowXamlSource.TakeFocusRequested += OnTakeFocusRequested; }
/// <summary> /// Initializes a new instance of the <see cref="WindowsXamlHostBase"/> class. /// </summary> /// <remarks> /// Default constructor is required for use in WPF markup. When the default constructor is called, /// object properties have not been set. Put WPF logic in OnInitialized. /// </remarks> public WindowsXamlHostBase() { // Windows.UI.Xaml.Application object is required for loading custom control metadata. If a custom // Application object is not provided by the application, the host control will create one (XamlApplication). // Instantiation of the application object must occur before creating the DesktopWindowXamlSource instance. // If no Application object is created before DesktopWindowXamlSource is created, DestkopWindowXamlSource // will create a generic Application object unable to load custom UWP XAML metadata. Action <XamlApplication> action = Microsoft.Toolkit.Win32.UI.XamlHost.XamlApplication.GetOrCreateXamlApplicationInstance(ref _application); // Create an instance of the WindowsXamlManager. This initializes and holds a // reference on the UWP XAML DXamlCore and must be explicitly created before // any UWP XAML types are programmatically created. If WindowsXamlManager has // not been created before creating DesktopWindowXamlSource, DesktopWindowXaml source // will create an instance of WindowsXamlManager internally. (Creation is explicit // here to illustrate how to initialize UWP XAML before initializing the DesktopWindowXamlSource.) _windowsXamlManager = windows.UI.Xaml.Hosting.WindowsXamlManager.InitializeForCurrentThread(); action(_application as XamlApplication); // Create DesktopWindowXamlSource, host for UWP XAML content _xamlSource = new windows.UI.Xaml.Hosting.DesktopWindowXamlSource(); // Hook DesktopWindowXamlSource OnTakeFocus event for Focus processing _xamlSource.TakeFocusRequested += OnTakeFocusRequested; }
/// <summary> /// Initializes a new instance of the <see cref="WindowsXamlHostBase"/> class. /// </summary> public WindowsXamlHostBase() { // Return immediately if control is instantiated by the Visual Studio Designer // https://stackoverflow.com/questions/1166226/detecting-design-mode-from-a-controls-constructor if (LicenseManager.UsageMode == LicenseUsageMode.Designtime) { return; } SetStyle(ControlStyles.ContainerControl, true); SetStyle(ControlStyles.SupportsTransparentBackColor, true); SetStyle(ControlStyles.UserPaint, true); SetStyle(ControlStyles.AllPaintingInWmPaint, true); // Must be a container control with TabStop == false to allow nested UWP XAML Focus // BUGBUG: Uncomment when nested Focus is available // TabStop = false; // Respond to size changes on this Control SizeChanged += OnWindowXamlHostSizeChanged; // Windows.UI.Xaml.Application object is required for loading custom control metadata. If a custom // Application object is not provided by the application, the host control will create one (XamlApplication). // Instantiation of the application object must occur before creating the DesktopWindowXamlSource instance. // If no Application object is created before DesktopWindowXamlSource is created, DestkopWindowXamlSource // will create a generic Application object unable to load custom UWP XAML metadata. Microsoft.Toolkit.Win32.UI.XamlHost.XamlApplication.GetOrCreateXamlApplicationInstance(ref _application); // Create an instance of the WindowsXamlManager. This initializes and holds a // reference on the UWP XAML DXamlCore and must be explicitly created before // any UWP XAML types are programmatically created. If WindowsXamlManager has // not been created before creating DesktopWindowXamlSource, DesktopWindowXaml source // will create an instance of WindowsXamlManager internally. (Creation is explicit // here to illustrate how to initialize UWP XAML before initializing the DesktopWindowXamlSource.) _windowsXamlManager = Windows.UI.Xaml.Hosting.WindowsXamlManager.InitializeForCurrentThread(); // Create DesktopWindowXamlSource, host for UWP XAML content _xamlSource = new Windows.UI.Xaml.Hosting.DesktopWindowXamlSource(); // Hook up method for DesktopWindowXamlSource Focus handling _xamlSource.TakeFocusRequested += this.OnTakeFocusRequested; // Check if the XAML island scales the content according to the current dpi value _xamlIslandHandlesDpiScaling = ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 8); // Add scaling panel as the root XAML element _xamlSource.Content = new DpiScalingPanel(); }
public WindowsXamlHostBase() { SetStyle(ControlStyles.SupportsTransparentBackColor, true); SetStyle(ControlStyles.UserPaint, true); SetStyle(ControlStyles.AllPaintingInWmPaint, true); // Respond to size changes on this Control SizeChanged += WindowsXamlHost_SizeChanged; // Create a custom UWP XAML Application object that implements reflection-based XAML metdata probing. // Instantiation of the application object must occur before creating the DesktopWindowXamlSource instance. // DesktopWindowXamlSource will create a generic Application object unable to load custom UWP XAML metadata. if (_application == null) { try { // global::Windows.UI.Xaml.Application.Current may throw if DXamlCore has not been initialized. // Treat the exception as an uninitialized global::Windows.UI.Xaml.Application condition. _application = Windows.UI.Xaml.Application.Current as XamlApplication; } catch { _application = new XamlApplication(); } } // Create an instance of the WindowsXamlManager. This initializes and holds a // reference on the UWP XAML DXamlCore and must be explicitly created before // any UWP XAML types are programmatically created. If WindowsXamlManager has // not been created before creating DesktopWindowXamlSource, DesktopWindowXaml source // will create an instance of WindowsXamlManager internally. (Creation is explicit // here to illustrate how to initialize UWP XAML before initializing the DesktopWindowXamlSource.) _windowsXamlManager = Windows.UI.Xaml.Hosting.WindowsXamlManager.InitializeForCurrentThread(); // Create DesktopWindowXamlSource, host for UWP XAML content desktopWindowXamlSource = new Windows.UI.Xaml.Hosting.DesktopWindowXamlSource(); }