简体   繁体   English

在 WinUI 3 中将网格保存到 DependencyProperty:在 WinUI 3 中使用 AppTitleBar

[英]Saving a Grid to a DependencyProperty in WinUI 3: Working with the AppTitleBar in WinUI 3

I've run into a curious problem trying to work with the AppTitleBar in a WinUI 3 program.我在尝试在 WinUI 3 程序中使用 AppTitleBar 时遇到了一个奇怪的问题。 The title bar is meant to reformat itself dynamically (for visually challenged users) which means I need to know where everything in the WindowChrome is.标题栏旨在动态重新格式化自身(对于有视觉障碍的用户),这意味着我需要知道 WindowChrome 中的所有内容在哪里。 I'm using the system buttons in the top right of the window (held in the MinMaxCloseContainer, a Grid) as a reference for various characteristics so, once I find it in the visual tree, I save it's reference in a DependencyProperty.我使用窗口右上角的系统按钮(保存在 MinMaxCloseContainer,一个网格中)作为各种特征的参考,因此,一旦我在可视化树中找到它,我将它的参考保存在 DependencyProperty 中。

The problem is, I'm getting a System.InvalidCastException: 'Unable to cast object of type 'Microsoft.UI.Xaml.GridLength' to type 'System.Double'.'问题是,我得到一个 System.InvalidCastException:'无法将'Microsoft.UI.Xaml.GridLength' 类型的对象转换为'System.Double'。 exception in the property setter.属性设置器中的异常。

I am using the CommunityToolkit.Mvvm v7.1.2, and the Microsoft.WindowsAppSDK 1.1.1 (WinUI 3).我正在使用 CommunityToolkit.Mvvm v7.1.2 和 Microsoft.WindowsAppSDK 1.1.1 (WinUI 3)。 The project targets .NET 6.0.该项目面向 .NET 6.0。 The ViewModel is derived from ObservableRecipient. ViewModel 派生自 ObservableRecipient。

Here is the code that searches the visual tree:这是搜索可视化树的代码:

    var clientAreaPresenter = VisualTreeHelper.GetParent(shellPage) as ContentPresenter;
    var layoutRoot = VisualTreeHelper.GetParent(clientAreaPresenter) as Grid;                       // this is the content of the WindowChrome (chrome and program content)
    var titleBarMinMaxCloseContainer = VisualTreeHelper.GetChild(layoutRoot, 1) as Grid;            // the system Min, Max and close buttons on the title bar
    var minMaxCloseContainer = VisualTreeHelper.GetChild(titleBarMinMaxCloseContainer, 0) as Grid;  // this container holds three Buttons: Min, Max, Close
    ViewModel.MinMaxCloseContainer = minMaxCloseContainer;                                          // remember where this is

(last line sets the DependencyProperty) and here is the property definition in the viewmodel: (最后一行设置 DependencyProperty),这是视图模型中的属性定义:

/// <summary>
/// The system area containing the min, max and close buttons on the title bar
/// </summary>
public Grid MinMaxCloseContainer
{
    get => _minMaxCloseContainer;
    set => SetProperty(ref _minMaxCloseContainer, value);
}

The setter is where the error occurs but the Call Stack shows it's actually in the SetProperty() method. setter 是发生错误的地方,但调用堆栈显示它实际上在 SetProperty() 方法中。 Although this looks like a reference assignment, I think the SetProperty method is actually taking a private copy.虽然这看起来像一个引用分配,但我认为 SetProperty 方法实际上是在获取一个私有副本。 The error is probably due to undefined values (double.NaN) in various properties of the minMaxCloseContainer instance (for example, .Height and .Width during initialization).该错误可能是由于 minMaxCloseContainer 实例的各种属性中的未定义值 (double.NaN)(例如,初始化期间的 .Height 和 .Width)。 The .NET code I've found for我找到的 .NET 代码

I've tried: 1) attaching a cast to GridLength (not valid in static classes);我尝试过:1)将强制转换附加到 GridLength(在静态类中无效); 2) checking for undefined values and changing them before assigning the Grid (no effect); 2)在分配网格之前检查未定义的值并更改它们(无效); 3) customizing the GridLengthConverter. 3)自定义GridLengthConverter。 The last item is odd because the published .NET code says the default converter does handle casts from GridLength to Double, even with double.NaN values.最后一项很奇怪,因为已发布的 .NET 代码说默认转换器确实处理从 GridLength 到 Double 的强制转换,即使使用 double.NaN 值也是如此。 It may be that WinUI hasn't implemented a GridLengthConverter yet.可能是 WinUI 还没有实现 GridLengthConverter。

Anyway, everything I've tried hasn't worked and I'm not really sure why the error is happening in the first place.无论如何,我尝试过的一切都没有奏效,我不确定为什么会首先发生错误。 Any ideas would be greatly appreciated.任何想法将不胜感激。 Thanks.谢谢。

It appears that today's update to Visual Studio 2022 Enterprise has fixed the issue.看来,今天对 Visual Studio 2022 Enterprise 的更新已经解决了这个问题。 Everything is working as expected now.现在一切都按预期工作。 I'm not sure what changed, but I'm using the following version, for those who are experiencing a similar issue:我不确定发生了什么变化,但是对于遇到类似问题的人,我正在使用以下版本:

MicrosoftCorporationII.WinAppRuntime.Main.1.1_1001.524.1918.0_x64__8wekyb3d8bb

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

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