Visual Studio2013提供的Snippet,
包含了許多的項目,
12個項目列示如下,
1.ASP.NET MVC4
2.My Code程式碼片段
3.Office程式碼片段
4.Windows Form應用程式
5.Windows系統-記錄,處理序,登錄,服務
6.WPF
7.其他-連接,安全性,工作流程
8.基本-集合,資料類型,檔案系統,數學
9.應用程式-編譯,資源和設定
10.測試
11.程式碼模式-If,For Each,Try Catch,Property等
依照Visusl Studio2013程式片段管理員中,
列示的項目順序,
把提供的Snippet詳細內容列示在下方,
雖然跟使用Snippet右鍵時,
列示的項目順序不同,
不過可以先查閱一下內容
6.WPF
6.1加入DependencyProperty登錄: 加入要登錄讀寫的 DependencyProperty 的程式碼,而且必須將此程式碼片段加入衍生自 DependencyObject 的類別。
Public Property Prop1 As String
Get
Return
GetValue(Prop1Property)
End Get
Set(ByVal value As String)
SetValue(Prop1Property, value)
End Set
End Property
Public Shared ReadOnly Prop1Property As
DependencyProperty = _
DependencyProperty.Register("Prop1", _
GetType(String), GetType(Window1), _
New PropertyMetadata(Nothing))
|
6.2加入DependencyProperty登錄(附加): 加入要登錄附加的
DependencyProperty 的程式碼,而且必須將此程式碼片段加入衍生自
DependencyObject 的類別。
Public Shared Function GetProp1(ByVal element As DependencyObject)
As String
If element Is Nothing Then
Throw New
ArgumentNullException("element")
End If
Return
element.GetValue(Prop1Property)
End Function
Public Shared Sub SetProp1(ByVal element As DependencyObject,
ByVal value As String)
If element Is Nothing Then
Throw New
ArgumentNullException("element")
End If
element.SetValue(Prop1Property, value)
End Sub
Public Shared ReadOnly Prop1Property As _
DependencyProperty =
DependencyProperty.RegisterAttached("Prop1", _
GetType(String), GetType(Window1), New PropertyMetadata(Nothing))
|
6.3加入DependencyProperty登錄(唯讀): 加入要登錄唯讀的
DependencyProperty 的程式碼,而且必須將此程式碼片段加入衍生自
DependencyObject 的類別。
Public ReadOnly Property Prop1 As String
Get
Return
GetValue(Window1.Prop1Property)
End Get
End Property
Private Shared ReadOnly Prop1PropertyKey As
DependencyPropertyKey = _
DependencyProperty.RegisterReadOnly("Prop1", _
GetType(String), GetType(Window1), _
New PropertyMetadata(Nothing))
Public Shared ReadOnly Prop1Property As DependencyProperty
= _
Prop1PropertyKey.DependencyProperty
|
6.4加入RoutedCommand CanExecute和Excuted處理常式: 加入 CanExecute 和 Executed 處理常式。若要產生作用,必須將這些處理常式攔截到
CommandBinding。
Private Sub
OnCanExecuteCommand1(ByVal sender As Object, ByVal e As CanExecuteRoutedEventArgs)
' Add actions to
ensure that the command can execute.
' Remove this
exception after the appropriate code is added.
Throw New
NotImplementedException()
'' Example action:
' If condition
Then
' ' The condition for the command was met.
' e.CanExecute =
True
' e.Handled = True
' End If
End Sub
Private Sub
OnExecuteCommand1(ByVal sender As Object, _
ByVal e As
ExecutedRoutedEventArgs)
' Perform the
actions associated with the command.
' Remove this
exception after the appropriate code is added.
Throw New NotImplementedException()
' Work was done
for this command. Mark the event as handled.
e.Handled = True
End Sub
|
6.5加入RoutedCommand登錄: 加入要登錄 RoutedCommand 的程式碼。
Public Shared ReadOnly Routed1Command As RoutedCommand = _
New RoutedCommand("Routed1", _
GetType(Window1))
|
6.6加入RoutedEvent登錄: 加入要登錄 RoutedEvent 的程式碼,而且必須將此程式碼片段加入衍生自 UIElement 的類別。
Public Custom Event Event1 As
RoutedEventHandler
AddHandler(ByVal value As
RoutedEventHandler)
Me.AddHandler(Event1Event,
value)
End AddHandler
RemoveHandler(ByVal value As
RoutedEventHandler)
Me.RemoveHandler(Event1Event,
value)
End RemoveHandler
RaiseEvent(ByVal sender As Object, ByVal e As RoutedEventArgs)
Me.RaiseEvent(e)
End RaiseEvent
End Event
Public Shared ReadOnly Event1Event As RoutedEvent = _
EventManager.RegisterRoutedEvent("Event1", _
RoutingStrategy.Bubble, _
GetType(RoutedEventHandler),
GetType(Window1))
|
7.其他-連接,安全性,工作流程
7.1工作流程
7.1.1新增相依性屬性,EventHandler: 用於建立 Windows 工作流程活動中之相依性屬性的程式碼片段。
Public Shared MyEventEvent As
DependencyProperty = _
DependencyProperty.Register("MyEvent", GetType(EventHandler), _
GetType(ActivityClassName))
<Description("MyEvent")> _
<Category("MyEvent
Category")> _
<Browsable(True)> _
<DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)>
_
Public Custom Event MyEvent As EventHandler
AddHandler(ByVal value As EventHandler)
MyBase.AddHandler(ActivityClassName.MyEventEvent,
value)
End AddHandler
RemoveHandler(ByVal value As EventHandler)
MyBase.RemoveHandler(ActivityClassName.MyEventEvent,
value)
End RemoveHandler
RaiseEvent(ByVal sender As Object, ByVal e As System.EventArgs)
End RaiseEvent
End Event
|
7.1.2新增相依性屬性,屬性: 用於建立 Windows 工作流程活動中之相依性屬性的程式碼片段。
Public Shared
MyPropertyProperty As DependencyProperty = _ DependencyProperty.Register("MyProperty", GetType(String), _ GetType(ActivityClassName))
<Description("MyProperty")> _
<Category("MyProperty
Category")> _
<Browsable(True)> _
<DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)>
_
Public Property MyProperty As String
Get
Return (CType((MyBase.GetValue(ActivityClassName.
_
MyPropertyProperty)), String))
End Get
Set(ByVal Value As String)
MyBase.SetValue(ActivityClassName.MyPropertyProperty,
value)
End Set
End Property
|
7.2安全性
7.2.1加密字串: 使用 Rijndael 加密演算法加密檔案中的文字。
Using fStream = File.Open("encrypted.txt",
FileMode.OpenOrCreate)
Dim RijndaelAlg As Rijndael = Rijndael.Create
Using cStream As New
CryptoStream(fStream, _
RijndaelAlg.CreateEncryptor(RijndaelAlg.Key,
RijndaelAlg.IV), _
CryptoStreamMode.Write)
Using sWriter As New
StreamWriter(cStream)
sWriter.WriteLine("Text
to encrypt")
End Using
End Using
End Using
|
7.2.2計算字串的雜湊碼: 使用 MD5、SHA1 或 SHA384 演算法計算字串的雜湊。
Enum HashMethod
MD5
SHA1
SHA384
End Enum
Function
GenerateHashDigest(ByVal source As String, ByVal algorithm As HashMethod) As String
Dim hashAlgorithm As HashAlgorithm = Nothing
Select Case algorithm
Case HashMethod.MD5
hashAlgorithm = New
MD5CryptoServiceProvider
Case HashMethod.SHA1
hashAlgorithm = New
SHA1CryptoServiceProvider
Case HashMethod.SHA384
hashAlgorithm = New SHA384Managed
Case Else
' Error case.
End Select
Dim byteValue = Encoding.UTF8.GetBytes(source)
Dim hashValue = hashAlgorithm.ComputeHash(byteValue)
Return Convert.ToBase64String(hashValue)
End Function
|
7.2.3計算密碼的雜湊碼: 計算密碼的 SHA1 雜湊碼。
Dim sha1CryptoService
As New
SHA1CryptoServiceProvider()
Dim byteValue =
Encoding.UTF8.GetBytes("passwordString")
Dim hashValue = sha1CryptoService.ComputeHash(byteValue)
|
7.2.4產生密碼編譯隨機資料: 產生密碼編譯隨機資料。
Dim randomNumGen As
RandomNumberGenerator = RNGCryptoServiceProvider.Create()
Dim randomBytes(8) As Byte
randomNumGen.GetBytes(randomBytes)
|
7.2.5尋找目前使用者的名稱: 擷取目前使用者的名稱。
Dim userName = My.User.Name
|
7.2.6解密檔案中的文字: 使用 Rijndael 加密演算法解密檔案中的文字。
Using fStream =
File.Open("encrypted.txt", FileMode.OpenOrCreate)
Dim RijndaelAlg As Rijndael = Rijndael.Create
Using cStream As New
CryptoStream(fStream,
RijndaelAlg.CreateDecryptor(RijndaelAlg.Key,
RijndaelAlg.IV),
CryptoStreamMode.Read)
Using sReader As New
StreamReader(cStream)
Dim plainText = sReader.ReadLine()
End Using
End Using
End Using
|
7.3連接和網路
7.3.1Ping另一部電腦: 判斷指定的網站是否有回應。
Dim siteResponds = My.Computer.Network.Ping("somehost.my.domain")
|
7.3.2由序列埠讀取資料: 由序列埠讀取資料。
Dim buffer As New StringBuilder
Using comPort =
My.Computer.Ports.OpenSerialPort("COM1")
Do
Dim line = comPort.ReadLine()
If line Is Nothing Then
Exit Do
Else
buffer.AppendLine(line)
End If
Loop
End Using
|
7.3.3列舉序列埠: 顯示如何使用 My 物件列舉序列埠。
For Each portName In
My.Computer.Ports.SerialPortNames
Next
|
7.3.4判斷網路是否可用: 判斷本機電腦上的網路連接是否可用。
Dim isAvailable = My.Computer.Network.IsAvailable
|
7.3.5使用HTTP下載檔案: 使用 HTTP 下載檔案。
My.Computer.Network.DownloadFile("http://www.URLtoDownloadFrom.net",
"C:\filename.html")
|
7.3.6使用My.Computer.Network上傳檔案: 將本機檔案上傳到網際網路。
My.Computer.Network.UploadFile("c:\File.txt",
"http://www.someserver.com/upload.aspx")
|
7.3.7使用SerialPort撥出電話號碼: 使用 SerialPort 撥出電話號碼。
Using comPort =
My.Computer.Ports.OpenSerialPort("COM1", 2400)
comPort.DtrEnable = True
comPort.Write("ATDT 206-555-1000"
& vbCrLf)
' All data
transfer code goes here.
End Using
|
7.3.8建立電子郵件訊息: 使用 MailMessage 類別傳送電子郵件訊息。
Dim message As New MailMessage("sender@address", "from@address", "Subject", "Message
Text")
Dim emailClient As New SmtpClient("Email Server
Name")
emailClient.Send(message)
|
7.3.9將相對URI轉換為絕對URI: 將相對 URI 轉換為絕對
URI。
Dim baseUri As New Uri("http://www.contoso.com/")
Dim relativeUri As New Uri("images/index.htm?id=null")
' Compose absolute
Uri using the base and the relative Uri.
Dim absoluteUri As New Uri(baseUri,
relativeUri)
Dim absolute =
absoluteUri.AbsolutePath
|
相關貼文
VB.Net開發-使用Visual Studio2013提供之Snippet程式碼片段
VB.Net Snippet,利用工具軟體Snippet Editor,自訂常用的程式碼片段
VB.Net開發-變數格式速查
沒有留言:
張貼留言