亿图 API - “ContentControl”的内容必须是单个元素

问题描述

我是 WPF 新手。我想连接到 Edrawings。我使用了以下指令:

https://www.codestack.net/edrawings-api/gettings-started/wpf/

我已经在 windowsforms 中完成了这个实现,效果很好。在 Wpf 中,我收到以下错误

“ContentControl”的内容必须是单项”

在这里找到了一些解决方案。但不幸的是,没有什么能解决我的问题。

另外,这里是代码(与同类相同):

namespace PDM
{

public partial class eDrawingsHostControl : UserControl
{

    private EModelViewControl m_Ctrl;
    public eDrawingsHostControl()
    {
        InitializeComponent();

        var host = new windowsformshost();
        var ctrl = new eDrawingHost();
        ctrl.ControlLoaded += OnControlLoaded;
        host.Child = ctrl;
        this.AddChild(host); // --- Here I got the error ---

    }   

    public string FilePath
    {
        get { return (string)GetValue(FilePathProperty); }
        set { SetValue(FilePathProperty,value); }
    }

    public static readonly DependencyProperty FilePathProperty =
        DependencyProperty.Register(nameof(FilePath),typeof(string),typeof(eDrawingsHostControl),new FrameworkPropertyMetadata(OnFilePathPropertyChanged));

    private static void OnFilePathPropertyChanged(DependencyObject d,DependencyPropertyChangedEventArgs e)
    {
        (d as eDrawingsHostControl).OpenFile(e.NewValue as string);
    }

    private void OpenFile(string filePath)
    {
        if (m_Ctrl == null)
        {
            throw new NullReferenceException("eDrawings control is not loaded");
        }

        if (string.IsNullOrEmpty(filePath) || !File.Exists(filePath))
        {
            m_Ctrl.CloseActiveDoc("");
        }
        else
        {
            m_Ctrl.OpenDoc(filePath,false,"");
        }
    }

    private void OnControlLoaded(EModelViewControl ctrl)
    {
        m_Ctrl = ctrl;
        m_Ctrl.OnFinishedLoadingDocument += OnFinishedLoadingDocument;
        m_Ctrl.OnFailedLoadingDocument += OnFailedLoadingDocument;
    }

    private void OnFailedLoadingDocument(string fileName,int errorCode,string errorString)
    {
        Trace.WriteLine($"{fileName} Failed to loaded: {errorString}");
    }

    private void OnFinishedLoadingDocument(string fileName)
    {
        Trace.WriteLine($"{fileName} loaded");
    }
}

这是xaml代码

<Window x:Class="PDM.Edrawing"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:myControls="clr-namespace:PDM"
    mc:Ignorable="d"
    Title="Edrawing" Height="450" Width="800">

<Grid>
    <Grid.RowDeFinitions>
        <RowDeFinition Height="*"/>
        <RowDeFinition Height="Auto"/>
    </Grid.RowDeFinitions>
    <myControls:eDrawingsHostControl Grid.Row="0" FilePath="{Binding Path=Text,ElementName=txtFilePath,UpdateSourceTrigger=Explicit}"/>
    <TextBox Grid.Row="1" x:Name="txtFilePath"/>
</Grid>

非常感谢您的帮助:)

解决方法

您必须将 this.AddChild(host); 替换为 this.Content=host;。请注意,这种方式主机将是唯一的内容。
如果您想在 UserControl 中有一些额外的控件,您必须使用这些控件定义 ControlTemplate,例如ContentPresenter 在其中。