GetName() fails in Windows Phone 7

I am finishing up my 1st real windows phone application and I am creating an “About” page. Instead of hard-coding in the version number, I thought I would take a trick form my old WinForm days and use System.Reflection

I whipped up some code like this:

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) { this.VersionTextBlock.Text = String.Format("Version {0}", Assembly.GetExecutingAssembly(). GetName().Version.ToString()); base.OnNavigatedTo(e); }

I then ran it and got the following error:

image

Ugh. I was going to figure out what was wrong but then I deferred to the example in Chapter 6 in Adam Nathen’s 101 Windows Phone Apps . I changed the code to this:

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) { string fullAssemblyName = typeof(AboutPage).Assembly.ToString(); int firstComma = fullAssemblyName.IndexOf("Version="); int secondComma = fullAssemblyName.IndexOf(",", firstComma); string versionNumber = fullAssemblyName.Substring(firstComma, secondComma - firstComma); this.VersionTextBlock.Text = versionNumber; base.OnNavigatedTo(e); }

That worked fine.

Advertisements

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: