[UWP] [VB] [XAML]在Visual Studio 2015中自动更新XAML [英] [UWP][VB][XAML]Automatically Updating XAML in Visual Studio 2015
问题描述
我有以下类和XAML:
Public Class TestRoute
Public ReadOnly Property Cities()As WorldCityNames()
Public Property City1 ()As WorldCityNames
Get
Return Me.Cities(0)
End Get
Set(value As WorldCityNames)
Me.Cities(0)= value
结束集
结束物业
公共物业City2()As WorldCityNames
获取
返回Me.Cities(1)
结束获取
设置(值As WorldCityNames)
Me.Cities(1)= value
End Set
End Property
Public Property RouteType()As WorldPieceType
Public Property RoutePosition()As RouteLayoutPosition
Public Property Spaces()As List(Of TestSpace)
Public ReadOnly Property RouteLength()As Integer
Get
Return Me.Spaces.Count
End Get
End Property
Public ReadOnly Property SpacesWithData()As IEnumerable(Of Object)
Get
Return Me.Spaces.Select(Function(s)New With {
.RenderTransformOrigin = Me.GetRenderTransformOrigin(),
.OuterMargin = Me.GetOuterMargin(s.Center),
.Rotation = s.Rotation,
.CornerRadius = Me。 GetCornerRadius(),
.InnerMargin = Me.GetInnerMargin()})
End Get
End Property
Public Sub New()
Me.New({WorldCityNames。 None,WorldCityNames.None},RouteLayoutPosition.Top,WorldPieceType.Train,New List(Of TestSpace))
End Sub
Public Sub New(city1 As WorldCityNames,city2 As WorldCityNames,rtposition As RouteLayoutPosition,rttype As WorldPieceType,rtspaces As List(Of TestSpace))
Me.New({city1,city2},rtposition,rttype,rtspaces)
End Sub
Public Sub New(rtcities As WorldCityNames(), rtposition As RouteLayoutPosition,rttype As WorldPieceType,rtspaces As List(Of TestSpace))
Me.Cities = rtcities
Me.RoutePosition = rtposition
Me.RouteType = rttype
Me.Spaces = rtspaces
End Sub
私人函数GetRen derTransformOrigin()As Point
选择Case Me.RoutePosition
Case RouteLayoutPosition.Top:返回新点(0.5,0.875)
Case RouteLayoutPosition.Middle:返回新点(0.5,0.5)
Case Else:返回新点(0.5,0.125)
结束选择
结束功能
私人功能GetOuterMargin(中心为点)作为厚度
选择Case Me .RoutePosition
Case RouteLayoutPosition.Top:返回新的厚度(center.X - App.PieceWidth / 2,center.Y - 3.5 * App.PieceHeight,0,0)
Case RouteLayoutPosition.Middle:Return New厚度(center.X - App.PieceWidth / 2,center.Y - 2 * App.PieceHeight,0,0)
Case Else:返回新厚度(center.X - App.PieceWidth / 2,center.Y - 0.5 * App.PieceHeight,0,0)
结束选择
结束函数
私函数GetCornerRadius()As CornerRadius
返回If(Me.RouteType = WorldPieceType .Train,New CornerRadius(0),New CornerRadius(8))
结束功能
私有函数GetInnerMargin()As Thickness
选择Case Me.RoutePosition
Case RouteLayoutPosition.Top:返回新的厚度(0,27,0,0)
Case RouteLayoutPosition.Middle:返回新的厚度( 0,13.5,0,13.5)
Case Else:返回新的厚度(0,0,27,0)
结束选择
结束功能
结束等级
Public Class TestSpace
Public Property Center()As Point
Public Property Rotation()As Double
Public Sub New()
Me.New(New Point(0,0),0)
End Sub
Public Sub New(c As Point,rotate As Double)
Me.Center = c
Me.Rotation = rotate
End Sub
End Class
< Viewbox控件>
< Grid Background =" Green" VerticalAlignment = QUOT;中心"的HorizontalAlignment = QUOT;中心">
< ItemsControl>
< ItemsControl.ItemsPanel>< ItemsPanelTemplate>< Grid VerticalAlignment =" Top"的HorizontalAlignment = QUOT;左" />< / ItemsPanelTemplate>< /ItemsControl.ItemsPanel>
< ItemsControl.ItemTemplate>
< DataTemplate>
< ItemsControl ItemsSource =" {Binding SpacesWithData}">
< ItemsControl.ItemsPanel>< ItemsPanelTemplate>< Grid HorizontalAlignment =" Left" VerticalAlignment = QUOT;顶部和QUOT; />< / ItemsPanelTemplate>< /ItemsControl.ItemsPanel>
< ItemsControl.ItemTemplate>
< DataTemplate>
< Border HorizontalAlignment =" Left" VerticalAlignment = QUOT;陀螺" RenderTransformOrigin =" {Binding RenderTransformOrigin}" Margin =" {Binding OuterMargin}">
< Border.RenderTransform>< RotateTransform Angle =" {Binding Rotation}" />< /Border.RenderTransform>
< Border Width =" 26"高度= QUOT; 9"背景= QUOT;银及QUOT; CornerRadius =" {Binding CornerRadius}" Margin =" {Binding InnerMargin}" />
< / Border>
< / DataTemplate>
< /ItemsControl.ItemTemplate>
< / ItemsControl>
< / DataTemplate>
< /ItemsControl.ItemTemplate>
< ItemsControl.Items>
< ctrl:TestRoute City1 =" AlQahira"城2 ="卡萨布兰卡" RouteType = QUOT;火车和QUOT; RoutePosition = QUOT;中">
< ctrl:TestRoute.Spaces>
< ctrl:TestSpace Center =" 30,30"旋转= QUOT; 0" />
< ctrl:TestSpace Center =" 50,100"旋转= QUOT; 30英寸; />
< ctrl:TestSpace Center =" 100,150"旋转= QUOT; 60 QUOT; />
< / ctrl:TestRoute.Spaces>
< / ctrl:TestRoute>
< ctrl:TestRoute City1 =" AlQahira"城2 ="卡萨布兰卡" RouteType = QUOT;船舶及QUOT; RoutePosition = QUOT;底座">
< ctrl:TestRoute.Spaces>
< ctrl:TestSpace Center =" 130,60"旋转= QUOT; 32" />
< ctrl:TestSpace Center =" 50,100"旋转= QUOT; 130" />
< ctrl:TestSpace Center =" 300,150"旋转= QUOT; 60 QUOT; />
< / ctrl:TestRoute.Spaces>
< / ctrl:TestRoute>
< /ItemsControl.Items>
< / ItemsControl>
< / Grid>
< / Viewbox>
您会注意到ItemsControl的Items属性(ItemsControl.Items)具有由以下内容组成的文字数据TestRoute& TestSpace类。另请注意,文字数据是将值分配给Spaces属性(ctrl:TestRoute.Spaces),但内部
ItemsControl的ItemsSource为{Binding SpacesWithData},如果您查看TestRoute类,则属性为返回匿名类型。所有这一切都很好,但除非我重建应用程序,否则设计师不会更新。有没有办法让
Visual Studio中的设计器在更改数据时自动更新(ItemsControl.Items)?谢谢。
Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/
< blockquote>嗨Nathan,
您可以设置
DataContext 并使用数据绑定
ItemsControl.ItemsSource 而不是使用
ItemsControl.Items 。对于数据源,您可以使用Blend for Visual Studio"从类创建示例数据"。您可以在"数据"面板上找到该命令。如果您只是想在设计时显示该数据源,那么您可以使用设计时属性。
欲了解更多信息,请参阅以下链接。
https://msdn.microsoft.com/en-us/windows/uwp/data-binding/displaying-data-in-the-designer? f = 255& MSPPError = -2147217396
https://blogs.msdn.microsoft.com/lucian/2015/05/21/win10-apps-in-net-design-time-data -in-xaml /
$
最好的问候,
David
>
I have the following classes and XAML:
Public Class TestRoute Public ReadOnly Property Cities() As WorldCityNames() Public Property City1() As WorldCityNames Get Return Me.Cities(0) End Get Set(value As WorldCityNames) Me.Cities(0) = value End Set End Property Public Property City2() As WorldCityNames Get Return Me.Cities(1) End Get Set(value As WorldCityNames) Me.Cities(1) = value End Set End Property Public Property RouteType() As WorldPieceType Public Property RoutePosition() As RouteLayoutPosition Public Property Spaces() As List(Of TestSpace) Public ReadOnly Property RouteLength() As Integer Get Return Me.Spaces.Count End Get End Property Public ReadOnly Property SpacesWithData() As IEnumerable(Of Object) Get Return Me.Spaces.Select(Function(s) New With { .RenderTransformOrigin = Me.GetRenderTransformOrigin(), .OuterMargin = Me.GetOuterMargin(s.Center), .Rotation = s.Rotation, .CornerRadius = Me.GetCornerRadius(), .InnerMargin = Me.GetInnerMargin()}) End Get End Property Public Sub New() Me.New({WorldCityNames.None, WorldCityNames.None}, RouteLayoutPosition.Top, WorldPieceType.Train, New List(Of TestSpace)) End Sub Public Sub New(city1 As WorldCityNames, city2 As WorldCityNames, rtposition As RouteLayoutPosition, rttype As WorldPieceType, rtspaces As List(Of TestSpace)) Me.New({city1, city2}, rtposition, rttype, rtspaces) End Sub Public Sub New(rtcities As WorldCityNames(), rtposition As RouteLayoutPosition, rttype As WorldPieceType, rtspaces As List(Of TestSpace)) Me.Cities = rtcities Me.RoutePosition = rtposition Me.RouteType = rttype Me.Spaces = rtspaces End Sub Private Function GetRenderTransformOrigin() As Point Select Case Me.RoutePosition Case RouteLayoutPosition.Top : Return New Point(0.5, 0.875) Case RouteLayoutPosition.Middle : Return New Point(0.5, 0.5) Case Else : Return New Point(0.5, 0.125) End Select End Function Private Function GetOuterMargin(center As Point) As Thickness Select Case Me.RoutePosition Case RouteLayoutPosition.Top : Return New Thickness(center.X - App.PieceWidth / 2, center.Y - 3.5 * App.PieceHeight, 0, 0) Case RouteLayoutPosition.Middle : Return New Thickness(center.X - App.PieceWidth / 2, center.Y - 2 * App.PieceHeight, 0, 0) Case Else : Return New Thickness(center.X - App.PieceWidth / 2, center.Y - 0.5 * App.PieceHeight, 0, 0) End Select End Function Private Function GetCornerRadius() As CornerRadius Return If(Me.RouteType = WorldPieceType.Train, New CornerRadius(0), New CornerRadius(8)) End Function Private Function GetInnerMargin() As Thickness Select Case Me.RoutePosition Case RouteLayoutPosition.Top : Return New Thickness(0, 27, 0, 0) Case RouteLayoutPosition.Middle : Return New Thickness(0, 13.5, 0, 13.5) Case Else : Return New Thickness(0, 0, 27, 0) End Select End Function End Class
Public Class TestSpace Public Property Center() As Point Public Property Rotation() As Double Public Sub New() Me.New(New Point(0, 0), 0) End Sub Public Sub New(c As Point, rotate As Double) Me.Center = c Me.Rotation = rotate End Sub End Class
<Viewbox> <Grid Background="Green" VerticalAlignment="Center" HorizontalAlignment="Center"> <ItemsControl> <ItemsControl.ItemsPanel><ItemsPanelTemplate><Grid VerticalAlignment="Top" HorizontalAlignment="Left"/></ItemsPanelTemplate></ItemsControl.ItemsPanel> <ItemsControl.ItemTemplate> <DataTemplate> <ItemsControl ItemsSource="{Binding SpacesWithData}"> <ItemsControl.ItemsPanel><ItemsPanelTemplate><Grid HorizontalAlignment="Left" VerticalAlignment="Top"/></ItemsPanelTemplate></ItemsControl.ItemsPanel> <ItemsControl.ItemTemplate> <DataTemplate> <Border HorizontalAlignment="Left" VerticalAlignment="Top" RenderTransformOrigin="{Binding RenderTransformOrigin}" Margin="{Binding OuterMargin}"> <Border.RenderTransform><RotateTransform Angle="{Binding Rotation}"/></Border.RenderTransform> <Border Width="26" Height="9" Background="Silver" CornerRadius="{Binding CornerRadius}" Margin="{Binding InnerMargin}"/> </Border> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> </DataTemplate> </ItemsControl.ItemTemplate> <ItemsControl.Items> <ctrl:TestRoute City1="AlQahira" City2="Casablanca" RouteType="Train" RoutePosition="Middle"> <ctrl:TestRoute.Spaces> <ctrl:TestSpace Center="30,30" Rotation="0"/> <ctrl:TestSpace Center="50,100" Rotation="30"/> <ctrl:TestSpace Center="100,150" Rotation="60"/> </ctrl:TestRoute.Spaces> </ctrl:TestRoute> <ctrl:TestRoute City1="AlQahira" City2="Casablanca" RouteType="Ship" RoutePosition="Bottom"> <ctrl:TestRoute.Spaces> <ctrl:TestSpace Center="130,60" Rotation="32"/> <ctrl:TestSpace Center="50,100" Rotation="130"/> <ctrl:TestSpace Center="300,150" Rotation="60"/> </ctrl:TestRoute.Spaces> </ctrl:TestRoute> </ItemsControl.Items> </ItemsControl> </Grid> </Viewbox>
You will notice that the Items property (ItemsControl.Items) of the ItemsControl has literal data made up of the TestRoute & TestSpace classes. Also note that the literal data is assigning values to the Spaces property (ctrl:TestRoute.Spaces), but the inner
ItemsControl has an ItemsSource of {Binding SpacesWithData}, which if you look at the TestRoute class is a property that returns an anonymous type. All of this works great, but the designer is not updated unless I rebuild the app. Is there any way to have
the Designer in Visual Studio automatically update when I change the data (ItemsControl.Items)? Thanks.
Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/
Hi Nathan,
You may set DataContext and use data binding for the ItemsControl.ItemsSource instead of using ItemsControl.Items directly. For the data source, you may use Blend for Visual Studio to "create sample data from class". You could find that command on Data panel. If you just want that data source shown at design time, then you could use design-time attributes. For more information, please refer to the following links.
https://msdn.microsoft.com/en-us/windows/uwp/data-binding/displaying-data-in-the-designer?f=255&MSPPError=-2147217396
https://blogs.msdn.microsoft.com/lucian/2015/05/21/win10-apps-in-net-design-time-data-in-xaml/
Best Regards,
David
这篇关于[UWP] [VB] [XAML]在Visual Studio 2015中自动更新XAML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!