简体   繁体   English

为什么 AppWindowTitleBar.PreferredHeightOption 在 Windows 应用程序 SDK 中导致 CS0120?

[英]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++.我目前第一次使用 Winui-3 和 Windows 应用程序 SDK,因为我通常在经典 Win32 C++ 中编写应用程序。 My app has a tall title bar (48px) and I want the caption buttons to be sized appropriately.我的应用程序有一个高标题栏(48 像素),我希望标题按钮的大小适当。 I have already tried using the method from the Microsoft docs, but that causes CS0120 : AppWindowTitleBar.PreferredHeightOption = TitleBarHeightOption.Tall;我已经尝试使用 Microsoft 文档中的方法,但这会导致CS0120AppWindowTitleBar.PreferredHeightOption = TitleBarHeightOption.Tall;

I have not found any solutions to this exact problem online.我还没有在网上找到任何解决这个确切问题的方法。 Putting it into a static method did not work.将其放入 static 方法不起作用。 While experimenting, I've found that other customizations like AppWindowTitleBar.ForegroundColor = Colors.White;在试验过程中,我发现其他自定义项如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 . AppWindowTitleBar没有PreferredHeightOption That's why you're getting CS0120 .这就是您获得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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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