2

我正在尝试创建一个允许用户打开新窗口的 UWP 应用程序。我基于Microsoft 的 Multiple Views Sample创建新窗口

当新视图包含刀片(来自 Microsoft.Toolkit.Uwp.UI.Controls)时,我遇到了一个奇怪的错误。错误是:

System.Runtime.InteropServices.COMException:对 COM 组件的调用已返回错误 HRESULT E_FAIL。在 Windows.UI.Xaml.FrameworkElement.MeasureOverride(大小可用大小)

要复制错误,请将以下代码添加到 SecondaryViewPage.xaml 中第 49 行的链接示例:

<controls:BladeView x:Name="BladeView" Grid.Column="0"
            Padding="0"
            BladeMode="{Binding BladeMode.Value}">
    <controls:BladeItem
                TitleBarVisibility="Collapsed"
                IsOpen="True" Width="300" />
</controls:BladeView>

然后执行以下步骤:

  1. 创建新视图
  2. 显示视图
  3. 关闭视图
  4. 创建新视图

谁能确定导致错误的原因,或者告诉我独立视图中的刀片是否不起作用?

4

1 回答 1

2

有趣的。该问题实际上是由于BladeItem某种原因的默认样式引起的。

由于您已经设置TitleBarVisibilityCollapsed,因此修复此问题非常简单。您只需将以下样式应用于您的BladeItem. 这个和默认的唯一区别是内部Grid被注释掉了。是的,这就是问题所在。

<Style x:Key="MyBladeStyle" TargetType="controls:BladeItem">
    <Setter Property="VerticalContentAlignment" Value="Stretch" />
    <Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}" />
    <Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}" />
    <Setter Property="Background" Value="Transparent" />
    <Setter Property="Foreground" Value="{ThemeResource SystemControlForegroundBaseHighBrush}" />
    <Setter Property="TabNavigation" Value="Local" />
    <Setter Property="IsHoldingEnabled" Value="True" />
    <Setter Property="HorizontalContentAlignment" Value="Stretch" />
    <Setter Property="HorizontalAlignment" Value="Stretch" />
    <Setter Property="VerticalAlignment" Value="Stretch" />
    <Setter Property="Margin" Value="0" />
    <Setter Property="MinWidth" Value="{ThemeResource GridViewItemMinWidth}" />
    <Setter Property="MinHeight" Value="{ThemeResource GridViewItemMinHeight}" />
    <Setter Property="BorderBrush" Value="Black" />
    <Setter Property="BorderThickness" Value="1" />
    <Setter Property="IsTabStop" Value="False" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="controls:BladeItem">
                <Grid BorderBrush="{TemplateBinding BorderBrush}"
                      BorderThickness="{TemplateBinding BorderThickness}">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>

                    <!--<Grid Background="{TemplateBinding TitleBarBackground}"
                          Visibility="{TemplateBinding TitleBarVisibility}">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>

                        <TextBlock Margin="4,0,0,0"
                                   HorizontalAlignment="Left"
                                   VerticalAlignment="Center"
                                   Foreground="{TemplateBinding TitleBarForeground}"
                                   Text="{TemplateBinding Title}" />
                        <Button Name="CloseButton"
                                Grid.Column="1"
                                TabIndex="0"
                                HorizontalAlignment="Right"
                                AutomationProperties.Name="Cancel"
                                Background="{TemplateBinding CloseButtonBackground}"
                                Content="&#xE711;"
                                FontFamily="Segoe MDL2 Assets"
                                Foreground="{TemplateBinding CloseButtonForeground}" />
                    </Grid>-->

                    <ContentPresenter Grid.Row="1"
                                      VerticalAlignment="Stretch"
                                      Background="{TemplateBinding Background}"
                                      Visibility="{TemplateBinding IsOpen}" />
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

更新

好的,经过一番挖掘,我发现真正的问题实际上在于BladeItem-TitleBarForegroundCloseButtonForeground. 这可能是一个 UWP 错误,因为解决方法很简单,只需为它们提供一些默认值(请参阅下面的 xaml 代码),尽管它们的依赖项属性声明中已经设置了相同的默认值。

<!-- Add the following to the default style -->
<Setter Property="TitleBarForeground" Value="Black" />
<Setter Property="CloseButtonForeground" Value="Black" />

现在注意上面的两行代码,你不再需要注释掉内部的Grid.

于 2017-06-13T12:41:28.397 回答