为UWP App实施教学提示控件

时间:2019-06-19 13:13:17

标签: xaml uwp nuget win-universal-app uwp-xaml

要在我的UWP应用中安装TeachingTip-控件,请执行以下步骤:

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

我实现了 TeachingTip -控件,如下所示:

<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错误),我不理解:

enter image description here

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

编辑: 我现在确定错误是由于App.xaml引起的。我安装了Nuget软件包Microsoft.UI.Xaml之后,应该在App.xaml中添加以下代码: enter image description here

但是我已经拥有App.xaml的其他设置和资源: enter image description here

当我尝试仅在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

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

3 个答案:

答案 0 :(得分:0)

我将您的代码粘贴到我的项目中,并且可以正常工作。但是,上面提供的代码没有触发按钮的内容,因此出现的唯一按钮是顶部按钮。您确定不会因为BackButton_Click而不是TeachingTipButton_OnClick中的代码而失败吗?

您可以从Microsoft.UI.Xaml.Controls删除对Application.Resources的重复引用

答案 1 :(得分:0)

我通过删除 ContentDialog Size样式默认按钮样式解决了我的问题,使得<Application.Resources>仅具有<XamlControlsResources x: Key = "XamlControlsResources" xmlns = "using: Microsoft.UI.Xaml.Controls" />作为单个条目。就我而言,很遗憾,<Application.Resources> </Application.Resources>中的多个条目未被接受。

答案 2 :(得分:0)

您的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>