Himberjack
Himberjack

Reputation: 5792

Silverlight control InitializeComponent freezes application

I have a control that I create hundreds of times during the application. I have noticed that my app freezes because in the Initializecomponent function, there is System.Windows.Application.LoadComponent(this, new System.Uri("/fa;component/Controls/Common/Popup/PopupItem.xaml", System.UriKind.Relative));

if I comment this out, the application runs smoothly (of course without the control rendered).

How can I avoid/increase performance so the XAML won't be loaded each time, but somehow to recycle the control??

for (int i = 5; i < colValues.Count; i++)
            {
                if (colValues[i].Count == "1")
                    continue;

                PopupItem pi = new PopupItem(colValues[i], false, this, FilterCategorySearch.PopupContent);
                FilterCategorySearch.PopupContent.spItemsContainer.Children.Add(pi);
            }

and the XAML is

<UserControl x:Class="FacetedSearch.Controls.Common.Popup.PopupItem"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:FacetedSearch.Controls.Common"
mc:Ignorable="d">
<UserControl.Resources>
    <SolidColorBrush x:Key="TextNormalBrush" Color="#FF656565"/>
    <SolidColorBrush x:Key="TextHoverBrush" Color="#FFA39F9F"/>
</UserControl.Resources>

<StackPanel MouseEnter="LayoutRoot_MouseEnter" MouseLeave="LayoutRoot_MouseLeave" Orientation="Horizontal" Margin="0,4,0,0">
    <local:CTLCheckBox x:Name="cbFilter" MouseLeftButtonUp="cbFilter_MouseLeftButtonUp" Cursor="Hand" Height="14" Width="10" Margin="4,0" />
    <TextBlock x:Name="tbFilterName"  Foreground="{StaticResource TextNormalBrush}" MouseLeftButtonUp="tbFilterName_MouseLeftButtonUp" TextWrapping="Wrap" FontFamily="Arial" Margin="0,0,4,0" Cursor="Hand"/>
    <TextBlock x:Name="tbFilterCount" TextWrapping="Wrap"  Foreground="{StaticResource TextNormalBrush}" FontFamily="Arial"/>
</StackPanel>

thanks

Upvotes: 1

Views: 240

Answers (1)

marcslogic
marcslogic

Reputation: 882

The xaml for UserControls is parsed by Silverlight for every new instance of the UserControl. This means that if you add 100 instances of the same UserControl, the xaml will be read, parsed, instantiated as objects then visual objects 100 times. You have 2 possibilities:

  1. Access your UserControl from another location by referencing it from within a DataTemplate (used by means of, say, a ContentControl)
  2. Rewrite your UserControl to be a "real" control (i.e. a sublass of Control or ContentControl)

Upvotes: 2

Related Questions