WinUI2 XAML Island : Unable to use TabView

Harshithraj1871 1,421 Reputation points
2023-05-11T07:00:56.6666667+00:00

Hi,

I'm working with WinUI2 in desktop apps using XAML island. I wanted to use WinUI2's tabview controls.

I followed this documentation to create a sample WinUI2 xaml island app. Then I added Microsoft.UI.Xaml NuGet package to the project.User's image

Then I tried using WinUI2 tabview control.

But when i created a tabview like this -

winrt::Microsoft::UI::Xaml::Controls::TabView tabView;

I'm getting this error

User's image

User's image

It would be of great help if u could help me fix this error.

Thank you

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,389 questions
{count} votes

Accepted answer
  1. Junjie Zhu - MSFT 13,971 Reputation points Microsoft Vendor
    2023-05-11T08:43:34.72+00:00

    Hi @Harshithraj1871

    The documentation Host a standard WinRT XAML control in a C++ desktop (Win32) app describes how to use WinRT XAML control, but Microsoft.WinUI2 is not supported for now.

    If you want to use Tabview in a desktop application, it is recommended that you use the WinUI3 project.

    How to add Win32 tab control in XAML island?

    You can refer to this document Create a Tab Control in the Main Window.

    I tested the code, and it can be used in XAML island.

    //#include "CommCtrl.h"
    //#pragma comment(lib,"Comctl32.lib")
    RECT rcClient;
    INITCOMMONCONTROLSEX icex;
    TCITEM tie;
    int i;
    TCHAR achTemp[256];  // Temporary buffer for strings.
    
    // Initialize common controls.
    icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
    icex.dwICC = ICC_TAB_CLASSES;
    InitCommonControlsEx(&icex);
    
    // Get the dimensions of the parent window's client area, and 
    // create a tab control child window of that size. Note that g_hInst
    // is the global instance handle.
    GetClientRect(WindowHandle(), &rcClient);
    HWND hwndTab = CreateWindow(WC_TABCONTROL, L"",
        WS_CHILD | WS_CLIPSIBLINGS | WS_VISIBLE,
        0, 0, rcClient.right, rcClient.bottom,
        WindowHandle(), NULL, m_instance, NULL);
    if (hwndTab == NULL)
    {
        return NULL;
    }
    
    // Add tabs for each day of the week. 
    tie.mask = TCIF_TEXT | TCIF_IMAGE;
    tie.iImage = -1;
    wchar_t lpWStr[32] = { 0 };
    wsprintfW(lpWStr, L"TAB");
    tie.pszText = lpWStr;
    
    for (i = 0; i < 7; i++)
    {
        // Load the day string from the string resources. Note that
        // g_hInst is the global instance handle.
                
        if (TabCtrl_InsertItem(hwndTab, i, &tie) == -1)
        {
            DestroyWindow(hwndTab);   
        }
    }
    

    Thank you,

    Junjie


0 additional answers

Sort by: Most helpful