public partial classMainWindow: Window
{
public MainWindow(){
InitializeComponent();
this.DataContext = new MainViewModel();}
private void Window_MouseMove(object sender, MouseEventArgs e){if(e.LeftButton == MouseButtonState.Pressed)
this.DragMove();}
private void Button_Click(object sender, RoutedEventArgs e){
this.Close();}}
public classMainViewModel: NotifyPropertyBase
{
private UIElement _mainContent;
public UIElement MainContent
{
get {return _mainContent;}set{
Set<UIElement>(ref _mainContent, value);}}
public CommandBase TabChangedCommand { get;set;}
public MainViewModel(){
TabChangedCommand = new CommandBase(OnTabChanged);
OnTabChanged("Zhaoxi.Industrial.View.SystemMonitor");}
private void OnTabChanged(object obj){if(obj == null)return;// 完整方式
//string[] strValues = o.ToString().Split('|');//Assembly assembly = Assembly.LoadFrom(strValues[0]);//Type type= assembly.GetType(strValues[1]);//this.MainContent =(UIElement)Activator.CreateInstance(type);// 简化方式,必须在同一个程序集下
Type type= Type.GetType(obj.ToString());
this.MainContent =(UIElement)Activator.CreateInstance(type);}}
public classNotifyPropertyBase: INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged([CallerMemberName] string propName =""){
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));}
public void Set<T>(ref T field, T value,[CallerMemberName] string propName =""){if(EqualityComparer<T>.Default.Equals(field, value))return;
field = value;
RaisePropertyChanged(propName);}}
public classCommandBase: ICommand
{
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter){return true;}
public void Execute(object parameter){
this.DoExecute?.Invoke(parameter);}
public Action<object> DoExecute { get;set;}
public CommandBase(){}
public CommandBase(Action<object> action){
DoExecute = action;}}