Are Labels being re-drawn unnecessarily, or is there some relationship I haven't spotted?

JohnHardmanJohnHardman GBUniversity mod

Whilst re-factoring some code, I enabled EnableRedrawRegions in the App.xaml.cs for my app's WinPhone 8 project, using the line:

Application.Current.Host.Settings.EnableRedrawRegions = true;

As I exercised different parts of my app, I noticed that some Labels were being redrawn unnecessarily. On some reasonably complex pages, the Labels being redrawn had no obvious connection to the parts of the page that were intentionally being updated. I have put together a much cut down version of one page, removing the vast majority of Views and code. The code is below.

In this sample, when the Slider is moved, I would expect just one Label to be redrawn, but actually three Labels are being redrawn on my Nokia phone. Can anybody tell me why please? If the cause in this sample can be identified, I may be able to address unnecessary redrawing in more complex pages.

Many thanks,

John H.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Xamarin.Forms;

namespace App1
{
    public class App : Application
    {
        private Label _labelToChange;
        private Slider _sliderThatWeMove;

        private void OnSliderChanged(object sender, ValueChangedEventArgs e)
        {
            int newValue = (int)(e.NewValue + 0.5);
            String desiredValue = newValue.ToString();
            if (desiredValue != _labelToChange.Text)
                _labelToChange.Text = desiredValue;
        }

        public App()
        {
            _labelToChange = new Label
            {
                Text = "44",
                HorizontalOptions = LayoutOptions.Start,
                VerticalOptions = LayoutOptions.End
            };

            _sliderThatWeMove = new Slider
            {
                Maximum = 255,
                Minimum = 0,
                Value = 44,

                HorizontalOptions = LayoutOptions.Fill, // AndExpand,
                VerticalOptions = LayoutOptions.Start
            };

            _sliderThatWeMove.ValueChanged += OnSliderChanged;

            MainPage = new ContentPage
            {
                Title="ObfuscatedString1",

                Content = new StackLayout
                {
                    Orientation = StackOrientation.Vertical,

                    HorizontalOptions = LayoutOptions.Fill,
                    VerticalOptions = LayoutOptions.Fill,

                    Children = // of StackLayout
                    {
                        new Label
                        {
                            Text="Why redrawn #1 ?",

                            HorizontalOptions = LayoutOptions.Start,
                            VerticalOptions = LayoutOptions.Start

                        },
                        new StackLayout
                        {
                            VerticalOptions = LayoutOptions.CenterAndExpand,
                        },
                        new StackLayout
                        {
                            Orientation = StackOrientation.Vertical,

                            HorizontalOptions = LayoutOptions.Fill,
                            VerticalOptions = LayoutOptions.End,

                            Children = // of StackLayout
                            {
                                new Label
                                {
                                    Text="Why redrawn #2 ?",
                                    HorizontalOptions = LayoutOptions.Start,
                                    VerticalOptions = LayoutOptions.Start

                                },
                                _labelToChange,
                                _sliderThatWeMove

                            } // Children of StackLayout
                        } // StackLayout
                    } // Children of StackLayout
                } // StackLayout
            }; // ContentPage
        } // constructor
    } // class
} // namespace
Sign In or Register to comment.