# Supporting Browser Zoom in Silverlight

ID: SL22

Technology: silverlight

Type: Technique

## When to Use

- Microsoft Silverlight, versions 3 and greater
- Silverlight managed programming model and Silverlight XAML

- Silverlight content in a user agent host that supports browser zoom

## Description

The objective of this technique is to support or anticipate existing browser zoom features effectively when users interact with the Silverlight application. This technique explains how the Silverlight content area reacts to the browser zoom feature implemented by some user agent hosts. Silverlight content and layout properties are based on physical screen pixel measurements. When the browser zoom is engaged, Silverlight content scales correctly for the zoom factor, and uses the same zoom factor as any surrounding HTML content.

Browser zoom is relevant for accessibility and Silverlight because it is likely to be the zoom /scaling feature enabled by the browser host that Web technology users are the most familiar with as a technique for increasing the text size in Web content.

#### Legacy behavior in Silverlight version 2

Built-in support for browser zoom was introduced as a feature in Silverlight version 3. Older documents on the Web might describe techniques that were relevant for Silverlight version 2, where dealing with browser zoom required JavaScript handling of the Resized event, and developers manually applied a `ScaleTransform` to Silverlight content to scale it up. Silverlight has a "quirks mode" that detects existing handlers that might still use the older techniques. Built-in zoom not active in these cases, so that applications can avoid doubling or otherwise mishandling the user agent's zooming behavior.

#### Deliberately disabling browser zoom in Silverlight applications

Silverlight also provides the ability to disable the built-in browser zoom handling and rendering behavior. This is sometimes done in order to suppress some of the aliasing and distortion artifacts that host-level scaling can introduce, particularly for video content or certain uses of text. In these cases, application authors might consider other Silverlight techniques for scaling the user interface, perhaps specifically only for the text elements on a page. When an application disables the built-in zoom behavior and rendering for Silverlight content, the browser still retains its zoom settings, and that setting applies to other content outside of Silverlight such as the hosting HTML.

## Examples

### Verifying browser zoom, and checking the zoom factor

This example has a UI defined in XAML and logic defined in C#. The UI shows the zoom factor, and also demonstrates what happens when built-in browser zoom handling is deliberately disabled. Note that the zooming factor as reported by the API is still retained even when Silverlight built-in zoom scaling is disabled deliberately. The following is the XAML UI:

    <UserControl x:Class="BrowserZoom.MainPage"
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    >
       <StackPanel x:Name="LayoutRoot" Background="White">
           <TextBlock>Some text you can zoom.</TextBlock>
           <Button Click="Button_Click">Toggle built-in zoom handling</Button>
           <StackPanel Orientation="Horizontal">
               <Button Click="Button_Click_1">Query current zoom factor</Button>
               <TextBox IsReadOnly="true" Name="zoomf"
       Text="{Binding Path=reportZoom}"/>
           </StackPanel>
       </StackPanel>
    </UserControl>

The following is the C# logic:

       public partial class MainPage : UserControl
       {
           public MainPage()
           {
               InitializeComponent();
           }
           private void Button_Click(object sender, RoutedEventArgs e)
           {
               if (!Application.Current.Host.Settings.EnableAutoZoom == false) {
               Application.Current.Host.Settings.EnableAutoZoom = false;
               }
               else
               {
                   Application.Current.Host.Settings.EnableAutoZoom = true;
               }
           }
           private void Button_Click_1(object sender, RoutedEventArgs e)
           {
               zoomf.Text = Application.Current.Host.Content.ZoomFactor.ToString();
           }
       }

This example is shown in operation in the working example of Browser Zoom.

## Tests

### Procedure

1. Using a browser that supports Silverlight, open an HTML page that references a Silverlight application through an object tag. The browser being used for the test must support a browser zoom feature, and the feature must be turned on as a user preference.
2. Verify that the Silverlight application enables auto zoom (no Silverlight application-specific code or markup has set EnableAutoZoom API to false).
3. Test the zooming feature of the user agent. Verify that browser zoom factors apply to the Silverlight content.

### Expected Results

#2 and #3 are true.

## Related Techniques

- G142
- G179

## Resources

- EnableAutoZoom Property
- Silverlight Accessibility Overview