static UnoXamlHostBase() { //TODO: These lines should be set in a different location, possibly in a more general way (for multi-window support) https://github.com/unoplatform/uno/issues/8978 Windows.UI.Core.CoreDispatcher.DispatchOverride = d => global::System.Windows.Application.Current.Dispatcher.BeginInvoke(d); Windows.UI.Core.CoreDispatcher.HasThreadAccessOverride = global::System.Windows.Application.Current.Dispatcher.CheckAccess; if (MetadataProviderDiscovery.MetadataProviderFactory is null) { MetadataProviderDiscovery.MetadataProviderFactory = type => { if (typeof(WUX.Application).IsAssignableFrom(type)) { WUX.Application application = null; WUX.Application.Start(_ => { application = (WUX.Application)Activator.CreateInstance(type); }); return((WUX.Markup.IXamlMetadataProvider)application); } return(null); }; } _metadataContainer = XamlApplicationExtensions.GetOrCreateXamlMetadataContainer(); }
/// <summary> /// Gets and returns the current UWP XAML Application instance in a reference parameter. /// If the current XAML Application instance has not been created for the process (is null), /// a new <see cref="Microsoft.Toolkit.Win32.UI.XamlHost.XamlApplication" /> instance is created and returned. /// </summary> /// <returns>The instance of <seealso cref="XamlApplication"/></returns> public static IXamlMetadataContainer GetOrCreateXamlMetadataContainer() { // 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 (_metadataContainer == null) { // Create a custom UWP XAML Application object that implements reflection-based XAML metadata probing. try { var app = new XamlApplication(); app.MetadataProviders.AddRange(MetadataProviderDiscovery.DiscoverMetadataProviders()); return(app); } catch { _metadataContainer = WUX.Application.Current as IXamlMetadataContainer; } } var xamlApplication = _metadataContainer as XamlApplication; if (xamlApplication != null && xamlApplication.IsDisposed) { throw new ObjectDisposedException(typeof(XamlApplication).FullName); } return(_metadataContainer); }
/// <summary> /// Gets and returns the current UWP XAML Application instance in a reference parameter. /// If the current XAML Application instance has not been created for the process (is null), /// a new <see cref="Uno.UI.XamlHost.XamlApplication" /> instance is created and returned. /// </summary> /// <returns>The instance of <seealso cref="XamlApplication"/></returns> public static IXamlMetadataContainer GetOrCreateXamlMetadataContainer() { // 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 (_metadataContainer == null && !_initialized) { _initialized = true; // Create a custom UWP XAML Application object that implements reflection-based XAML metadata probing. try { _metadataContainer = GetCurrentProvider(); if (_metadataContainer == null) { var providers = MetadataProviderDiscovery.DiscoverMetadataProviders().ToList(); _metadataContainer = GetCurrentProvider(); if (_metadataContainer == null) { _metadataContainer = new XamlApplication(providers); return(_metadataContainer); } } else { return(_metadataContainer); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.ToString()); _metadataContainer = GetCurrentProvider(); } } var xamlApplication = _metadataContainer as XamlApplication; if (xamlApplication != null && xamlApplication.IsDisposed) { throw new ObjectDisposedException(typeof(XamlApplication).FullName); } return(_metadataContainer); }
static WindowsXamlHostBase() { _metadataContainer = XamlApplicationExtensions.GetOrCreateXamlMetadataContainer(); }