类型初始化器在uwp中使用MVVM抛出异常
本文关键字:MVVM 抛出异常 uwp 初始化 类型 | 更新日期: 2023-09-27 18:16:13
下面是使用MVVM概念开发一个简单的UWP项目的代码。我正在努力解决在 Locator
类中的 AddEngineerviewmodel
静态属性上抛出的 System.TypeInitializationException
。
内部异常指向View.xaml中的DataContext行:
无法找到具有名称/键primary [Line: 10 Position: 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");
}
}
}
ViewModel:
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}/>
如何改变
<Page>
Datacontext={Binding Source={StaticResources loc},Path=EngineerViewModel}
</Page>
<Page Datacontext={Binding Source={StaticResource loc},Path=EngineerViewModel}>
</Page>