Q's about Windows Phone 8 App dev; Working through the Bob's, Clint's and Larry's tutorial

Stamper_wc

New member
Feb 21, 2015
4
0
0
Visit site
Hi everyone!!

I just decided yesterday that I want to add some functionality to my new and first Windows Phone myself(got a Lumia 830, couldn't be happier!). Coming from a C and Python background, I am working/reading through the "Absolute Beginner Series For Windows Phone 8" to get a clear understanding of all the cool features which we can manipulate in Windows Phone. (Tutorial can be found at channel9 msdn , unfortunately I cannot write URL's yet) The reason I am writing here, is because I have some questions and I might get some more questions later on which I will also add in this first post. I am hoping somebody might be willing to (partially) answer them. It might be some of the questions are actually handled later on in the material so don't be afraid to tell me just that if you are familier with the material.

Thanks in advance!

Questions:

1. After reading section 3 on XAML, I was thinking if it might be possible to inherit one of the standard classes (like Textbox or Button) and add some common functionality that a lot of instance might share and then use th?t element in the XAML code. In other words, is it possible to add your own elements based on existing elements in XAML?

2. After thinking a little bit about how XAML works and what kind of functionality I would like to add in apps in the future, is it possible to "update" XAML code just like javascript updates HTML or is another mechanism usually used here? An example where you might use this: If a user clicks on a new Button and a new textbox must be inserted in the current page, you can of course instantiate the new Textbox in your C# code and add it as a children to the container. But XAML is used to create such content much more easily so how would you got about adding XAML code dynamically while also knowing that XAML code is actually compiled to C#? Is there like a function which you can use to inject XAML code from a C# script?



Again guys and girls, thanks very much for reading/answering any of my questions!
 

a5cent

New member
Nov 3, 2011
6,622
0
0
Visit site
After reading section 3 on XAML, I was thinking if it might be possible to inherit one of the standard classes (like Textbox or Button) and add some common functionality that a lot of instance might share and then use th?t element in the XAML code. In other words, is it possible to add your own elements based on existing elements in XAML?
Yes, that is possible. Consider however that in most cases you're likely better off using a UserControl in that scenario. Inheriting from existing Controls is what you do when you're primary aim is to change how those Controls behave. If your goal is code reuse, then inheritance should never be your first choice. Code reuse is what composition is for, and that's what a UserControl provides. It's basically a collection of one or more UI controls and decorators that are strung together so as to do what you need. That's typically easier for others to understand as they don't change how the existing Controls behave.

On a side note, you'd also not inherit from a Control to change its looks. That is done entirely in XAML. Inheritance should generally be about changing a Controls behaviour (events it raises, when it raises them, how it interacts with its ControlTemplate, etc). Nothing else.

After thinking a little bit about how XAML works and what kind of functionality I would like to add in apps in the future, is it possible to "update" XAML code just like javascript updates HTML or is another mechanism usually used here? An example where you might use this: If a user clicks on a new Button and a new textbox must be inserted in the current page, you can of course instantiate the new Textbox in your C# code and add it as a children to the container. But XAML is used to create such content much more easily so how would you got about adding XAML code dynamically while also knowing that XAML code is actually compiled to C#? Is there like a function which you can use to inject XAML code from a C# script?
XAML is actually not compiled to C# code. It's compiled to a binary representation of XAML which is parsed during application startup, but that's a bit off topic.

There are many ways to do this. Some examples:

a)
One way is similar to what you're familiar with. In that scenario you create your Controls in code-behind and insert them into the visual tree (what XAML is turned into at runtime). This works fine for small changes to the UI.

b)
Another way is to include all the Controls you need in the XAML, but set their Visibility to collapsed, so they are invisible to the user. When required you'd then just set their Visibility to visible to have them shown. This is easy and performs well, but it only works for a set number of elements. It's not really as dynamic as what you're likely asking for.

c)
The final and most common approach involves the MVVM pattern. This is a bit more involved to explain and understand, but it essentially allows the dynamic parts of your UI to be constructed automatically based on the contents of your business model.

Imagine having three Person objects that are contained in an ObservableCollection, and that collection being observed by a ListView Control that you define in XAML. Together with that ListView you'd also use XAML to define a DataTemplate that specifies how a Person object is to be shown on screen. After hooking all that up, you can then add/remove Person objects to/from the ObservableCollection and the ListView will update itself automatically based on the collection's contents.

With this solution you have no code explicitly creating the dynamic parts of the UI at all.

There are many samples on msdn that you can look at that demonstrate this type of thing, for example here:

https://code.msdn.microsoft.com/windowsapps/ListViewSimple-d5fc27dd#content
 

Members online

No members online now.

Forum statistics

Threads
322,911
Messages
2,242,885
Members
428,005
Latest member
COME ON WIN ANDROID (ADI)