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

怎甘沉沦 提交于 2019-12-07 03:38:49

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/8783051/why-are-my-xaml-controls-not-showing-up-in-code-behind

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!