类型初始化程序在uwp中使用MVVM抛出异常?

时间:2016-11-04 10:32:49

标签: c# xaml mvvm uwp

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

指向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}/>

1 个答案:

答案 0 :(得分:0)

如何改变

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

{{1}}
相关问题