locked
XamlParseException Error in GroupedItemsPage code-behind RRS feed

  • Question

  • Hi,

    For my app, users are supposed to login according to their respective dept. So, when a user login and there's nothing available for that dept, a message will be displayed in the GroupedItemsPage indicating that nothing is available at the moment. The displaying of the items is based on a count. If its more than 0, the gridview will display the groups. 

    The code below is written in the LoadState of the GroupedItemsPage:

    if (count > 0)
    {
         // TODO: Assign a collection of bindable groups to this.DefaultViewModel["Groups"]
         var sampleDataGroups = VMDataSource.GetGroups((String)navigationParameter);
         this.DefaultViewModel["Groups"] = sampleDataGroups;
    }
    
    else
    {
         StringBuilder sb = new StringBuilder();
         sb.Append("<DataTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">");
         sb.Append("<Grid>");
         sb.Append("<StackPanel>");
         sb.Append("<TextBlock Text=\"There is nothing available currently.\" HorizontalAlignment=\"Center\" VerticalAlignment=\"Center\" Style=\"{StaticResource AppBodyTextStyle}\" />");
         sb.Append("</Grid>");
         sb.Append("</DataTemplate>");
    
         DataTemplate datatemplate = (DataTemplate)XamlReader.Load(sb.ToString());
         this.itemGridView.ItemTemplate = datatemplate;
         this.itemListView.ItemTemplate = datatemplate;
    }

    I'm getting error Window.UI.Xaml.Markup.XamlParseException at the following line:

    DataTemplate datatemplate = (DataTemplate)XamlReader.Load(sb.ToString());

    Can anyone point where I went wrong?

    Thanks.


    Thursday, August 29, 2013 3:03 AM

Answers

  • OK - if you have no data to display, the GridView/ListView won't loop through the datatemplate.  If the count is 0, assign the datatemplate, then create a temp list with 1 item in it and set that as the ItemsSource.


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Wednesday, September 4, 2013 5:17 PM
    Moderator

All replies

  • If you output the string to a textblock, and then copy/paste it into a XAML page, does the XAML parse properly?

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Thursday, August 29, 2013 12:41 PM
    Moderator
  • Hi Matt,

    Thanks for your reply.

    I've made a little mistake by not closing the stack panel tag. Now I can parse it properly its just that it doesn't appear anything when I run the project. The text does not appear. Is it due to the grid app template that already has a binding to its items source and the declaration of the item template? 

    I declare it as the itemGridView item template but nothing was displayed. There's no error now when I run. It just doesn't display anything.

    Thanks.

    Friday, August 30, 2013 1:51 AM
  • Hi,

    Can anyone help me out? I'm in need of assistance to overcome the problem that I encountered. When I run the app, there's no error but the message doesn't get displayed.

    Thanks.

    Monday, September 2, 2013 2:24 AM
  • OK - if you have no data to display, the GridView/ListView won't loop through the datatemplate.  If the count is 0, assign the datatemplate, then create a temp list with 1 item in it and set that as the ItemsSource.


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Wednesday, September 4, 2013 5:17 PM
    Moderator
  • Hi Matt,

    Do you have any links of a sample that I can refer to? 

    Thanks.

    Thursday, September 5, 2013 3:18 AM
  • var NewList = new List<string>();
    NewList.Add("dummy string");
    this.itemGridView.ItemsSource = NewList;
    this.itemListView.ItemsSource = NewList;


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Thursday, September 5, 2013 6:01 PM
    Moderator