【问题标题】:No matching constructor found on type. You can use the Arguments or FactoryMethod directives to construct the type在类型上找不到匹配的构造函数。您可以使用 Arguments 或 FactoryMethod 指令来构造类型
【发布时间】:2013-07-15 09:49:27
【问题描述】:

我希望有人可以帮助我解决一个奇怪的问题。开发 WPF .NET Framework 4.0 应用程序,接收消息:

在类型上找不到匹配的构造函数。尝试使用以下代码时,您可以使用 Arguments 或 FactoryMethod 指令来构造类型。

UserControl:

<Grid>                    
    <GroupBox Header="Diagram">
        <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
            <designer:DesignerCanvas Focusable="true" x:Name="MyDesigner"
                Background="{StaticResource WindowBackgroundBrush}" Margin="10"
                FocusVisualStyle="{x:Null}"
                ContextMenu="{StaticResource DesignerCanvasContextMenu}" />
        </ScrollViewer>
    </GroupBox>
</Grid>

Class:

/// <summary>
/// The designer canvas.
/// </summary>
public partial class DesignerCanvas : Canvas
{
    public DesignerCanvas()
    {
      ....
    }
}

据我所知,代码似乎没有任何问题。

谢谢, 约翰

更新:

原来这是在掩盖底层异常。我在 DesignerCanvas 的构造函数中去掉了任何东西,它起作用了。然后我在一个单独的方法中重新添加了设置命令绑定并收到:

System.MissingMethodException:找不到方法:无效 System.Windows.Input.CommandBinding..ctor(System.Windows.Input.ICommand, System.Windows.Input.ExecutedRoutedEventHandler)

构造函数包括尝试设置一些命令绑定。例如

public void SetupCommandDefaults()
    {
        this.CommandBindings.Add(new CommandBinding(ApplicationCommands.New, this.New_Executed));
    }

我正在卸载并重新安装 .NET Framework 4.0,希望这能解决问题。

【问题讨论】:

  • 该错误是否有任何进一步的信息,可能详细说明类型?我似乎能够毫无错误地使用上述内容(包括一些模拟资源)。
  • 暂时没有。我正在尝试深入了解 DesignerCanvas 并找出导致异常的类型的原因。
  • 这很奇怪,您查看过CommandBinding 元数据来验证构造函数确实丢失了吗? =o

标签: c# .net wpf xaml canvas


【解决方案1】:

在重新安装 .NET Framework 4 并应用各种 KB 补丁(包括运行时 4.0.3)后,结果非常痛苦。问题是由 ILMerge msbuild 步骤引起的。

构建过程包括构建后的 msbuild 任务,以将主可执行文件与某些 dll 合并。

我删除了这个 ILMerge 步骤,错误消失了。

不知道为什么会在这个环境中发生这种情况,而不是在我们的任何测试机器上,但它现在可以工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-16
    • 1970-01-01
    • 2020-07-17
    • 2011-12-28
    • 1970-01-01
    相关资源
    最近更新 更多