Edrawing 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, false, 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.Content=host;替换this.AddChild(host);。请注意,这样的主持人方式将是唯一的内容
如果您想在UserControl中有一些额外的控件,您必须定义具有这些控件的ControlTemplate,例如其中的ContentPresenter

最新更新