Skip to content

WebView ZoomFactor Resets on each Navigation #3459

Open
@RickStrahl

Description

@RickStrahl

The ZoomFactor property is getting reset every time the WebView instance is navigated when it should maintain the ZoomFactor across navigations. We are after all looking at the same instance.

Observed:
Any time you create a new WebView instance and change the source or refresh the ZoomFactor is reset to its initial value. If you set the value during Initialization to 2 it resets to 2. Default is 1 and it so it usually resets to 1.

Additionally the ZoomFactorChanged event is fired whenever the page is refreshed and receives the now reset value instead of the current active ZoomFactor the user may have changed to during using the active Web page.

Steps:

  • create a WebView
  • Open a page in the WebView
  • Change the Zoom level with Ctrl-Mousewheel or Ctrl+/-
  • Refresh the page

Observed:

Changing zoom level works, but is not persisted across the refresh operation.

Expected:

Since you are setting the ZoomFactor on the WebBrowser instance I would expect the ZoomFactor to stay at what the user sets it to unless it's explicitly overridden at the code level.

Further the fact that ZoomFactorChanged is fired when the value is reset makes it very difficult to maintain this value explicitly. It would be useful to be able to distinguish between:

  • Initial Assignment
  • Reset on Navigation
  • Changed by user / in document

Without this you need to use flag values to disable level capture in NavigationStarting and enable in DomContentLoaded which is a PITA for something that seems like it should be automatic since you set the value at the WebBrowser instance.

FWIW full browsers behave as follows:

  • Refresh: Zoom level is preserved
  • Load a new page: Zoom level is reset to default (in Edge)
  • Go back to original page: Zoom level is back to now zoomed level (ie. change is remembered)

Workaround

For now the workaround I use requires a bunch of extra code:

        /// <summary>
        /// If true, tracks the Zoom factor of this browser instance
        /// over refreshes. By default the WebView looses any user
        /// zooming via Ctrl+ and Ctrl- or Ctrl-Mousewheel whenever
        /// the content is reloaded. This settings explicitly keeps
        /// track of the ZoomFactor and overrides it.
        /// </summary>
        public bool TrackZoomFactor { get; set; }
        
        public double ZoomLevel { get; set;  }= 1;
        private bool CaptureZoom = false;
        
        /// <summary>
        /// Handle keeping the Zoom Factor the same for all preview instances
        /// Without this 
        /// </summary>
        private void CaptureZoomFactor()
        {
            if (WebBrowser.CoreWebView2 == null)
            {
                throw new InvalidOperationException("Please call CaptureZoomFactor after WebView has been initialized");
            }
            
            if (!TrackZoomFactor)
                return;
            
            WebBrowser.ZoomFactorChanged += (s, e) =>
            {
                if (!CaptureZoom)
                {
                    WebBrowser.ZoomFactor = ZoomLevel;
                }
                else
                {
                    ZoomLevel = WebBrowser.ZoomFactor;
                }
            };
            WebBrowser.NavigationStarting += (object sender, CoreWebView2NavigationStartingEventArgs e) => CaptureZoom = false;
            WebBrowser.CoreWebView2.DOMContentLoaded += (s, e) => CaptureZoom = true;
        }

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions