使用刀片的多个视图

时间:2017-06-09 12:03:48

标签: uwp uwp-xaml windows-community-toolkit

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

创建新窗口

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

  

System.Runtime.InteropServices.COMException:错误HRESULT已从调用COM组件返回E_FAIL。       在Windows.UI.Xaml.FrameworkElement.MeasureOverride(Size availableSize)

要复制错误,请将以下代码添加到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. 创建新视图
  5. 任何人都可以识别导致错误的原因,或者告诉我独立视图中的刀片是否不起作用?

1 个答案:

答案 0 :(得分:2)

有趣。问题实际上是由BladeItem的默认样式引起的。

由于您已将TitleBarVisibility设置为Collapsed,因此解决此问题非常简单。您只需将以下样式应用于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