Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are my XAML controls not showing up in code-behind?

Tags:

c#

windows-8

xaml

I booted into the Windows Developer Preview today, hoping to get some code out and into a functioning Metro application. I'm excited for the launch of the Windows Store, and I wanted to be a part of it. So I fire up VS11 Express, and start coding. I barely even get out one line when:

"The name 'View' does not exist in the current context"

So I go back to my XAML page. I dig around, and lo and behold, my button named View is still there, with the following code:

<Button Name="View" Content="View the help page" HorizontalAlignment="Left" Height="142" Margin="765,275,0,0" VerticalAlignment="Top" Width="456" FontSize="48" Foreground="Black" Background="Orange" BorderBrush="Black"/>

I saw nothing wrong with that XAML. However, my only experience with C# is through Windows Phone 7. So I do a bit of research. And I see nothing wrong with my code.

What the heck is wrong here? This worked with desktop Windows AND WP7!

How can I prevent this from happening, and why is it happening?

EDIT

I just returned to my project and realized that I had forgotten to save. I then pressed the traditional Ctrl-Shift-S to save everything. I went back to the tab of MainPage.xaml.cs and lo and behold, it works! No error! Why did this happen?

like image 936
JavaAndCSharp Avatar asked Jan 09 '12 01:01

JavaAndCSharp


1 Answers

Try using x:Name instead of vanilla Name. The use of Name creates the potential for a conflict while x:Name ensures it will name the identifier the specified value

There is a more detailed explanation available on MSDN. See the "Named Elements" section

  • http://msdn.microsoft.com/en-us/library/ms752059.aspx
like image 71
JaredPar Avatar answered Sep 27 '22 19:09

JaredPar