【问题标题】:Type initializer threw Exception using MVVM in uwp?类型初始化程序在 uwp 中使用 MVVM 抛出异常?
【发布时间】:2016-11-04 10:32:48
【问题描述】:

下面是使用 MVVM 概念开发简单 UWP 项目的代码。我正在努力解决 Locator 类中的 AddEngineerviewmodel 静态属性引发的 System.TypeInitializationException

指向 View.xaml 中 DataContext 行的内部异常:

找不到具有名称/主要密钥 [行:10 位置:5]"} 的资源 ..

型号:

public class AddEngineerModel : BindableBase
{

    private string _engrName;
    public string EngrName
    {
        get { return _engrName; }
        set
        {
            _engrName = value;
            RaisePropertyChanged("EngrName");
        }
    }

    private string _phoneNo;
    public string PhoneNo
    {
        get { return _phoneNo; }
        set
        {
            _phoneNo = value;
            RaisePropertyChanged("PhoneNo");
        }
    }

    private string _email;
    public string Email
    {
        get { return _email; }
        set
        {
            _email = value;
            RaisePropertyChanged("Email");
        }
    }

    private string _address;
    public string Address
    {
        get { return _address; }
        set
        {
            _address = value;
            RaisePropertyChanged("Address");
        }
    }

    private int _salary;
    public int Salary
    {
        get { return _salary; }
        set
        {
            _salary = value;
            RaisePropertyChanged("Salary");
        }
    }
}

视图模型:

public class AddEngineerViewModel:BindableBase
{

    #region Constructor

    public AddEngineerViewModel(IAddEngineerDataService _engineerDataService, IDialogServices _dialog)
    {
        _engineerDataService = new AddEngineerDataServices(new AddEngineerData());
        _dialog = new DialogServices();
        LoadCommand();
    }


    #endregion

    #region Fields

    private static IAddEngineerDataService _engineerDataService;
    private static IDialogServices _dialog;

    private string _engrName;
    public string EngrName
    {
        get { return _engrName; }
        set
        {
            _engrName = value;
            RaisePropertyChanged("EngrName");
        }
    }

    private string _phoneNo;
    public string PhoneNo
    {
        get { return _phoneNo; }
        set
        {
            _phoneNo = value;
            RaisePropertyChanged("PhoneNo");
        }
    }

    private string _email;
    public string Email
    {
        get { return _email; }
        set
        {
            _email = value;
            RaisePropertyChanged("Email");
        }
    }

    private string _address;
    public string Address
    {
        get { return _address; }
        set
        {
            _address = value;
            RaisePropertyChanged("Address");
        }
    }

    private int _salary;
    public int Salary
    {
        get { return _salary; }
        set
        {
            _salary = value;
            RaisePropertyChanged("Salary");
        }
    }

    #endregion

    #region Methods

    #region Command

    public ICommand AddEngineerCommand { get; set; }
    public ICommand ViewEmployeeCommand { get; set; }

    private void LoadCommand()
    {
        AddEngineerCommand = new CustomCommand(Add, CanAdd);
        ViewEmployeeCommand = new CustomCommand(Views, CanView);
    }

    private void Views(object obj)
    {
        _dialog.ShowDialog();
    }

    private bool CanView(object obj)
    {
        return true;
    }

    private bool CanAdd(object obj)
    {
        return true;
    }

    private void Add(object obj)
    {
        _engineerDataService.Add_Engineer_Details(new AddEngineerModel { EngrName=_engrName,Email=_email,PhoneNo=_phoneNo,Address=_address,Salary=_salary });
    }

    #endregion

    #endregion
}

定位器:

public class Locator
{

    private static IDialogServices Dialog = new DialogServices();
    private static IAddEngineerDataService EngineerService = new AddEngineerDataServices(new AddEngineerData());
    private static ISupervisorDataServices SupervisorService = new SupervisorDataServices(new SupervisorData());


    private static AddEngineerViewModel _engineerViewModel = new AddEngineerViewModel(EngineerService, Dialog);

    public static AddEngineerViewModel EngineerViewModel
    {
        get { return _engineerViewModel; }
    }
}

app.xaml

  <Application.Resources>
    <locator:Locator x:Key="loc"/>
  </Application.Resources>

View.xaml

<Page
 Datacontext={Binding Source={StaticResources loc},Path=EngineerViewModel}/>

【问题讨论】:

  • 点击“查看详情”查看内部异常。这通常可以很好地提示问题所在。
  • 哇,一张异常的照片。那没有帮助。但是,如果您查看图片,上面有一个链接,上面写着“将异常详细信息复制到剪贴板”。现在,如果您单击该链接,然后单击 edit 链接,然后将图片替换为剪贴板中的文本,那将非常有帮助。
  • 您是否使用了第三方包?棱镜还是 Mvvmlight?
  • 不,我没有使用第三方软件包(Prism 或 Mvvmlight)@Sunteen。为什么 Sunteen 那个包对我有帮助?

标签: c# xaml mvvm uwp


【解决方案1】:

怎么改

<Page>
 Datacontext={Binding Source={StaticResources loc},Path=EngineerViewModel}
</Page>

<Page Datacontext={Binding Source={StaticResource loc},Path=EngineerViewModel}>
</Page>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-16
    • 1970-01-01
    • 1970-01-01
    • 2013-01-31
    • 2018-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多