0

I am looking at Windows Store apps samples and trying to understand how some things work.

I have this code in App.xaml

<local:Apoel x:Key="apoel"/>

and this line of code in my MainPage.xaml.cs

Apoel apoellin= (Apoel)App.Current.Resources["apoel"];

I tried searching around the web but I do not know what terms to use in order to get a perfect explanation of how this works.

What exactly are these two lines of code doing?

How would it work if the constructor of the Class Apoel needed an argument?

When is the object instantiated?

John Demetriou
  • 4,093
  • 6
  • 52
  • 88

1 Answers1

1

It's just an assignment. In your xaml the Apoel object named apoel is being defined and

Apoel apoellin= (Apoel)App.Current.Resources["apoel"];

is just a reference assignment. No new objects are being created.

About parametrized constructors check the answer here Calling a parameterized constructor from XAML

Community
  • 1
  • 1
Tarec
  • 3,268
  • 4
  • 30
  • 47
  • so in my App.Xaml code the object is being created and made as "global" resource for other pages to use using the other line of code? – John Demetriou Feb 25 '14 at 09:47
  • Essentially yes. `App.Xaml` is a definition of your app. It should contain a `` section, which is a dictionary of objects that are globally accessible. It's somehow similar to using `resx` files. – Tarec Feb 25 '14 at 09:57
  • I do not know resx files :D but let me tell you what I understand. It's like Creating a global variable that it's accessible from every possible location of my code using Apoel apoellin= (Apoel)App.Current.Resources["apoel"]; which simply tells my code to go get that variable from the global "pool" and cast it as type Apoel. Why is the casting needed though? – John Demetriou Feb 25 '14 at 10:03
  • 1
    It's correct. You need to cast it, because `Resources[Key]` returns overall `object` type, which is a base class for all others. The dictionary does that because it does not know at the design time what kind of objects it's holding - they're all resolved at runtime. – Tarec Feb 25 '14 at 10:18