GetName() fails in Windows Phone 7
July 12, 2011 Leave a comment
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:
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.