WPF+MVVM的问题
我现在有点小疑惑了,恩,就是跨类添加数据的时候出现了一个小问题。
首先是父类的代码。
//实现功能的方法
public void addCompanyCommand()
{
AddCompanyInfo add = new AddCompanyInfo();
add.addDataEvent += new AddDataDelegate(addevenData);
add.ShowDialog();
//AddCompanyInfoViewModel addcompany = new AddCompanyInfoViewModel();
//addcompany.addDataEvent += new addDataDelegate(addevenData);
//addcompany.ShowView();
//addcompany.addData += new AddCompanyInfoViewModel.AddData(addevenData);
//add.ShowDialog();
//IBaseViewModel view = new AddCompanyInfo();
//view.Show();
}
子类的方法
namespace OptWpfApp
{
public delegate void addDataDelegate(CompanyInfoModel model);
public class AddCompanyInfoViewModel : BaseViewModel, INotifyPropertyChanged
{
public event addDataDelegate addDataEvent;
public AddCompanyInfoViewModel()
{
_addCompanyCommand = new RelayCommand(addCompanyCommand) { IsEnabled = true };
}
//public void ShowView()
//{
// AddCompanyInfo add = new AddCompanyInfo();
// add.ShowDialog();
//}
public void showViewEvent()
{
}
//实现功能的方法
public void addCompanyCommand()
{
CompanyInfoModel model = new CompanyInfoModel()
{
CompanyNo = CompanyNo,
CompanyName = CompanyName,
RegisteredAddress = RegisteredAddress,
OfficeAddress = OfficeAddress,
CorporateRepresentative = CorporateRepresentative,
RegisteredCapital = RegisteredCapital,
ScopeBusiness = ScopeBusiness,
Code = Code,
CompanyCreateTime = CompanyCreateTime,
YingYeStartTime = YingYeStartTime,
YingYeEndTime = YingYeEndTime
};
//if (addDataEvent != null)
//{
// addDataEvent(model);
//}
// addData += new AddData(model);
//if (addData != null)
//{
// addData(model);
//}
// this.CloseView();
}
private ICommand _addCompanyCommand;
public ICommand AddCompanyCommand
{
get { return _addCompanyCommand; }
}
/// <summary>
/// 公司简称代码,例如0000
/// </summary>
private string _companyNo = "";
public string CompanyNo
{
get
{
return _companyNo;
}
set
{
if (_companyNo != value)
{
_companyNo = value;
OnPropertyChanged("CompanyNo");
}
}
}
/// <summary>
/// 单位名称
/// </summary>
private string _companyName = "";
public string CompanyName
{
get
{
return _companyName;
}
set
{
if (_companyName != value)
{
_companyName = value;
OnPropertyChanged("CompanyName");
}
}
}
/// <summary>
/// 公司注册地址
/// </summary>
private string _registeredAddress = "";
public string RegisteredAddress
{
get
{
return _registeredAddress;
}
set
{
if (_registeredAddress != value)
{
_registeredAddress = value;
OnPropertyChanged("RegisteredAddress");
}
}
}
/// <summary>
/// 办公地址
/// </summary>
private string _officeAddress = "";
public string OfficeAddress
{
get
{
return _officeAddress;
}
set
{
if (_officeAddress != value)
{
_officeAddress = value;
OnPropertyChanged("OfficeAddress");
}
}
}
/// <summary>
/// 法人代表
/// </summary>
private string _corporateRepresentative = "";
public string CorporateRepresentative
{
get
{
return _corporateRepresentative;
}
set
{
if (_corporateRepresentative != value)
{
_corporateRepresentative = value;
OnPropertyChanged("_corporateRepresentative");
}
}
}
/// <summary>
/// 注册资金
/// </summary>
private decimal _registeredCapital = 0;
public decimal RegisteredCapital
{
get
{
return _registeredCapital;
}
set
{
if (_registeredCapital != value)
{
_registeredCapital = value;
OnPropertyChanged("RegisteredCapital");
}
}
}
/// <summary>
/// 经营范围
/// </summary>
private string _scopeBusiness = "";
public string ScopeBusiness
{
get
{
return _scopeBusiness;
}
set
{
if (_scopeBusiness != value)
{
_scopeBusiness = value;
OnPropertyChanged("ScopeBusiness");
}
}
}
/// <summary>
/// 组织机构代码
/// </summary>
private string _code = "";
public string Code
{
get
{
return _code;
}
set
{
if (_code != value)
{
_code = value;
OnPropertyChanged("Code");
}
}
}
/// <summary>
/// 成立日期
/// </summary>
private DateTime _companyCreateTime = DateTime.MaxValue;
public DateTime CompanyCreateTime
{
get
{
return _companyCreateTime;
}
set
{
if (_companyCreateTime != value)
{
_companyCreateTime = value;
OnPropertyChanged("CompanyCreateTime");
}
}
}
/// <summary>
/// 营业期限开始时间
/// </summary>
///
private DateTime _yingYeStartTime = DateTime.MaxValue;
public DateTime YingYeStartTime
{
get
{
return _yingYeStartTime;
}
set
{
if (_yingYeStartTime != value)
{
_yingYeStartTime = value;
OnPropertyChanged("YingYeStartTime");
}
}
}
/// <summary>
/// 营业期限结束时间
/// </summary>
private DateTime _yingYeEndTime = DateTime.MaxValue;
public DateTime YingYeEndTime
{
get
{
return _yingYeEndTime;
}
set
{
if (_yingYeEndTime != value)
{
_yingYeEndTime = value;
OnPropertyChanged("YingYeEndTime");
}
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
}
现在出现一个情况。假如我把委托卸载子类的窗体里面,也就是如下所示
namespace OptWpfApp
{
// public delegate void AddDataDelegate(CompanyInfoModel model);
/// <summary>
/// AddCompanyInfo.xaml 的交互逻辑
/// </summary>
public partial class AddCompanyInfo : WinFormsBase
{
// public event AddDataDelegate addDataEvent;
public AddCompanyInfo()
{
InitializeComponent();
this.DataContext = new AddCompanyInfoViewModel();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
//CompanyInfoModel model = new CompanyInfoModel()
//{
// CompanyNo = textBox1.Text.Trim(),
// CompanyName = textBox2.Text.Trim()
//};
//if (addDataEvent != null)
//{
// addDataEvent(model);
// this.Close();
//}
}
}
}
可以执行委托并且添加成功,但是这样就有点违背MVVM模式了,而且子类里面的构造方法有点无用了,
所以我现在就有点困惑了、
public AddCompanyInfoViewModel()
{
_addCompanyCommand = new RelayCommand(addCompanyCommand) { IsEnabled = true };
如果保存都在外面执行了。那么子类还有用吗?我感觉没用了。。。
所以我现在在想父类调用子类,然后实例化这个窗口应该进行一个怎样的传值。
也就是父窗口调用子窗口,然后子窗口添加成功之后返回一个状态给父类。。
}
[解决办法]
不是WPF没人学,是你这代码看起来太累了,半天没弄清你在说啥
[解决办法]
//if (addDataEvent != null)
//{
// addDataEvent(model);
//}
// addData += new AddData(model);
//if (addData != null)
//{
// addData(model);
//}
这一段是准备做什么呢?addData这个字段,为什么没在你的类里边看到呢?
[解决办法]
提问题不是你这样提的,你需要提炼。
[解决办法]
看来大家都一样,这问题看着眼花缭乱的,半天都没太搞明白什么情况.
你完全可以写一个单例或者静态的全局事件,进行你的跨窗体或者跨MVVM的值传递