WebForm版demo,類比手機Usb介面充電

來源:互聯網
上載者:User

標籤:

材料清單:Mobile(手機),MiniCharger(迷你充電器),IUsb(USB介面),

設計思路:

1.聲明IUsb約定對象之間的互動方式,其中包含一個事件;

2.Mobile實現IUsb介面,這是關鍵點,是調用者實現介面,需求通過事件委託給充電裝置自行處理;

3.Mobile反射充電裝置,通過建構函式注入IUsb;

代碼清單:

1 public interface IUsb {2     decimal Voltage { 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 <html xmlns="http://www.w3.org/1999/xhtml"> 6 <head runat="server"> 7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 8     <title></title> 9 </head>10 <body>11     <form id="form1" runat="server">12     <div>13         <label>電壓:</label><asp:Label ID="lblVoltage" runat="server" Text="0" ></asp:Label>14         <asp:Label ID="lblMessage" runat="server" Text="沒電了" ></asp:Label>15         <asp:DropDownList ID="drpSelect" runat="server">16             <asp:ListItem Text="迷你充電器" Value="MiniCharger"></asp:ListItem>17             </asp:DropDownList>18         <asp:Button ID="btnConnect" runat="server" Text="串連充電裝置" OnClick="btnConnect_Click" />19     </div>20     </form>21 </body>22 </html>
Mobile.aspx
 1 using System; 2 public partial class Mobile : System.Web.UI.Page, IUsb { 3     public decimal Voltage { get; set; } 4     public event EventHandler Connecting; 5     protected const decimal Increment = 0.5m; 6     protected const decimal Max = 5.0m; 7  8     protected object LoadCharger(string pType) { 9         string _fileName = "Z:\\" + pType + ".dll";10         object[] _args = new object[] { this };11         return System.Activator.CreateInstanceFrom(12             _fileName, pType, true, 0, null, _args, null, null);13         }14 15     protected void btnConnect_Click(object sender, EventArgs e) {16         this.LoadCharger(this.drpSelect.SelectedItem.Value);17         this.ShowMessage();18         }19 20     protected void ShowMessage() {21         if (this.Connecting == null) this.lblMessage.Text = "裝置無響應";22         else {23             this.Connecting(null, EventArgs.Empty);24             this.lblVoltage.Text = this.Voltage.ToString();25             this.lblMessage.Text = (this.Voltage==0)?"裝置無充電功能"26                 : (this.Voltage == Max) ? "瞬間充滿" 27                 :"充電中";28             }29         }30 31     }
Mobile.aspx.cs
 1 using System; 2 [Serializable] 3 public class MiniCharger { 4     protected IUsb Usb { get; set; } 5     protected const decimal Voltage = 5.0m; 6     public MiniCharger(IUsb pElement) { 7         this.Usb = pElement; 8         this.Usb.Connecting += Element_Connecting; 9         }10 11     void Element_Connecting(object sender, EventArgs e) {12         this.Usb.Voltage = Voltage;13         }14     }
MiniCharger.cs

我只是寫個骨架,有興趣的朋友可以多實現幾個不同類型的Usb裝置,
並且可以嘗試擴充Usb介面的功能,比如說為手機增加拷貝資料的功能,

看看串連膝上型電腦和充電器的不同效果

 

WebForm版demo,類比手機Usb介面充電

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.