A Silverlight Bug ?
昨日在写Silverlight程序的时候,遇到一个问题,感觉是Silverlight的Bug。使用版本是Silverlight5。异常信息如下:
行: 56
错误: Silverlight 应用程序中未处理的错误
代码: 4004
类别: ManagedRuntimeError
消息: System.ArgumentException: 值不在预期的范围内。
位于 System.Windows.Size.set_Width(Double value)
位于 System.Windows.Controls.Primitives.TabPanel.ArrangeHorizontal(Size arrangeSize)
位于 System.Windows.Controls.Primitives.TabPanel.ArrangeOverride(Size finalSize)
位于 System.Windows.FrameworkElement.ArrangeOverride(IntPtr nativeTarget, Single inWidth, Single inHeight, Single& outWidth, Single& outHeight)
给出一个示例代码:
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" x:Class="SilverlightApplication1.MainPage" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="1024"> <Grid x:Name="LayoutRoot" Background="White"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="5" /> <ColumnDefinition Width="240" /> </Grid.ColumnDefinitions> <Grid x:Name="LayoutLeft" Grid.Column="0"> <controls:TabControl x:Name="BottomContent" Grid.Column="0" Grid.Row="0"> <controls:TabItem x:Name="Item1" Header="TabItem1" Height="25"></controls:TabItem> <controls:TabItem x:Name="Item2" Header="TabItem2" Height="25"></controls:TabItem> <controls:TabItem x:Name="Item3" Header="TabItem3" Height="25"></controls:TabItem> <controls:TabItem x:Name="Item4" Header="TabItem4" Height="25"></controls:TabItem> <controls:TabItem x:Name="Item5" Header="TabItem5" Height="25"></controls:TabItem> <controls:TabItem x:Name="Item6" Header="TabItem6" Height="25"></controls:TabItem> <controls:TabItem x:Name="Item7" Header="TabItem7" Height="25"></controls:TabItem> <controls:TabItem x:Name="Item8" Header="TabItem8" Height="25"></controls:TabItem> <controls:TabItem x:Name="Item9" Header="TabItem9" Height="25"></controls:TabItem> <controls:TabItem x:Name="Item10" Header="TabItem10" Height="25"></controls:TabItem> </controls:TabControl> </Grid> <controls:GridSplitter x:Name="StretchRight" Grid.Column="1" Background="#C8E0FB" Margin="0,0,0,0" HorizontalAlignment="Stretch" /> <Grid x:Name="LayoutRight" Grid.Column="2"> <sdk:DataGrid x:Name="DgInfo" ColumnHeaderHeight="24" Margin="0" AutoGenerateColumns="False" IsReadOnly="True"> <sdk:DataGrid.Columns> <sdk:DataGridTemplateColumn Header="Column1" Width="80"></sdk:DataGridTemplateColumn> <sdk:DataGridTextColumn Header="Column2" Width="80"></sdk:DataGridTextColumn> <sdk:DataGridTextColumn Header="Column3" Width="80"></sdk:DataGridTextColumn> </sdk:DataGrid.Columns> </sdk:DataGrid> </Grid> </Grid> </UserControl>
using System.Windows.Controls; namespace SilverlightApplication1 { public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); this.InitTabItem(); } private void InitTabItem() { Item3.Visibility = System.Windows.Visibility.Collapsed; Item4.Visibility = System.Windows.Visibility.Visible; Item5.Visibility = System.Windows.Visibility.Collapsed; Item6.Visibility = System.Windows.Visibility.Collapsed; } } }
奇怪的就是InitTabItem方法的这几行代码,如果不隐藏TabItem,程序运行正常。一旦去隐藏TabItem,在拖拽GridSplitter的时候,有时会抛出异常。
请高手指点迷津。