【问题标题】:cannot create an instance in xaml in silverlight 4.0无法在 silverlight 4.0 中的 xaml 中创建实例
【发布时间】:2011-07-09 11:52:35
【问题描述】:

在 xaml 代码中,我收到一条错误消息,提示无法创建 这个 AllEmployeeViewModel 类文件,实际上当我键入 scr 时,这个类文件存在于解决方案文件夹中:intellsene 向我显示类文件

<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 天开始处理此问题

任何帮助都会很棒 谢谢。

【问题讨论】:

  • 解决方案:InitializeComponent(); if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this)) { //抛出异常的代码 }
  • 你真的需要用这个问题和你的“修复”发布代码。例如,您在非设计模式检查中究竟绕过了哪些代码?如果没有澄清,您的问题和答案都没有用。

标签: silverlight silverlight-4.0


【解决方案1】:

我也有同样的问题。 cannot create instance of viewmodel

只需复制此代码并将其放入 ViewModel 中即可

public bool IsDesignTime
{
    get
    {
      return (Application.Current == null) || 
             (Application.Current.GetType() == typeof(Application));
    }
}




//Constructor    
public ViewModelClass()
{
    if(IsDesignTime == false)
    {
       //Your Code 
    }
}

【讨论】:

    【解决方案2】:

    只需在 MainPage.xaml.cs 页面中添加这一行

    InitializeComponent();
            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                //Code that throws the exception
            }
    

    效果很好。

    【讨论】:

    • 你真的需要用你的问题和这个“修复”示例发布代码。例如,您在非设计模式检查中究竟绕过了哪些代码?如果没有澄清,您的问题和答案都没有用。
    【解决方案3】:

    你的类AllEmployeeViewModel 有零参数构造函数吗?否则,Silverlight 将无法创建您的类的实例。

    【讨论】:

      【解决方案4】:

      您已绑定到EmployeeClass。这必须是某种类型的集合才能工作,但名称 EmployeeClass 听起来像是一个对象而不是集合。

      您确实需要发布您的视图模型代码,因为我们不得不猜测这一点。

      我整理了一个简单的示例,如果 ViewModel 包含:

      public ObservableCollection<EmployeeClass> Employees { get; set; }
      

      我用一些示例 EmployeeClass 对象填充它们,

      public AllEmployeeViewModel()
      {
          this.Employees  = new ObservableCollection<EmployeeClass>();
          this.Employees.Add(new EmployeeClass() { Name = "One" });
          this.Employees.Add(new EmployeeClass() { Name = "Two" });
      

      我将绑定更改为:

      <Grid  x:Name="grdAllEmp" DataContext="{Binding Path=Employees}">
      

      看起来像这样(没有其他变化):

      【讨论】:

        【解决方案5】:

        我遇到了同样的错误,我会向你解释,希望它会对你有所帮助。 在我的 ViewModel 的构造函数中,我正在执行一些代码,所有代码都低于 if 条件,除了,

        如果(!IsInDesignMode) { // 我的代码 } // 有问题的方法执行点

        除了我每次都想执行的方法,但事实证明只有满足上述条件才能执行代码,否则将不会创建您的视图模型实例。

        所以为了避免这种情况,你必须这样做:

        如果(!IsInDesignMode) { // 我的代码 // 有问题的方法执行点 }

        将所有代码放在该条件内,一切都会好起来的。
        注意:我使用的是 MVVMLight 库Model-View-ViweModel 模式。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-12-20
          • 1970-01-01
          • 2011-05-15
          • 2023-03-31
          • 2011-08-03
          • 1970-01-01
          • 2012-06-10
          相关资源
          最近更新 更多