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
其他本地化的工作还没有做,等全部完了后,再提供完整汉化版本吧