0

When I call ShowRevertInventorySignDialogView() through COMMAND, event throws ObjectNullException. I'm thinking that Object is not dispose as its assined to CurrentViewModel property.

Both GetReport() and ShowRevertInventorySignDialogView() functions called by Command which are binded to Buttons. I'm pretty sure that GetReport() function is called first so that object is created before raising the event.

What am I missing here?

class MainWindowViewModel : ViewModel{

    public ViewModel CurrentViewModel
    {
        get { return currentViewModel; }
        set { currentViewModel = value; NotifyPropertyChanged(); }
    }

    public void GetReport()
    {
        inventoryReportViewModel = new InventoryReportViewModel();
        inventoryReportViewModel.OnStatusChange += Event_OnStatusChange;
        CurrentViewModel = inventoryReportViewModel;          
    }
}

public class InventoryReportViewModel : InventoryBaseViewModel
{
    public event EventHandler<StatusChangeEventArgs> OnStatusChange;

    private void ShowRevertInventorySignDialogView()
    {
        OnStatusChange(this, new StatusChangeEventArgs("test",10));
        ....
        ....
    }
}

XAML;

<ContentControl Content="{Binding Path=CurrentViewModel}" Margin="20 10 20 0"></ContentControl>
onurfoca
  • 103
  • 7

2 Answers2

0

Check if there are any subscribers before raising the event:

private void ShowRevertInventorySignDialogView()
{
    if (OnStatusChange != null)
        OnStatusChange(this, new StatusChangeEventArgs("test", 10));

    //or simply: OnStatusChange?.Invoke(this, new StatusChangeEventArgs("test", 10));
}

Then you should atleast get rid of the exception.

Since you haven't showed us how and where the GetReport() method is called, or any other details, it is impossible to say why there are no subscribers by the time the ShowRevertInventorySignDialogView method gets called.

mm8
  • 163,881
  • 10
  • 57
  • 88
  • both methods called by commands which are binded to buttons. I know that I need to check for any subscribers, I have deleted for testing purposes. – onurfoca Aug 14 '17 at 16:42
0

I figured out that problem occurs because of the explicit definition of the viewModel in XAML. I remove the lines below and now I can get the right object as viewModel. (I'm defining VM it in DataTemplate)

<UserControl.DataContext>
    <vm:InventoryReportViewModel />
</UserControl.DataContext>
onurfoca
  • 103
  • 7