Silverlight practice example 6 (and the use of set attributes)-client view model layer (VM)

Source: Internet
Author: User
Tags silverlight

3) view model layer dynamicdataviewmodel. CS

Using system;
Using system. net;
Using system. windows;
Using system. Windows. controls;
Using system. Windows. documents;
Using system. Windows. Ink;
Using system. Windows. input;
Using system. Windows. Media;
Using system. Windows. Media. animation;
Using system. Windows. shapes;
Using mappstructure. datasource;
Using system. componentmodel;
Using system. Collections. Generic;
Using mentities;
Namespace mappstructure. viewmodel
{
/// <Summary>
/// View model layer. Note that the inotifypropertychanged interface must be implemented so that the VM layer can be bound.
/// Viewmodel can be used as a base class. This base class mainly implements the inotifypropertychanged interface.
/// And some public methods you need. For simplicity, no base class is implemented.
/// </Summary>
Public class dynamicdataviewmodel: inotifypropertychanged
{
/// <Summary>
/// The model layer service instance used in the view model layer. Note that
/// You can call multiple service instances at the model layer. Here is the demonstration, so only one instance is used.
/// </Summary>
Private dynamicdatasource thes;
Public dynamicdataviewmodel ()
{
Thes = new dynamicdatasource ();
// Initialize and register the command.
Commands = new dictionary <string, mycommand> ();
Commands. Add ("button1command", new mycommand (onbuttoncommand ));
}
Private void onbuttoncommand (object parameter)
{
Loaddata ();
MessageBox. Show ("OK ");
}

/// <Summary>
/// Command dictionary set. The first advantage is that it can reduce the hard code for defining commands and provide a dynamic
/// The possibility of State commands, and is conducive to expansion.
/// </Summary>
Public dictionary <string, mycommand> commands
{
Get;
Private set;
}
Private list <dynamicdatarow> _ datasource;
Public list <dynamicdatarow> datasource
{
Get
{
Return _ datasource;
}
Private set
{
_ Datasource = value;
Raisepropertychanged ("datasource ");

}
}
Private dynamicdatatable _ datatable;
/// <Summary>
/// Obtain the dynamic data table.
/// </Summary>
Public dynamicdatatable datatable
{
Get
{
Return _ datatable;
}
Private set
{
_ Datatable = value;
Raisepropertychanged ("datatable ");

}
}
/// <Summary>
/// Data loading.
/// </Summary>
Private void loaddata ()
{
Thes. getdynamicdatatable ("select * From employeeinfo", op =>
{
If (op. haserror = false)
{
Datasource = op. value. Rows;
Datatable = op. value;
}
Else
{
MessageBox. Show (op. errormsg );
}
}, Null );
}
Public event propertychangedeventhandler propertychanged;
Protected void raisepropertychanged (string propertyname)
{
VaR handler = propertychanged;
If (handler! = NULL)
{
Handler (this, new propertychangedeventargs (propertyname ));
}
}
}
/// <Summary>
/// Your own command class, mainly for command binding. Here is a typical command mode, and the command receiver is the local VM.
/// The command receiver does not act as a member of the command, but uses the delegate method, which is more convenient in this case.
/// </Summary>
Public class mycommand: icommand
{
Public bool canexecute (object parameter)
{
Return true;
}
Private action <object >_action;
Public mycommand (Action <Object> action)
{
This. _ Action = action;
}
Public event eventhandler canexecutechanged;

Public void execute (object parameter)
{
If (_ Action! = NULL)
{
_ Action (parameter );
}
}
}
}
Note that my commands are not defined in a general way, but in a dictionary set. The benefits are described above. Please pay attention to the page binding method.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.