0

I am using silverlight, My code is set up for a usercontrol as follows:

myxaml.xaml (Just showing the toggle button [line 119])

<ToggleButton x:Name="btnToggleResizeMap" Checked="btnToggleResizeMap_Checked" Unchecked="btnToggleResizeMap_Unchecked" IsChecked="True"/>

codebehind.cs

public partial class MapRadar : UserControl
{

    public delegate void OnMapExpandChange(object sender, EventArgs e);
    public event OnMapExpandChange Expanded;
    public event OnMapExpandChange NotExpanded;

    private void btnToggleResizeMap_Checked(object sender, System.Windows.RoutedEventArgs e)
    {
        NotExpanded(this, null); //If i remove this line, the app runs fine
    }

    private void btnToggleResizeMap_Unchecked(object sender, System.Windows.RoutedEventArgs e)
    {
        Expanded(this, null); //If i remove this line, the app runs fine
    }
}

Visual studio throws this error before the application is completely loaded:

AG_E_PARSER_BAD_PROPERTY_VALUE [Line: 119 Position: 285]

at:

System.Windows.Application.LoadComponent(this, new System.Uri("/Xormis.Silverlight.ExSys;component/Views/Map/MapRadar.xaml", System.UriKind.Relative));

which is located inside a function named public void InitializeComponent()

I have no idea what is happening here, is there something against having event calls inside another event?

Shawn Mclean
  • 56,733
  • 95
  • 279
  • 406

1 Answers1

2

The problem is that you have null events. As soon as the checkbox is created, it immediately raises the Unchecked event, which calls your btnToggleResizeMap_Unchecked handler, which tries to call your Expanded event. Since Expanded is null, an exception is thrown, and it never finishes running the XAML.

Your code should look like this:

private void btnToggleResizeMap_Checked(object sender, System.Windows.RoutedEventArgs e) 
{
    if (NotExpanded != null) 
        NotExpanded(this, null);
} 

private void btnToggleResizeMap_Unchecked(object sender, System.Windows.RoutedEventArgs e) 
{
    if (Expanded != null)
        Expanded(this, null);
} 

For a more thorough description of events, see C# Events and Thread Safety

Community
  • 1
  • 1
Gabe
  • 84,912
  • 12
  • 139
  • 238