无法在silverlight 4.0中的xaml中创建实例
在xaml代码中,我得到一个错误,告诉我无法创建的实例 这是AllEmployeeViewModel类文件,实际上,当我键入scr时,该类文件存在于解决方案文件夹中:intellsene显示该类文件无法在silverlight 4.0中的xaml中创建实例,silverlight,silverlight-4.0,Silverlight,Silverlight 4.0,在xaml代码中,我得到一个错误,告诉我无法创建的实例 这是AllEmployeeViewModel类文件,实际上,当我键入scr时,该类文件存在于解决方案文件夹中:intellsene显示该类文件 <UserControl.Resources> <scr:AllEmployeeViewModel x:Key="empName"></scr:AllEmployeeViewModel> </UserControl.Resources> &l
<UserControl.Resources>
<scr:AllEmployeeViewModel x:Key="empName"></scr:AllEmployeeViewModel>
</UserControl.Resources>
<Grid x:Name="MainGrid" Background="White" Width="400"
Height="407" DataContext="{Binding Source={StaticResource empName}}" >
<Grid x:Name="grdAllEmp" DataContext="{Binding Path=EmployeeClass}">
<sdk:DataGrid AutoGenerateColumns="True" Height="274"
HorizontalAlignment="Left" Margin="8,8,0,0"
Name="dgEmployee" VerticalAlignment="Top" Width="385"
ItemsSource="{Binding}"/>
<Button Content="Get All Employees" Height="23"
HorizontalAlignment="Left" Margin="12,288,0,0"
Name="btnAllEmplloyees" VerticalAlignment="Top" Width="381"
Command="{Binding Path=DataContext.GetEmployees,ElementName=MainGrid}"/>
</Grid>
我试图将数据绑定到网格,如果我忽略编译时错误并运行它,则会给出一个找不到的错误键
请让我知道解决方案,如果你知道,在过去的2天里一直在处理这个问题
任何帮助都会很好
谢谢。您的类
AllEmployeeViewModel
是否有零参数构造函数?没有它,Silverlight就无法创建类的实例。您的类AllEmployeeViewModel
是否具有零参数构造函数?否则,Silverlight无法创建类的实例。只需在MainPage.xaml.cs页面中添加这一行即可
InitializeComponent();
if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
{
//Code that throws the exception
}
它工作正常。只需在MainPage.xaml.cs页面中添加这一行即可
InitializeComponent();
if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
{
//Code that throws the exception
}
它工作正常。您有一个到
EmployeeClass
的绑定。这必须是某种类型的集合才能起作用,但名称EmployeeClass
听起来像是一个单独的对象,而不是集合
您确实需要发布视图模型代码,因为我们不得不猜测这一点
我总结了一个快速示例,如果ViewModel包含:
public ObservableCollection<EmployeeClass> Employees { get; set; }
publicobservableCollection雇员{get;set;}
我用几个示例EmployeeClass对象填充它们
public AllEmployeeViewModel()
{
this.Employees = new ObservableCollection<EmployeeClass>();
this.Employees.Add(new EmployeeClass() { Name = "One" });
this.Employees.Add(new EmployeeClass() { Name = "Two" });
public AllEmployeeViewModel()
{
this.Employees=新的ObservableCollection();
Add(newEmployeeClass(){Name=“One”});
Add(newEmployeeClass(){Name=“Two”});
我将绑定更改为:
<Grid x:Name="grdAllEmp" DataContext="{Binding Path=Employees}">
看起来是这样的(没有其他更改):
您有一个到
EmployeeClass
的绑定。它必须是某种类型的集合才能工作,但名称EmployeeClass
听起来像一个单一对象,而不是集合
您确实需要发布视图模型代码,因为我们不得不猜测这一点
我总结了一个快速示例,如果ViewModel包含:
public ObservableCollection<EmployeeClass> Employees { get; set; }
publicobservableCollection雇员{get;set;}
我用几个示例EmployeeClass对象填充它们
public AllEmployeeViewModel()
{
this.Employees = new ObservableCollection<EmployeeClass>();
this.Employees.Add(new EmployeeClass() { Name = "One" });
this.Employees.Add(new EmployeeClass() { Name = "Two" });
public AllEmployeeViewModel()
{
this.Employees=新的ObservableCollection();
Add(newEmployeeClass(){Name=“One”});
Add(newEmployeeClass(){Name=“Two”});
我将绑定更改为:
<Grid x:Name="grdAllEmp" DataContext="{Binding Path=Employees}">
看起来是这样的(没有其他更改):
我也有同样的问题。
无法创建viewmodel的实例
只需复制此代码并将其放置在ViewModel中
public bool IsDesignTime
{
get
{
return (Application.Current == null) ||
(Application.Current.GetType() == typeof(Application));
}
}
//Constructor
public ViewModelClass()
{
if(IsDesignTime == false)
{
//Your Code
}
}
我也有同样的问题。
无法创建viewmodel的实例
只需复制此代码并将其放置在ViewModel中
public bool IsDesignTime
{
get
{
return (Application.Current == null) ||
(Application.Current.GetType() == typeof(Application));
}
}
//Constructor
public ViewModelClass()
{
if(IsDesignTime == false)
{
//Your Code
}
}
我得到了同样的错误,我会解释给你,希望它能帮助你。 在我的视图模型的构造函数中,我正在执行一些代码,所有代码都在if条件下,除了 If(!IsInDesignMode) { // my code } // Problamatic method execution point 如果(!IsInDesignMode) { //我的代码 } //问题方法执行点 除了一个我每次都想执行的方法之外,但事实证明,只有在满足上述条件的情况下才能执行代码,否则将不会创建视图模型实例 因此,为了避免这种情况,您必须这样做: If(!IsInDesignMode) { // my code // Problamatic method execution point } 如果(!IsInDesignMode) { //我的代码 //问题方法执行点 } 把你所有的代码都放在这个条件下,一切都会好起来的。
注意:我使用的是MVVMLight库以及模型视图和模型模式。我也遇到了同样的错误,我会向您解释,希望它能帮助您。 在我的视图模型的构造函数中,我正在执行一些代码,所有代码都在if条件下,除了 If(!IsInDesignMode) { // my code } // Problamatic method execution point 如果(!IsInDesignMode) { //我的代码 } //问题方法执行点 除了一个我每次都想执行的方法之外,但事实证明,只有在满足上述条件的情况下才能执行代码,否则将不会创建视图模型实例 因此,为了避免这种情况,您必须这样做: If(!IsInDesignMode) { // my code // Problamatic method execution point } 如果(!IsInDesignMode) { //我的代码 //问题方法执行点 } 把你所有的代码都放在这个条件下,一切都会好起来的。
注意:我使用的是MVVMLight库以及模型视图ViweModel模式。解决方案:InitializeComponent();if(!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this)){//引发异常的代码}您确实需要发布带有此问题和“修复”的代码。例如,您在“非设计模式”检查中绕过了哪些代码?如果不进行澄清,您的问题和答案都将无效。解决方案:InitializeComponent();if(!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this)){//引发异常的代码}您确实需要发布带有此问题和“修复”的代码。例如,您在非设计模式检查中绕过了哪些代码?没有澄清,您的问题和答案都没有用处。您确实需要发布带有问题和此示例的代码“修复”。例如,您在非设计模式检查中绕过了哪些代码?如果没有澄清,您的问题和答案都没有用。您确实需要在问题和此“修复”示例中发布代码。例如,您在非设计模式检查中绕过了哪些代码?如果没有澄清,您的问题和答案都没有用