Bill of Materials: mobile (mobile phone), Minicharger (mini charger), IUSB (USB interface),
Design ideas:
1. Declare the interaction between the Iusb Convention objects, which contains an event;
2.Mobile implement IUSB interface, this is the key point, is the caller implements the interface, the demand through the event entrusted to the charging device self-processing;
3.Mobile reflective charging device, injected IUSB by the constructor function;
Code Listing:
1 Public Interface Iusb {2 decimal Get Set ; } 3 Event System.EventHandler connecting; 4 }
IUsb.cs
1<%@ Page language="C #"autoeventwireup="true"codefile="Mobile.aspx.cs"inherits="Mobile"%>2 3<! DOCTYPE html>4 5"http://www.w3.org/1999/xhtml">6"Server">7<meta http-equiv="Content-type"Content="text/html; Charset=utf-8"/>8<title></title>9Ten<body> One<form id="Form1"runat="Server"> A<div> -<label> Voltage: </label><asp:label id="Lblvoltage"runat="Server"text="0"></asp:Label> -<asp:label id="lblmessage"runat="Server"text="There 's no electricity."></asp:Label> the<asp:dropdownlist id="Drpselect"runat="Server"> -<asp:listitem text="Mini Charger"Value="Minicharger"></asp:ListItem> -</asp:DropDownList> -<asp:button id="Btnconnect"runat="Server"text="connecting the charging device"onclick="Btnconnect_click"/> +</div> -</form> +</body> Amobile.aspx1 usingSystem;2 Public Partial classMobile:System.Web.UI.Page, Iusb {3 Public decimalVoltage {Get;Set; }4 Public EventEventHandler connecting;5 protected Const decimalIncrement =0.5m;6 protected Const decimalMax =5.0m;7 8 protected ObjectLoadcharger (stringPType) {9 string_filename ="z:\\"+ PType +". DLL";Ten Object[] _args =New Object[] { This }; One returnSystem.Activator.CreateInstanceFrom ( A_filename, PType,true,0,NULL, _args,NULL,NULL); - } - the protected voidBtnconnect_click (Objectsender, EventArgs e) { - This. Loadcharger ( This. DrpSelect.SelectedItem.Value); - This. ShowMessage (); - } + - protected voidShowMessage () { + if( This. connecting = =NULL) This. Lblmessage.text ="Device Not responding"; A Else { at This. Connecting (NULL, eventargs.empty); - This. Lblvoltage.text = This. Voltage.tostring (); - This. Lblmessage.text = ( This. voltage==0)?"device no charging function" -: ( This. Voltage = = Max)?"Instant Full" -:"in charge"; - } in } - to}
Mobile.aspx.cs1 usingSystem;2 [Serializable]3 Public classMinicharger {4 protectedIusb USB {Get;Set; }5 protected Const decimalVoltage =5.0m;6 PublicMinicharger (Iusb pelement) {7 This. USB =pelement;8 This. Usb.connecting + =element_connecting;9 }Ten One voidElement_connecting (Objectsender, EventArgs e) { A This. Usb.voltage =Voltage; - } -}
MiniCharger.csI'm just writing a skeleton, interested friends can implement several different types of USB devices,
And can try to extend the functions of the USB interface, such as the ability to add copy data to the phone,
See the different effects of connecting your laptop and charger
WebForm version demo, analog phone USB interface charging