Ejemplo n.º 1
0
        private void StartWebViewCoreIfPossible()
        {
            if (!RequiredStartupPropertiesSet ||
                _webviewManager != null)
            {
                return;
            }
            if (NativeView == null)
            {
                throw new InvalidOperationException($"Can't start {nameof(BlazorWebView)} without native web view instance.");
            }

            // We assume the host page is always in the root of the content directory, because it's
            // unclear there's any other use case. We can add more options later if so.
            var contentRootDir       = Path.GetDirectoryName(HostPage !) ?? string.Empty;
            var hostPageRelativePath = Path.GetRelativePath(contentRootDir, HostPage !);

            // On WinUI we override HandleWebResourceRequest in WinUIWebViewManager so that loading static assets is done entirely there in an async manner.
            // This allows the code to be async because in WinUI all the file storage APIs are async-only, but IFileProvider is sync-only and we need to control
            // the precedence of which files are loaded from where.
            var customFileProvider = VirtualView.CreateFileProvider(contentRootDir) ?? new NullFileProvider();

            _webviewManager = new WinUIWebViewManager(NativeView, new WinUIWebView2Wrapper(NativeView), Services !, ComponentsDispatcher, customFileProvider, VirtualView.JSComponents, hostPageRelativePath, contentRootDir);

            if (RootComponents != null)
            {
                foreach (var rootComponent in RootComponents)
                {
                    // Since the page isn't loaded yet, this will always complete synchronously
                    _ = rootComponent.AddToWebViewManagerAsync(_webviewManager);
                }
            }
            _webviewManager.Navigate("/");
        }
Ejemplo n.º 2
0
        private void StartWebViewCoreIfPossible()
        {
            if (!RequiredStartupPropertiesSet ||
                _webviewManager != null)
            {
                return;
            }
            if (PlatformView == null)
            {
                throw new InvalidOperationException($"Can't start {nameof(BlazorWebView)} without native web view instance.");
            }

            // We assume the host page is always in the root of the content directory, because it's
            // unclear there's any other use case. We can add more options later if so.
            var contentRootDir       = Path.GetDirectoryName(HostPage !) ?? string.Empty;
            var hostPageRelativePath = Path.GetRelativePath(contentRootDir, HostPage !);

            var fileProvider = VirtualView.CreateFileProvider(contentRootDir);

            _webviewManager = new IOSWebViewManager(this, PlatformView, Services !, ComponentsDispatcher, fileProvider, VirtualView.JSComponents, hostPageRelativePath);

            if (RootComponents != null)
            {
                foreach (var rootComponent in RootComponents)
                {
                    // Since the page isn't loaded yet, this will always complete synchronously
                    _ = rootComponent.AddToWebViewManagerAsync(_webviewManager);
                }
            }

            _webviewManager.Navigate("/");
        }