【问题标题】:Adaptive card in WPF throws error 'Value cannot be null parameter name:Key'WPF 中的自适应卡引发错误“值不能为空参数名称:键”
【发布时间】:2021-07-29 15:24:29
【问题描述】:

我正在尝试在我们的一个 WPF 应用程序中呈现自适应卡片。由于我对 WPF 很陌生,我想先实现并展示基本的自适应卡,然后再转向实际需求。 我已经实现了MS Link for rendering adaptive card using .Net WPF SDK 中显示的示例。我尝试了相同的解决方案并进行了一些必要的更改,如下所示。

MainWindow.XAML:

    <Window x:Class="MyFirstWPFDotNetApp.MainWindow"
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
                Title="MainWindow" Height="350" Width="525">
        <Grid>
        <Grid x:Name="RenderAdaptiveCard" Margin="10" Background="WhiteSmoke">
            <Grid.Effect>
                <DropShadowEffect BlurRadius="15" ShadowDepth="2" RenderingBias="Quality" Direction="-90"/>
            </Grid.Effect>
        </Grid>  
        </Grid>
</Window>

代码背后的代码:

using AdaptiveCards;
using AdaptiveCards.Rendering.Wpf;
using System.Collections.Generic;
using System.Windows;

namespace MyFirstWPFApp
{
    
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            LoadAdaptiveCard();
        }

        private void LoadAdaptiveCard()
        {
            // Create a card renderer
            AdaptiveCardRenderer renderer = new AdaptiveCardRenderer();

            // If using the Xceed package, enable the enhanced input
            renderer.UseXceedElementRenderers();

            // For fun, check the schema version this renderer supports
            AdaptiveSchemaVersion schemaVersion = renderer.SupportedSchemaVersion;
            // Build a simple card
            // In the real world this would probably be provided as JSON
            AdaptiveCard card = new AdaptiveCard("1.0")
            {
                Body = { new AdaptiveTextBlock() { Text = "Hello World", Id="textWord" } }
            };

            try
            {
                // Render the card
                RenderedAdaptiveCard renderedCard = renderer.RenderCard(card);

                // Get the FrameworkElement

                // Add this to your app's UI somewhere
                RenderAdaptiveCard.Children.Clear();
                RenderAdaptiveCard.Children.Add(renderedCard.FrameworkElement);  // Added this line to render it into grid

                // (Optional) Check for any renderer warnings
                // This includes things like an unknown element type found in the card
                // Or the card exceeded the maximum number of supported actions, etc
                IList<AdaptiveWarning> warnings = renderedCard.Warnings;
            }
            catch (AdaptiveException ex)
            {
                // Failed rendering
            }
        }
    }
}

我在 .Net framework 4.7.1 和 .Net core 3.1 中都尝试了相同的代码,但在这两个应用程序中我都收到错误消息

值不能为空。参数名称:键

在线renderer.RenderCard(card);。我不明白这个例子是一半还是我需要添加一些其他引用或参数?尝试了一些研究,但没有找到关于这个主题的任何帮助。

【问题讨论】:

    标签: c# .net wpf asp.net-core-3.1 adaptive-cards


    【解决方案1】:

    我刚收到这个问题,看到你的问题没有得到解答,所以我想我仍然在这里为任何追随我们的人发帖。

    这里的问题是 AdaptiveCard 对象有一个未分配的 InternalId 属性,该属性稍后在 AdaptiveCardRenderer.RenderAdaptiveCardWrapper 方法中用作存储字典的索引。这就是引发非常模棱两可的异常的原因,因此只需按以下方式实例化您的 AdaptiveCard 即可解决该问题:

    AdaptiveCard card = new AdaptiveCard("1.0")
    {
      Body = { new AdaptiveTextBlock() { Text = "Hello World", Id = "textWord"} },
      InternalID = new AdaptiveInternalID()
    }
    

    【讨论】:

      猜你喜欢
      • 2014-05-17
      • 1970-01-01
      • 1970-01-01
      • 2015-06-22
      • 1970-01-01
      • 2013-03-23
      • 1970-01-01
      • 2021-01-02
      • 2020-04-04
      相关资源
      最近更新 更多