开发者

Error with calling event inside another event

开发者 https://www.devze.com 2022-12-21 06:28 出处:网络
I am using silverlight, My code is set up for a usercontrol as follows: myxaml.xaml (Just showing the toggle button [line 119])

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.xa开发者_如何学JAVAml", 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?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消