我的表单中有两个 WPF PropertyGrid,需要为每个 PropertyGrid 创建两个具有不同名称的 xml 文件。我在互联网上寻找,但一无所获。是否可以创建两个xml文件?
我的第一个 PropertyGrid 课程:
public class Tube {}
public class OvalTube : Tube {}
public class Dome : Tube {}
对于第二个 PropertyGrid:
public class Intersection {}
public class TubeIntersection : Intersection {}
public class Macro : Intersection {}
并且对于每个 PropertyGrid 一个 xml 文件。
保存xml的代码:
private Type tube = typeof(Tube);
private Type[] intersection = { typeof(Intersection)};
public bool SaveXml(Tube m_tube, Intersection m_intersecion, string m_fileName)
{
FileStream fs = new FileStream(m_fileName, FileMode.Create);
XmlSerializer serializer = new XmlSerializer(this.tube, this.intersection);
serializer.Serialize(fs, m_tube);
fs.Close();
return true;
}
但我无法创建两个 xml 文件。
当我这样做时:
public bool SaveXml(Tube m_tube, Intersection m_intersection, string m_fileName)
{
using (StreamWriter writer = new StreamWriter(m_fileName))
{
try
{
XmlSerializer serializer = new XmlSerializer(typeof(Tube));
serializer.Serialize(writer, m_tube);
serializer.Serialize(writer, m_intersection);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
writer.Flush();
writer.Close();
}
}
return true;
}
我有错误代码。
为 Tube 初始化 PropertyGrid
namespace Prototyp1_1.GUI.ViewModel
{
public class PartViewModel : ViewModelBase
{
private Tube m_tube;
private TubeType m_tubeType;
public PartViewModel()
{
m_tubeType = TubeType.Not_Specified;
SetTubeTypes();
}
public TubeType _TubeType
{
get { return m_tubeType; }
set
{
if (value == m_tubeType)
return;
m_tubeType = value;
OnTubeTypeChanged();
base.OnPropertyChanged("_TubeType");
}
}
public List<TubeType> TubeTypes { get; set; }
public Tube _Tube
{
get { return m_tube; }
set
{
m_tube = value;
base.OnPropertyChanged("_Tube");
}
}
public void OnTubeTypeChanged()
{
switch (m_tubeType)
{
case TubeType.BentTube:
_Tube = new BentTube();
break;
case TubeType.Dome:
_Tube = new Dome();
break;
case TubeType.OvalTube:
_Tube = new OvalTube();
break;
case TubeType.RectangularTube:
_Tube = new RectangularTube();
break;
case TubeType.RoundTube:
_Tube = new RoundTube();
break;
}
MainViewModel.GetInstance().IntersectionToolbar._TubeType = m_tubeType;
}
private void SetTubeTypes()
{
Array values = Enum.GetValues((typeof(TubeType)));
TubeTypes = new List<TubeType>();
foreach (TubeType value in values)
{
TubeTypes.Add(value);
}
TubeTypes.Remove(TubeType.Not_Specified);
}
}
}
交叉点的 PropertyGrid:
namespace Prototyp1_1.GUI.ViewModel
{
public class IntersectionViewModel : ViewModelBase
{
Intersection _intersection;
IntersectionRepository _intersectionRepository;
ObservableCollection<CommandViewModel> _intersections;
ICollectionView _intersectionView;
bool _isSelected;
public IntersectionViewModel()
{
_intersectionRepository = new IntersectionRepository();
_intersectionView = CollectionViewSource.GetDefaultView(_intersectionRepository._Intersections);
_IntersectionView.CurrentChanged += IntersectionSelectionChanged;
}
public IntersectionViewModel(Intersection intersection, IntersectionRepository intersectionRepository)
{
if (intersection == null)
throw new ArgumentNullException("intersection");
if (intersectionRepository == null)
throw new ArgumentNullException("intersectionRepository");
_intersection = intersection;
_intersectionRepository = intersectionRepository;
}
public bool IsSelected
{
get { return _isSelected; }
set
{
if (value == _isSelected)
return;
_isSelected = value;
base.OnPropertyChanged("IsSelected");
}
}
public Intersection _Intersection
{
get { return _intersection; }
set
{
if (value == null)
return;
_intersection = value;
OnPropertyChanged("_Intersection");
}
}
public IntersectionType _IntersectionType
{
get { return _intersection._IntersectionType; }
set
{
if (value == _intersection._IntersectionType)
return;
_intersection._IntersectionType = value;
base.OnPropertyChanged("Intersection");
}
}
public IntersectionRepository _IntersectionRepository
{
get { return _intersectionRepository; }
}
public ICollectionView _IntersectionView
{
get { return _intersectionView; }
}
public ObservableCollection<CommandViewModel> _Intersections
{
set
{
_intersections = value;
base.OnPropertyChanged("Commands");
}
get
{
if (_intersections == null)
{
_intersections = new ObservableCollection<CommandViewModel>();
}
return _intersections;
}
}
public void addIntersectionListItem(IntersectionListItem intersection)
{
_Intersection = intersection._Intersection;
_IntersectionRepository.AddIntersection(intersection);
_IntersectionView.Refresh(); // update the view
}
public void onIntersectionClicked()
{
object o = _IntersectionView.CurrentItem;
}
void IntersectionSelectionChanged(object sender, EventArgs e)
{
IntersectionListItem i = (IntersectionListItem)_IntersectionView.CurrentItem;
if(i != null)
_Intersection = i._Intersection;
}
}
}
初始化 PartViewModell 和 IntersectionModellView 类及调用方法:
public class MainViewModel : ViewModelBase
{
IntersectionToolbarViewModel m_intersectionToolbar;
PartViewModel m_partView;
string _projectFolder;
private static MainViewModel instance = null;
public MainViewModel()
{
base.DisplayName = Strings.MainViewModel_DisplayName;
_projectFolder = "";
}
public static MainViewModel GetInstance()
{
if (instance == null)
{
instance = new MainViewModel();
}
return instance;
}
List<CommandViewModel> CreateCommands()
{
return new List<CommandViewModel>
{
new CommandViewModel(
LocalizedStrings.Instance.getLocalizedCommands("MainViewModel_Command_Save"),
new ActionCommand(param => this.Save())),
};
}
public PartViewModel PartView
{
get
{
if (m_partView == null)
{
m_partView = new PartViewModel();
}
return m_partView;
}
}
public IntersectionViewModel IntersectionView
{
get
{
if (m_intersectionView == null)
{
m_intersectionView = new IntersectionViewModel();
}
return m_intersectionView;
}
}
void Save()
{
PartSerializer part = new PartSerializer();
Microsoft.Win32.SaveFileDialog saveDialog = new Microsoft.Win32.SaveFileDialog();
saveDialog.InitialDirectory = @"C:\";
saveDialog.Filter = "Text documents (.xml)|*.xml";
if (saveDialog.ShowDialog() == true)
{
_projectFolder = saveDialog.FileName;
part.SaveXml(PartView._Tube, IntersectionView._Intersection, saveDialog.FileName);
}
}
}