简体   繁体   中英

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

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();
    }

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM