Home > database >  Why does AppWindowTitleBar.PreferredHeightOption cause CS0120 in the Windows App SDK?
Why does AppWindowTitleBar.PreferredHeightOption cause CS0120 in the Windows App SDK?

Time:08-19

I am currently using Winui-3 and the Windows App SDK for the first time as I usually write apps in classic Win32 C . My app has a tall title bar (48px) and I want the caption buttons to be sized appropriately. I have already tried using the method from the Microsoft docs, but that causes CS0120: AppWindowTitleBar.PreferredHeightOption = TitleBarHeightOption.Tall;

I have not found any solutions to this exact problem online. Putting it into a static method did not work. While experimenting, I've found that other customizations like AppWindowTitleBar.ForegroundColor = Colors.White; also don't work. I am confused.

EDIT: Added implementation of the title bar

public ShellPage(ShellViewModel viewModel)
    {
        ViewModel = viewModel;
        InitializeComponent();

        ViewModel.NavigationService.Frame = NavigationFrame;
        ViewModel.NavigationViewService.Initialize(NavigationViewControl);

        AppWindowTitleBar.PreferredHeightOption = TitleBarHeightOption.Tall;

        App.MainWindow.ExtendsContentIntoTitleBar = true;
        App.MainWindow.SetTitleBar(AppTitleBar);
        App.MainWindow.Activated  = MainWindow_Activated;
        AppTitleBarText.Text = "AppDisplayName".GetLocalized();
    }

CodePudding user response:

The AppWindowTitleBar doesn't have PreferredHeightOption. That's why you're getting CS0120.

Try this.

public ShellPage(ShellViewModel viewModel)
{
    ViewModel = viewModel;
    InitializeComponent();

    ViewModel.NavigationService.Frame = NavigationFrame;
    ViewModel.NavigationViewService.Initialize(NavigationViewControl);

    //AppWindowTitleBar.PreferredHeightOption = TitleBarHeightOption.Tall;

    App.MainWindow.ExtendsContentIntoTitleBar = true;  // not sure if you need this line.
    this.appWindow = GetAppWindowForCurrentWindow();
    this.appWindow.TitleBar.ExtendsContentIntoTitleBar = true;
    this.appWindow.TitleBar.PreferredHeightOption = TitleBarHeightOption.Tall;
    App.MainWindow.SetTitleBar(AppTitleBar);
    App.MainWindow.Activated  = MainWindow_Activated;
    AppTitleBarText.Text = "AppDisplayName".GetLocalized();
}

private AppWindow appWindow;

private AppWindow GetAppWindowForCurrentWindow()
{
    IntPtr hWnd = WindowNative.GetWindowHandle(App.MainWindow);
    WindowId wndId = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(hWnd);
    return AppWindow.GetFromWindowId(wndId);
}
  • Related