/// <summary> /// 保存文件 /// </summary> /// <param name="savePath"></param> /// <param name="name"></param> /// <param name="modelType"></param> /// <param name="model"></param> public bool GenerateFile(string savePath, string name, Type modelType, UIVOBase model) { try { var content = Engine.Razor.Run(name, modelType, model); File.WriteAllText(savePath, content, System.Text.Encoding.UTF8); return(true); } catch { //出错 return(false); } }
public override bool CreateFiles(ref string msg, string saveDir, List <TableMeta> tables, GenerateFileDelegate generateFileFunc, UIVOBase vo) { msg = string.Empty; UIVO viewModel = vo as UIVO; for (int i = 0; i < tables.Count; i++) { vo.CurrentTable = tables[i]; string file = Path.Combine(saveDir, viewModel.ClassPre + vo.CurrentTable.UpperCamelName + ".cs"); if (!generateFileFunc(file, TEMPLATE_NAME, typeof(UIVO), viewModel)) { continue; } } return(true); }
/// <summary> /// 生成代码 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void GenerateCode_Click(object sender, RoutedEventArgs e) { if (this.ViewModel.TemplateSelectIndex >= 0) { TemplateBase template = this.ViewModel.Templates[this.ViewModel.TemplateSelectIndex]; if (template == null || template.Vars == null) { return; } //必须保存文件 if (template.MustChoiceSaveDir) { if (string.IsNullOrEmpty(this.ViewModel.SaveDir)) { MessageBox.Show("保存路径不能为空"); return; } if (!System.IO.Directory.Exists(this.ViewModel.SaveDir)) { MessageBox.Show("保存路径不存在"); return; } } //必须选择表 if (template.MustChoiceTables) { if (this.ViewModel.GetSelectedTables().Count <= 0) { MessageBox.Show("必须要选择数据表"); return; } } UIVOBase vo = Activator.CreateInstance(template.ViewModelType) as UIVOBase; if (vo == null) { MessageBox.Show("生成失败"); return; } string key = string.Empty; string val = string.Empty; //页面值 for (int i = 0; i < template.Vars.Count; i++) { CWControlBase uc = this.FindName(template.Vars[i].VarName) as CWControlBase; if (uc == null) { template.Vars[i].VarData = null; continue; } PropertyInfo property = template.ViewModelType.GetProperty(template.Vars[i].VarName); if (property == null) { continue; } val = uc.GetValue() == null?string.Empty :uc.GetValue().ToString(); key = $"{template.Name}.{template.Vars[i].VarName}"; ApplicationGlobal.Instance.States.SetValue(key, val); property.SetValue(vo, uc.GetValue(), null); } string msg = string.Empty; if (template.CreateFiles(ref msg, this.ViewModel.SaveDir, this.ViewModel.GetSelectedTables(), ApplicationGlobal.Instance.TemplateProvider.GenerateFile, vo)) { MessageBox.Show("生成成功"); } else { MessageBox.Show(msg); } } }