none
[UWP]Cannot use any control from Microsoft.UI.Xaml.Controls control, "Failed to add to UIElementCollection. Unspecified error" RRS feed

  • Question

  • Please help for me to use controls from Microsoft.UI.Xaml.Controls.

    1. I added Microsoft.UI.Xaml.Controls via NuGet. version: 2.2.190830001

    2. Add a control from Toolbox and it displays "Failed to add to UIElementCollection. Unspecified error"

    I tried many way to fix this. It does not work. It throws exception in runtime with very not related error message but pointing to the control.

    How can I fix this? Any help? Thank you!

    Here is my project details:

    Runtime error: Strange : "TabViewItemHeaderBackground" is mentioned which is totally not related, I guess, because I search all in projects and found none matching it.

    The text associated with this error code could not be found. Cannot find a Resource with the Name/Key TabViewItemHeaderBackground [Line: 13 Position: 11]



    MCSD .NET, SCJP, SCJWD


    Tuesday, September 10, 2019 8:40 PM

Answers

  • Hi,
    From your description, you missed a step. You need to add the Windows UI (WinUI) Theme Resources to your App.xaml resources. There are two ways to do this, depending on whether you have additional application resources. 
    a. If you don't have other application resources, add to your Application.
    <Application>
        <Application.Resources>
            <XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" /> 
        </Application.Resources>
    </Application>
    b. If you have more than one set of application resources, add to Application.Resources.MergedDictionaries:
    <Application>
        <Application.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
                </ResourceDictionary.MergedDictionaries> 
            </ResourceDictionary>
        </Application.Resources>
    </Application>
    The more details about it, you can refer to this document.
    Best Regards,
    Fay

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by OnTheWayToThat Wednesday, September 11, 2019 9:13 PM
    Wednesday, September 11, 2019 9:29 AM

All replies

  • Hi,
    From your description, you missed a step. You need to add the Windows UI (WinUI) Theme Resources to your App.xaml resources. There are two ways to do this, depending on whether you have additional application resources. 
    a. If you don't have other application resources, add to your Application.
    <Application>
        <Application.Resources>
            <XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" /> 
        </Application.Resources>
    </Application>
    b. If you have more than one set of application resources, add to Application.Resources.MergedDictionaries:
    <Application>
        <Application.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
                </ResourceDictionary.MergedDictionaries> 
            </ResourceDictionary>
        </Application.Resources>
    </Application>
    The more details about it, you can refer to this document.
    Best Regards,
    Fay

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by OnTheWayToThat Wednesday, September 11, 2019 9:13 PM
    Wednesday, September 11, 2019 9:29 AM
  • It works! Thank you so~~ much!

    MCSD .NET, SCJP, SCJWD

    Wednesday, September 11, 2019 8:32 PM