Vb.Net定义泛型方法并调用,示例代码为UWP存储设置的VB实现方式.
定义(和C#相比多了一个of 关键字)
Imports Windows.Storage
Public Class AppSetting
'写入存储信息
Public Shared Sub WriteSetting(Of T)(ByVal key As String, ByVal Tvalue As T)
Dim root As ApplicationDataContainer = ApplicationData.Current.LocalSettings
Dim oldkey As Object = Nothing
If root.Values.TryGetValue(key, oldkey) Then
root.Values(key) = Tvalue
Else
root.Values.Add(key, Tvalue)
End If
End Sub
'读取存储信息
Public Shared Function ReadSetting(Of T)(ByVal key As String) As T
Dim root As ApplicationDataContainer = ApplicationData.Current.LocalSettings
Dim value As Object
If root.Values.TryGetValue(key, value) Then
Return CType(value, T)
Else
Return Nothing
End If
End Function
'有条件的写入存储信息
Public Shared Sub WriteSettingIfBigger(Of T)(ByVal key As String, ByVal Tvalue As T)
Dim root As ApplicationDataContainer = ApplicationData.Current.LocalSettings
Dim oldvalue As Object = Nothing
If root.Values.TryGetValue(key, oldvalue) Then
If Convert.ToDouble(oldvalue) < Convert.ToDouble(Tvalue) Then
root.Values(key) = Tvalue
End If
Else
root.Values.Add(key, Tvalue)
End If
End Sub
End Class
调用
AppSetting.WriteSetting(Of Double)("Music Volume", e.NewValue)
menu_background_music.Volume = AppSetting.ReadSetting(Of Double)("Music Volume")