[英]“Method not found” error in XAML designer
当我在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
这一切都没有问题。 但是,当我在XAML中添加时会出现问题:
d:DataContext="{x:Static local:TestData.ELabelViewModel}"
XAML编辑器在我的d:DataContext
属性下面放了一条蓝色的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
的独立程序ELabel.Manager.ViewModels
。 编辑器无法完全加载此程序集吗?
稍后编辑
我尝试将此TestData
类移动到ELabel.Manager.ViewModels
程序集(构造函数所在的程序集)。 果然,它现在工作正常,我可以在编辑器中查看控件时看到测试数据。 好奇。
我已经仔细检查了ELabelViewModel
类和构造函数是否公开(当然是这样,否则我永远无法构建应用程序)。
我实现了所有我的viewmodel类,如下所示:
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.