首页 > 解决方案 > 为 UWP 应用实现 TeachingTip 控件

问题描述

要在我的 UWP 应用程序中安装TeachingTip -control,我已完成以下步骤:

  1. Microsoft.UI.Xaml在我的项目中通过 Nuget安装包
  2. 添加<XamlControlsResources xmlns = "using:Microsoft.UI.Xaml.Controls" />App.xaml.
  3. 导入的命名空间xmlns:controls="using:Microsoft.UI.Xaml.Controls"

我实现了TeachingTip -control 如下:

<Button x:Name="BackButton"
        Background="{x:Null}"
        Content="Back"
        Click="BackButton_Click">
    <Button.Resources>
        <controls:TeachingTip x:Name="ToggleThemeTeachingTip"
                              Target="{x:Bind BackButton}"
                              Title="Change themes without hassle"
                              Subtitle="It's easier than ever to see control samples in both light and dark theme!"
                              CloseButtonContent="Got it!">
        </controls:TeachingTip>
    </Button.Resources>
</Button>

<Button x:Name="TeachingTipButton"
        Click="TeachingTipButton_OnClick">
</Button>


private void TeachingTipButton_OnClick(object sender, RoutedEventArgs e)
{
    ToggleThemeTeachingTip.IsOpen = true;
}

当我调用该函数时,我收到以下DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION错误(可能是 UI 错误),我不明白:

在此处输入图像描述

可能是什么问题呢?为什么我的代码不起作用?

编辑: 我现在已经确定错误是由于App.xaml. 在我安装了 Nuget 包Microsoft.UI.Xaml之后,预计会在其中添加以下代码App.xaml在此处输入图像描述

但我已经在App.xaml其他设置和资源中: 在此处输入图像描述

当我尝试仅在App.xaml键中添加行时,会发生错误:

<XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls"/>

如果我给资源条目一个这样的键:

<XamlControlsResources x: Key = "XamlControlsResources" xmlns = "using: Microsoft.UI.Xaml.Controls" />

 它遇到了一个完全不同的错误:

Windows.UI.Xaml.Markup.XamlParseException: "The text for this error is not found.

Can not find a Resource with the Name / Key TeachingTipBackgroundBrush

如何<XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls"/>在我的 App.xaml 中正确添加资源?

标签: xamluwpnugetwin-universal-appuwp-xaml

解决方案


您的 App.xaml 文件需要如下所示:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
        </ResourceDictionary.MergedDictionaries>
        <!-- Other styles here -->
        <Style TargetType="Button">
          ...
        </Style>
    </ResourceDictionary>
</Application.Resources>

推荐阅读