Filter Control for Visual Studio LightSwitch 是MS開源的LS擴充,可以實現進階的使用者自訂查詢。可在http://code.msdn.microsoft.com/Filter-Control-for-Visual-90fb8e93下載,提供的是VB.NET的版本下載,不過也沒關係,下載後自己編繹一下即可,不過在此之前,還要下載LightSwitch Extensibility Toolkit,請到此處下載http://visualstudiogallery.msdn.microsoft.com/0dfaa2eb-3951-49e7-ade7-b9343761e1d2。該工具主要為實現LS自訂擴充提供環境.
Filter Control 的使用教程可參考http://lightswitchhelpwebsite.com/Blog/tabid/61/EntryId/22/Using-The-LightSwitch-Filter-Extension.aspx.在此就不多說了,現在我們要解決的是 Filter Control本地化的問題,在下載版本安裝到VS後,實際運行時效果如下:
大家可以看到Description, callType這二個框,這二個是資料實體的屬性,對於我們來說,要的是設計實體時所定義的DisplayName,我想使用者應該更習慣於使用中文,而不是E文吧,否則我估計軟體很難賣。,其他的本地化操作,應該不難都可以在代碼中直接找到。
對於VB.NET的文法有點瞭解,但都忘的差不多了,下面我把一開始進行調試實驗的代碼公開一下,寫的比較爛。(沒有LS的技術手冊,對於對象運行時結構一無所知)
開啟FilterControl.Client項目,找到FieldDefinition.vb,我們要做的修改就在這裡。
Private Sub New(ByVal propDef As IEntityPropertyDefinition, ByVal currentDepth As Int32, ByVal maxDepth As Int32) 在此構造中定位到原始處理代碼中,如下:
'get displayName
'Replace this with appropriate call in RTM
' Me.DisplayName = propDef.Name 這個地方就是處理查詢條件顯示名稱的地方,請注釋掉
下面是調試代碼,不知VB.NET的Linq文法怎麼寫,,只能來個迴圈了。
For Each o As Microsoft.LightSwitch.Model.IAttribute In propDef.Attributes
MessageBox.Show(o.Class.ToString())
If o.Class.Name.ToLower = "displayname" Then
MessageBox.Show("Found DisplayName")
Dim dn As IDisplayNameAttribute
dn = DirectCast(o, Microsoft.LightSwitch.Model.IDisplayNameAttribute)
If dn Is Nothing Then
MessageBox.Show("Not DisplayName")
Me.DisplayName = propDef.Name
Else
MessageBox.Show(dn.Value)
Me.DisplayName = dn.Value
End If
End If
Next
通過觀察對象運行實際情況判斷出IDisplayNameAttribute,有了這個就好辦了,這就是我們的中文名。功能基本達到,但通過反編譯C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\LightSwitch\1.0\Client\Microsoft.LightSwitch.dll,才知道有更好的辦法,一句代碼就可以解決該問題,全部代碼如下:
Private Sub New(ByVal propDef As IEntityPropertyDefinition, ByVal currentDepth As Int32, ByVal maxDepth As Int32) currentDepth += 1 Me.ShortName = propDef.Name If TypeOf (propDef.PropertyType) Is ISemanticType Then TypeName = DirectCast(propDef.PropertyType, ISemanticType).UnderlyingType.Name ElseIf TypeOf (propDef.PropertyType) Is INullableType AndAlso TypeOf (DirectCast(propDef.PropertyType, INullableType).UnderlyingType) Is ISemanticType Then TypeName = DirectCast(DirectCast(propDef.PropertyType, INullableType).UnderlyingType, ISemanticType).UnderlyingType.GetNullableType.Name ElseIf TypeOf (propDef.PropertyType) Is ISimpleType Then TypeName = DirectCast(propDef.PropertyType, ISimpleType).Name ElseIf TypeOf (propDef.PropertyType) Is IEntityType Then TypeName = "Entity" End If 'Dim tp As ISimpleType = TryCast(propDef.PropertyType, ISimpleType) 'If tp IsNot Nothing Then ' TypeName = tp.Name 'Else ' TypeName = "Entity" 'End If ' MessageBox.Show(Microsoft.LightSwitch.Model.Extensions.ModelExtensions.GetDefaultDisplayName(propDef)) 'get displayName 'Replace this with appropriate call in RTM,下面代碼可取得顯示名稱 Me.DisplayName = Microsoft.LightSwitch.Model.Extensions.ModelExtensions.GetDefaultDisplayName(propDef) If currentDepth <= maxDepth AndAlso TypeOf propDef.PropertyType Is IEntityType Then 'navigation property Dim navPropType As IEntityType = propDef.PropertyType For Each p As IEntityPropertyDefinition In navPropType.Properties 'Ignore any hidden or computed fields If p.Attributes.Where(Function(a) a.Class.Name = "Hidden").FirstOrDefault Is Nothing AndAlso p.Attributes.Where(Function(a) a.Class.Name = "Computed").FirstOrDefault Is Nothing Then If Not TypeOf p.PropertyType Is ISequenceType Then 'ignore collections for now Me.AddChild(New FieldDefinition(p, currentDepth, maxDepth)) End If End If Next End If End Sub
其他本地化的工作還沒有做,等全部完了後,再提供完整漢化版本吧