Metro Nuggets

Bitesized tidbits for building Modern (Metro) apps.

Running the Dispatcher From the Settings Charm

In my previous post, I talked about how to receive messages from a UDP broadcast, and for the Windows 8 app I’m writing, I wanted the results of this to be shown in the settings charm. But here I ran into a problem, the Dispatcher kept failing with an NullReferenceException on Window.Current and so the results of my broadcast never got shown in the settings page. I asked on twitter, but got no response, so I’ve had to resort to what feels like a bit of a dirty workaround, but there you go.

The Solution

As I said, this feels like a bit of a dirty workaround, so apologies when you look at it and fully agree with me Smile

App.xaml.cs:

        public static Window ThisWindow { get; private set; }

        public App()
        {
            this.InitializeComponent();
            this.Suspending += OnSuspending;

            ThisWindow = Window.Current;
        }

SettingsViewModel.cs:

App.ThisWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => /* Your code here */);

And that’s it really.

SL

Advertisements

One response to “Running the Dispatcher From the Settings Charm

  1. Pingback: Running the Dispatcher From the Settings Charm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: