×

Discussion Board

Results 1 to 7 of 7
  1. #1
    Registered User
    Join Date
    Jan 2009
    Location
    Melbourne, Australia
    Posts
    2,571

    Creating pages from code/using XAML pages as a template "class"

    Imagine an app which displays (potentially) nested lists of objects. The way I would envisage viewing this object is to have a page with a list view, and then launch a new instance (of the same) page for each level of nesting. The user could then use back stack to navigate out of each of the pages.

    My first question is "is this possible" - my searches don't show any way to create/launch a new page using the XAML as a "template" for the page to be created (of course if I knew that only one level of nesting was permitted I could just create a second unique page for the next level)

    Of course I could just have one "real" page and change my data context to display the different lists as the user navigates, perhaps hijacking the back button navigation to navigate through my own back stack. However that seems less elegant than to just create new page objects as needed.

    Advice on the "right" approach much appreciated.

  2. #2
    Registered User
    Join Date
    Dec 2012
    Location
    Zagreb, Croatia
    Posts
    73

    Re: Creating pages from code/using XAML pages as a template "class"

    Not sure I understand why your proposal is different/better than just spawning new pages and using the regular back stack.

    I mean, you can always create XAML in the code behind either manually or by parsing actual XAML using XamlReader.

  3. #3
    Nokia Developer Champion
    Join Date
    Feb 2013
    Location
    Dublin, Ireland
    Posts
    552

    Re: Creating pages from code/using XAML pages as a template "class"

    Having real pages, as opposed to lots of dynamic XAML templates, may be a little cleaner in the long run.

    When a user taps on an item in the list, if that item can be of several types, thus requiring a different template on the subsequent page, that's not very difficult to do - and I still think having that logic on a separate page is cleaner overall.

    Depending on the type of object that's passed to this second page, you could use something like a DataTemplateSelector which will allow you to template all the different views needed.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Location
    Melbourne, Australia
    Posts
    2,571

    Re: Creating pages from code/using XAML pages as a template "class"

    Quote Originally Posted by to_pe View Post
    Not sure I understand why your proposal is different/better than just spawning new pages and using the regular back stack.

    I mean, you can always create XAML in the code behind either manually or by parsing actual XAML using XamlReader.
    What I actually want to to is spawn new pages. However search on "C# spawn new page" or "C# create new page from code behind" does not give me any indication of how to do so. My question is really "is this possible" and if so how. If not, then what is the recommended way to achieve the same behaviour. Every example I've seen starts from pre-created XAML pages and a navigation model that works between them.

  5. #5
    Nokia Developer Champion
    Join Date
    Feb 2013
    Location
    Dublin, Ireland
    Posts
    552

    Re: Creating pages from code/using XAML pages as a template "class"

    Quote Originally Posted by hamishwillee View Post
    What I actually want to to is spawn new pages. However search on "C# spawn new page" or "C# create new page from code behind" does not give me any indication of how to do so. My question is really "is this possible" and if so how. If not, then what is the recommended way to achieve the same behaviour. Every example I've seen starts from pre-created XAML pages and a navigation model that works between them.
    I do have to ask why?

    There isn't much different between creating a new PhoneApplicationPage vs a Grid + animation

  6. #6
    Nokia Developer Champion
    Join Date
    Mar 2013
    Posts
    511

    Re: Creating pages from code/using XAML pages as a template "class"

    Are all the objects in the nested list of the same type? In that case I'd simply navigate to a new page and pass along a parameter that enables the page to load the new root node and create the new list consisting of the root node's child nodes. Even if they aren't of exactlly the same type you can still use data binding to the new list and use DataTemplates in the page to display the right UI.

    What it comes down to is using the Parameter to change the DataContext of the new page instead of changing the DataContext of the current page. So if you call the ListPage.xaml withouut a parameter you'd load the original list of objects. If the object with id 1 is selected you'd navigate to the same page ListPage.xaml?objid=1 and load the according list. You'll now have two instances of the same page but with different data.

    Have I understood correctly what you're trying to do?

  7. #7
    Registered User
    Join Date
    Jan 2009
    Location
    Melbourne, Australia
    Posts
    2,571

    Re: Creating pages from code/using XAML pages as a template "class"

    Quote Originally Posted by SB Dev View Post
    Are all the objects in the nested list of the same type? In that case I'd simply navigate to a new page and pass along a parameter that enables the page to load the new root node and create the new list consisting of the root node's child nodes. Even if they aren't of exactlly the same type you can still use data binding to the new list and use DataTemplates in the page to display the right UI.

    What it comes down to is using the Parameter to change the DataContext of the new page instead of changing the DataContext of the current page. So if you call the ListPage.xaml withouut a parameter you'd load the original list of objects. If the object with id 1 is selected you'd navigate to the same page ListPage.xaml?objid=1 and load the according list. You'll now have two instances of the same page but with different data.

    Have I understood correctly what you're trying to do?
    Yes, that answered the question. Thank you. Wasn't clear to me that navigating to a page with a new argument created a new instance.

    Quote Originally Posted by theothernt View Post
    I do have to ask why?
    I'm playing. Lots of time on my hands.

Similar Threads

  1. send sms more than 3 pages, got error "illegal argument exception : too big message"
    By eftech in forum Mobile Java Networking & Messaging & Security
    Replies: 1
    Last Post: 2008-12-31, 11:15
  2. Replies: 0
    Last Post: 2004-10-12, 15:56
  3. How to forward to other jsp pages/cards by "if..else..." in WML?
    By msc73jxs in forum Browsing and Mark-ups
    Replies: 0
    Last Post: 2002-08-29, 09:00

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×