locked
An issue on accessing a control-item(e.g., a button) within xaml layout design from C#(e.g., the corresponding .cs file)

    Question

  • Hello, newbie here..

    I have an question for you.

    It's for Windows Phone/Store App development.

    I've learned that by setting the x:Name (e.g., <Button x:Name="hello" />) of an arbitrary control-item within a page,

    I can access that control-item from the corresponding page class definition within the .cs C# source file.

    But I also learned that a button has a property : Name, and I can achieve the same "effect" (I don't say "thing" because I wonder if it is not ! )  by setting the Name property of that button (e.g., <Button Name="hello" />).

    I need to ask : what the difference between them is ?

    The latter is relatively common to me, since it's just a property of the button control-item like any others, and that's what it's used for according to the spec online; but for the former? There is something uncommon.

    For example, I can set every control-item a different x:Name respectively, using the exactly same fashion as the latter.

    And there are many "x:yyy" things in an original basic page.

    So what does the 'x'-part do?  Why could it serve for ALL control-items? What does it stand for actually in that page (class)?

    And how could ALL the "x:Name" 's work as properly? What actually is a x:Name, or a "x:yyy" ?


    • Edited by CSIE902074 Saturday, October 4, 2014 6:29 PM
    Saturday, October 4, 2014 6:28 PM

Answers

  • When you look at the the top of a XAML file there are a lot of xmlns namespace declarations. One of those declarations is going to look like this:

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    This tells you where exactlly those properties have been declared. As to why both work - there is an additional mapping defined but I would suggest to use x:Name as it always works.

    If you want a more in-depth answer I suggest to have a look at StackOverflow here: http://stackoverflow.com/questions/589874/in-wpf-what-are-the-differences-between-the-xname-and-name-attributes

    That deals with WPF which is the framework Microsoft pioneered XAML in - the basic concept however is the same so the explanation there is true for Windows Store Apps as well.

    Sunday, October 5, 2014 7:06 PM