locked
[UWP][Xaml]Xaml Parsing Failed when subclassing ListBox/ListView/GridView RRS feed

  • Question

  • I am subclassing ListView in a Windows Universal App Project. I create a new UserControl in Visual Studio 2015 RC and then change the UserControl type to ListView in both XAML and codebehind.

    When I insert the control and run the application I get a 'Xaml Parsing Failed' exception without any further information.

    Upon researching I came across the issue where the project name contains a dot, but my project name does not contain such characters.

    Any Idea how to research this further?

    EDIT: I also tried subclassing ListView, GridView, ListBox and Itemscontrol. Itemscontrol does not generate the error but all others do.

    EDIT 2:

    1. Created a new Universal App Project in VS2015 RC

    2. Add -> UserControl

    3. Changed Base Class into:

      public sealed partial class ListViewEx : ListView { public ListViewEx() { this.InitializeComponent(); } }

    4. Changed XAML UserControl tag into ListView


    Tuesday, June 2, 2015 2:28 PM

Answers

  • It's not really practical to derive from UserControl then change the base type. Try using templated controls. See: Building a custom control using XAML and C#

    FYI: When changing the root element of the XAML from UserControl to something else, whatever contained in it (in markup) is actually setting whatever property is marked with the ContentProperty attribute. In most controls, including ContentControl and all its descendants, the content property is the ContentControl.Content property. But in ListView, GridView, ListBox, and all types that derive from ItemsControl, they have their content property set to the ItemsControl.Items property. I am betting you are putting elements right inside the root element. This adds items to the Items collection. However when you set ItemsSource, the Items collection becomes read-only, then the contents you defined is erased or blocked. Attempting to further modify the contents will result in an exception. What you do with templated controls is you change the Control.Template property instead. The Template property changes the only appearance of a control but does not interfere with the operations (unless the base control relies on a specific template part or visual state). This prevents your control from contradicting with whatever class you are deriving from (in most cases).

    Tuesday, June 2, 2015 11:16 PM

All replies

  • It's not really practical to derive from UserControl then change the base type. Try using templated controls. See: Building a custom control using XAML and C#

    FYI: When changing the root element of the XAML from UserControl to something else, whatever contained in it (in markup) is actually setting whatever property is marked with the ContentProperty attribute. In most controls, including ContentControl and all its descendants, the content property is the ContentControl.Content property. But in ListView, GridView, ListBox, and all types that derive from ItemsControl, they have their content property set to the ItemsControl.Items property. I am betting you are putting elements right inside the root element. This adds items to the Items collection. However when you set ItemsSource, the Items collection becomes read-only, then the contents you defined is erased or blocked. Attempting to further modify the contents will result in an exception. What you do with templated controls is you change the Control.Template property instead. The Template property changes the only appearance of a control but does not interfere with the operations (unless the base control relies on a specific template part or visual state). This prevents your control from contradicting with whatever class you are deriving from (in most cases).

    Tuesday, June 2, 2015 11:16 PM
  • Please read the sticky posts before posting. The Known Issues for Windows 10 SDK and Tools forum is explicitly unmonitored and not a support forum: Please don't post discussion threads in this forum!

    I'll move this to the Building Universal Windows apps forum for you.

    Please read Welcome to the Developing Universal Windows apps forum! the Guide to posting: subject line tags


    Thursday, June 4, 2015 11:04 PM
  • This approach works fine in WPF, but throws XamlMarkupException in universal application.
    Friday, December 25, 2015 6:31 PM