XAML设计器中的"找不到方法"错误

And*_*erd 5 c# wpf xaml mvvm

当我在Visual Studio中的WPF设计器中查看数据时,我希望我的用户控件显示数据.

ViewModel没有默认构造函数,因此我编写了自己的静态TestData类来构造模型及其所有依赖项.

public static class TestData
{
    public static ELabelViewModel ELabelViewModel
    {
        get
        {
            return new ELabelViewModel
            (
                new ControlPanelGridLine(TestData.ELabel),
                new SerialPortFactoryImpl(),
                new Repository(),
                new PriceLabelGenerator(TestData.IPriceLabelViewModelFactory)
            );
        }
    }

    // Other static getter methods
Run Code Online (Sandbox Code Playgroud)

这一切都没有问题.但是,当我在XAML中添加时会出现问题:

   d:DataContext="{x:Static local:TestData.ELabelViewModel}"
Run Code Online (Sandbox Code Playgroud)

XAML编辑器在我的d:DataContext属性下放置一条蓝色的蓝色线条,在错误列表中我看到:

错误7找不到方法:'Void ELabel.Manager.ViewModels.ELabelViewModel..ctor(ELabel.Manager.ViewModels.ControlPanelGridLine,ELabel.Control.ISerialPortFactory,ELabel.Data.IRepository,ELabel.ImageGeneration.IPriceLabelGenerator)'.

我对此的解释是它找到了这个TestData类,并找到了TestData.ELabelViewModel属性.它只是无法解析在getter中调用的构造函数.

为什么找不到ELabelViewModel构造函数?为了确认我的代码没问题,我通过使用DataContext=而不是使这个测试视图模型化了实际的数据上下文d:DataContext=.在这种情况下,我打开了应用程序,并确认在运行时,所有工作都按预期进行:TestData.ELabelViewModel调用,getter函数内部的代码运行,并使用此视图模型.只是设计师无法运行代码.

ELabelViewModel类是在所谓的分离组件ELabel.Manager.ViewModels.编辑器无法完全加载此程序集吗?

稍后编辑

我尝试将此类移动TestDataELabel.Manager.ViewModels程序集(构造函数所在的程序集).果然,它现在工作正常,我可以在编辑器中查看控件时看到测试数据.好奇.

我已经仔细检查过ELabelViewModel类和构造函数是否公开(当然是这样,否则我永远无法构建应用程序).

小智 1

我像这样实现所有视图模型类:

<UserControl x:Class="MyApp.Views.MainView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:vm="clr-namespace:MyApp.ViewModel"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" Height="607" Width="616">

    <UserControl.DataContext>
        <vm:TestData/>
    </UserControl.DataContext>
Run Code Online (Sandbox Code Playgroud)