1. Stamper_wc's Avatar
    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 tht 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!
    02-21-2015 07:07 AM
  2. a5cent's Avatar
    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 tht 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/wind...fc27dd#content
    Stamper_wc likes this.
    02-21-2015 10:46 AM
  3. Stamper_wc's Avatar
    Wow that is an detailed explanation, thanks so much! I am definitely going to have a look at those examples. Thanks again!
    a5cent likes this.
    02-21-2015 10:50 AM

Similar Threads

  1. Is Windows 10 For Phone Supports Background Download? ?
    By sanjeet_deshwal in forum Windows 10 Mobile Insider Preview
    Replies: 1
    Last Post: 02-21-2015, 12:42 PM
  2. Where is the Windows Phone Taskbar???
    By Windows Central Question in forum Ask a Question
    Replies: 2
    Last Post: 02-21-2015, 07:12 AM
  3. Why has my Nokia 735 screen rotation stopped working?
    By Windows Central Question in forum Ask a Question
    Replies: 1
    Last Post: 02-21-2015, 06:10 AM
  4. Replies: 2
    Last Post: 02-21-2015, 06:03 AM
  5. Is every lumia getting windows phone 10 upgrade?
    By Priyanka Goswami in forum Ask a Question
    Replies: 4
    Last Post: 02-21-2015, 05:30 AM
LINK TO POST COPIED TO CLIPBOARD