0

My WP8 app displays a message in the system tray for a few seconds when navigated to. But the following method I use to display the message always throws an System.NullReferenceException: Object reference not set to an instance of an object. I'm baffled to why this is, maybe someone can point out maybe whats wrong? The code is:

    private void runSystrayMessage(bool isVisible, string text, int length)
    {
        try
        {
            SystemTray.ProgressIndicator.IsVisible = true;
            SystemTray.ProgressIndicator.IsIndeterminate = isVisible;
            SystemTray.ProgressIndicator.Text = text;
        }
        catch (Exception e)
        {
            Debug.WriteLine("Exception caught" + e);
        }


        DispatcherTimer timer = new DispatcherTimer();
        try
        {
            timer.Interval = TimeSpan.FromMilliseconds(length);
        }
        catch(ArgumentOutOfRangeException e)
        {
            Debug.WriteLine("ArgumentOutOfrangeException caught" + e);
        }

        timer.Tick += (sender, args) =>
        {
            try
            {
                SystemTray.ProgressIndicator.IsVisible = false;
            }
            catch(System.InvalidOperationException e)
            {
                Debug.WriteLine("InvalidOperationException caught" + e);
            }
            timer.Stop();
        };
        timer.Start();
    }

The full Exception message is:

System.NullReferenceException: Object reference not set to an instance of an object.

at ContosoSocial.StartPage.<>c__DisplayClass1.b__0(Object sender, EventArgs args)

at MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)

at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)

Matthew Haugen
  • 11,855
  • 5
  • 34
  • 52
TripVoltage
  • 241
  • 4
  • 15

1 Answers1

4

Your problem is you have not created a new instance of the ProgressIndicator in your app. You have to instantiate one. Do it in the OnNavigatedTo method.

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);
    SystemTray.ProgressIndicator = new ProgressIndicator();
}

That should fix your problem.

Kasun Kodagoda
  • 3,902
  • 5
  • 27
  • 54