Baffa

Hi,

I have a UserControl that fires three events. The parent or consumer of this control drives only two of them. When I fire the event that was not drived the application crashes with the following error:

System.NullReferenceException was unhandled

Message: Object reference not set to an instance of an object.

The events are drived like this:

<userCtl:Header CalendarSelected="OnCalendarSelected" ContactsSelected="OnContactsSelected"/>

The other event that is not drived is ActivitiesSelected.

This is very common, I do not want to treat some events. What must I do to. Will I be forced to declare all events I do not think so, there must be a way to do this. Can you help me

Thansk in advance



Re: Windows Presentation Foundation (WPF) UserControls and Events

Drew Marsh

What's your event declaration look like Are these WPF routed events or just plain old .NET events I'm assuming they're plain old .NET events. If that's the case then I first recommend you change them to WPF routed events. However, if you choose to stick with them as plain old .NET events, you always need to check sure the multi-cast delegate instance is not null before attempting to invoke it. For example:

Code Snippet
public event EventHandler MyEvent;

...

// Before firing, make sure somebody's listening first!
if(this.MyEvent != null)
{
this.MyEvent(this, EventArgs.Empty);
}

HTH,
Drew





Re: Windows Presentation Foundation (WPF) UserControls and Events

Baffa

Hi Drew,

My UserControl is like this:

<Border Margin="1,1,1,0" DockPanel.Dock="Top" CornerRadius="5" BorderBrush="#FFC8C8C8" HorizontalAlignment="Stretch" BorderThickness="2" Background="White">

<StackPanel>

<StackPanel Margin="20,5,5,5" Orientation="Horizontal">

<MenuItem Header="Agenda" Style="{StaticResource FunctionToolbarLink}" Click="OnClick_Agenda"/>

<MenuItem Header="Contatos" Style="{StaticResource FunctionToolbarLink}" Click="OnClick_Contatos"/>

<MenuItem Header="Atividades" Style="{StaticResource FunctionToolbarLink}" Click="OnClick_Atividades"/>

<Label x:Name="lblMsg" />

</StackPanel>

</StackPanel>

</Border>

The code behind is below:

public partial class Header : System.Windows.Controls.UserControl

{

public Header()

{

InitializeComponent();

}

public event EventHandler<EventArgs> AgendaSelected;

public event EventHandler<EventArgs> ContatosSelected;

public event EventHandler<EventArgs> AtividadesSelected;

void OnClick_Agenda(object sender, RoutedEventArgs e) {

AgendaSelected(this, EventArgs.Empty);

}

void OnClick_Contatos(object sender, RoutedEventArgs e) {

ContatosSelected(this, EventArgs.Empty);

}

void OnClick_Atividades(object sender, RoutedEventArgs e) {

AtividadesSelected(this, EventArgs.Empty);

}

}

}

The cosumer is like this:

<userCtl:Header AgendaSelected="OnAgendaSelected" ContatosSelected="OnContatosSelected"/>

C#:

void OnAgendaSelected(object sender, EventArgs e) {

Pag1 pg = new Pag1();

frmContent.Content = pg;

}

void OnContatosSelected(object sender, EventArgs e) {

Pag2 pg = new Pag2();

frmContent.Content = pg;

}





Re: Windows Presentation Foundation (WPF) UserControls and Events

Drew Marsh

Yeah, ok, so you're using C#'s event keyword and CLR events. You need to check before firing each one of your events to make sure someone is listening like so (my additions in bold):

Code Snippet

void OnClick_Agenda(object sender, RoutedEventArgs e) {

if(this.AgendaSelected)
{
AgendaSelected(
this, EventArgs.Empty);
}

}

void OnClick_Contatos(object sender, RoutedEventArgs e) {

if(this.ContatosSelected)
{
ContatosSelected(
this, EventArgs.Empty);
}

}

void OnClick_Atividades(object sender, RoutedEventArgs e) {

if(this.AtividadesSelected)
{
AtividadesSelected(
this, EventArgs.Empty);
}

}

I would however also recommend turning them into WPF routed events so that they play a little nicer in the world of WPF. Here's a section in the SDK that explains routed events.

HTH,
Drew





Re: Windows Presentation Foundation (WPF) UserControls and Events

Baffa

Hi Drew

Thank you very much, it worked OK !!!!

If you don't mind I would like to ask you something about the publish function of VS 2005.

I am deploying my XBAP manually. I build the app and copy the bin directory to my public server. The reason I am doing that is that after I use VS 2005 to publish my app the project just "dies". That is, after I use the publish when I execute it again, in VS 2005, the project it just do nothing. That is the project seems to execute but the browser does not appear. I have to recreate my project include all the files so that I can execute it again.

VS 2005, sometimes, when I publish my application, crashes, but there are other times that the publish works OK. But no matter what happens the project, after that, "dies".

Can you help me !!!!

Thanks in advance





Re: Windows Presentation Foundation (WPF) UserControls and Events

Drew Marsh

You should definitely start a new post for this question. I don't know the answer and I'm not sure other people who might know the answer will find your question here.

Cheers,
Drew





Re: Windows Presentation Foundation (WPF) UserControls and Events

Baffa

Ok I will post another thread

Thank you very much