我花了一整天时间试图弄清楚这个用户控件崩溃的原因VS2010(Windows Phone 7.1开发)。应用程序运行此控件没有问题,但是当我在MainPage.xaml中进入设计模式时 - VS崩溃。
<UserControl x:Class="blabla.Controls.Tile"
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"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}">
<UserControl.Resources>
<Storyboard x:Name="SwitchSidesAnimation">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Projection).(PlaneProjection.RotationX)" Storyboard.TargetName="FrontSide">
<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.2" Value="90"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.4" Value="90"/>
<EasingDoubleKeyFrame x:Name="MoveThisForPause1" KeyTime="0:0:6" Value="-90"/>
<EasingDoubleKeyFrame x:Name="MoveThisForPause2" KeyTime="0:0:6.2" Value="-90"/>
<EasingDoubleKeyFrame x:Name="MoveThisForPause3" KeyTime="0:0:6.4" Value="0"/>
<EasingDoubleKeyFrame x:Name="MoveThisForPause4" KeyTime="0:0:12" Value="0"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Projection).(PlaneProjection.RotationX)" Storyboard.TargetName="BackSide">
<EasingDoubleKeyFrame KeyTime="0" Value="-90"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.2" Value="-90"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.4" Value="0"/>
<EasingDoubleKeyFrame x:Name="MoveThisForPause5" KeyTime="0:0:6" Value="0"/>
<EasingDoubleKeyFrame x:Name="MoveThisForPause6" KeyTime="0:0:6.2" Value="90"/>
<EasingDoubleKeyFrame x:Name="MoveThisForPause7" KeyTime="0:0:6.4" Value="90"/>
<EasingDoubleKeyFrame x:Name="MoveThisForPause8" KeyTime="0:0:12" Value="90"/>
</DoubleAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="FrontSide">
<DiscreteObjectKeyFrame KeyTime="0:0:0.2">
<DiscreteObjectKeyFrame.Value>
<Visibility>Visible</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
<DiscreteObjectKeyFrame KeyTime="0:0:0.4">
<DiscreteObjectKeyFrame.Value>
<Visibility>Collapsed</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
<DiscreteObjectKeyFrame x:Name="MoveThisForPause9" KeyTime="0:0:6">
<DiscreteObjectKeyFrame.Value>
<Visibility>Visible</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
<DiscreteObjectKeyFrame x:Name="MoveThisForPause10" KeyTime="0:0:6.4">
<DiscreteObjectKeyFrame.Value>
<Visibility>Visible</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
<DiscreteObjectKeyFrame x:Name="MoveThisForPause11" KeyTime="0:0:12">
<DiscreteObjectKeyFrame.Value>
<Visibility>Visible</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.TranslateX)" Storyboard.TargetName="FrontSide">
<EasingDoubleKeyFrame KeyTime="0:0:0.4" Value="0"/>
<EasingDoubleKeyFrame x:Name="MoveThisForPause12" KeyTime="0:0:6" Value="0"/>
<EasingDoubleKeyFrame x:Name="MoveThisForPause13" KeyTime="0:0:6.2" Value="0"/>
<EasingDoubleKeyFrame x:Name="MoveThisForPause14" KeyTime="0:0:6.4" Value="0"/>
<EasingDoubleKeyFrame x:Name="MoveThisForPause15" KeyTime="0:0:12" Value="0"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</UserControl.Resources>
<Grid x:Name="LayoutRoot">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="1*" />
</Grid.RowDefinitions>
<!-- Front side -->
<Grid x:Name="FrontSide" Grid.Column="0" Grid.Row="0"
Background="{Binding FrontBackground}">
<Image Source="{Binding FrontImage}" />
</Grid>
<!-- /Front side -->
<!-- Back side -->
<Grid x:Name="BackSide" Grid.Column="0" Grid.Row="0"
Background="{Binding BackBackground}">
<Grid.Projection>
<PlaneProjection RotationX="-90" />
</Grid.Projection>
<Image Source="{Binding BackImage}" />
</Grid>
<!-- /Back side -->
</Grid>
</UserControl>
现在代码。
namespace blabla.Controls
{
public partial class Tile : UserControl
{
/// <summary>
/// Defines if the tile has two sides.
/// </summary>
public bool IsTwoSided
{
get { return (bool)GetValue(IsTwoSidedProperty); }
set
{
SetValue(IsTwoSidedProperty, value);
this.startAnimations();
}
}
/// <summary>
/// Image that will be displayed on front side.
/// </summary>
public BitmapImage FrontImage
{
get { return (BitmapImage)GetValue(FrontImageProperty); }
set { SetValue(FrontImageProperty, value); }
}
/// <summary>
/// Image that ill be displayed on back side.
/// </summary>
public BitmapImage BackImage
{
get { return (BitmapImage)GetValue(BackImageProperty); }
set { SetValue(BackImageProperty, value); }
}
/// <summary>
/// Brush that will be used as background for front side.
/// </summary>
public Brush FrontBackground
{
get { return (Brush)GetValue(FrontBackgroundProperty); }
set { SetValue(FrontBackgroundProperty, value); }
}
/// <summary>
/// Brush that will be used as background for back side.
/// </summary>
public Brush BackBackground
{
get { return (Brush)GetValue(BackBackgroundProperty); }
set { SetValue(BackBackgroundProperty, value); }
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
public static readonly DependencyProperty IsTwoSidedProperty =
DependencyProperty.Register("IsTwoSided", typeof(bool), typeof(Tile), new PropertyMetadata(false));
public static readonly DependencyProperty FrontImageProperty =
DependencyProperty.Register("FrontImage", typeof(BitmapImage), typeof(Tile), new PropertyMetadata(null));
public static readonly DependencyProperty BackImageProperty =
DependencyProperty.Register("BackImage", typeof(BitmapImage), typeof(Tile), new PropertyMetadata(null));
public static readonly DependencyProperty FrontBackgroundProperty =
DependencyProperty.Register("FrontBackground", typeof(Brush), typeof(Tile),
new PropertyMetadata(new SolidColorBrush((Color)Application.Current.Resources["PhoneAccentColor"])));
public static readonly DependencyProperty BackBackgroundProperty =
DependencyProperty.Register("BackBackground", typeof(Brush), typeof(Tile),
new PropertyMetadata(new SolidColorBrush((Color)Application.Current.Resources["PhoneAccentColor"])));
///////////////////////////////////////////////////////////////////////////////////////////////////////
public Tile()
{
InitializeComponent();
this.LayoutRoot.DataContext = this;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Modifies animation frames' KeyTime to adjust time for new timing.
/// <param name="pauseTime">Lenght of the pause.</param>
/// </summary>
private void setPauses(TimeSpan pauseTime)
{
// Sets pauses.
EasingDoubleKeyFrame frameToModify;
for(int i = 0; true; i++)
{
if(this.FindName("MoveThisForPause" + i) != null)
{
frameToModify = (EasingDoubleKeyFrame)this.FindName("MoveThisForPause" + i);
frameToModify.KeyTime = KeyTime.FromTimeSpan(frameToModify.KeyTime.TimeSpan - TimeSpan.FromSeconds(5) + pauseTime);
}
else
{
break;
}
}
}
/// <summary>
/// Starts animations.
/// </summary>
/// <param name="beginTime">Usually delay before first-time animation.</param>
private void startAnimations()
{
// We start animations only if the tile is two sided.
if(this.IsTwoSided)
{
// Stopping previous animation.
this.SwitchSidesAnimation.Stop();
// Sets correct pauses.
this.setPauses(TimeSpan.FromSeconds(new Random().Next(5, 10)));
// Starts animation.
this.SwitchSidesAnimation.BeginTime = TimeSpan.FromSeconds(new Random().Next(2, 15));
this.SwitchSidesAnimation.RepeatBehavior = RepeatBehavior.Forever;
this.SwitchSidesAnimation.Begin();
}
else
{
this.SwitchSidesAnimation.Stop();
}
}
}
}
答案 0 :(得分:4)
我已经能够重现这次崩溃,无可否认地使用Silverlight的非手机版本,以及Visual Web Dev Express而不是VS的完整版本。
问题最终归结为这两个依赖项属性声明中指定的默认值:
public static readonly DependencyProperty FrontBackgroundProperty =
DependencyProperty.Register("FrontBackground", typeof(Brush), typeof(Tile),
new PropertyMetadata(new SolidColorBrush((Color)Application.Current.Resources["PhoneAccentColor"])));
public static readonly DependencyProperty BackBackgroundProperty =
DependencyProperty.Register("BackBackground", typeof(Brush), typeof(Tile),
new PropertyMetadata(new SolidColorBrush((Color)Application.Current.Resources["PhoneAccentColor"])));
在我用null
替换默认值(使用Notepad ++作为Visual Web Dev Express崩溃)后,崩溃消失了,删除了项目的bin
和obj
文件夹并重新启动了Visual Web开发快递。当我重新启动VWDX时,它抱怨找不到类型Tile
,但那是因为我删除了bin
和obj
文件夹。重建将其整理出来。
我只能猜出问题到底是什么。在Tile
类被静态初始化时,Application.Current
可能是null
,Application.Current.Resources
可能是null
,或Application.Current.Resources["PhoneAccentColor"]
可能是null
{1}}(这会导致转化为Color
失败,因为Color
是struct
)。也许VS设计器不能处理在类型的静态初始化期间抛出的非常好的异常?
顺便说一句,我还想指出另外几个潜在的问题。首先,这是您的IsTwoSided
属性:
/// <summary>
/// Defines if the tile has two sides.
/// </summary>
public bool IsTwoSided
{
get { return (bool)GetValue(IsTwoSidedProperty); }
set
{
SetValue(IsTwoSidedProperty, value);
this.startAnimations();
}
}
看起来您希望在startAnimations
依赖项属性更改时调用IsTwoSided
方法。你上面写的代码不会实现这一点。
当Silverlight更改依赖项属性的值时,它不会调用属性setter来执行此操作。如果您希望在依赖项属性的值发生更改时发生事情,请改为使用property-changed callback。
其次,在Tile.xaml
中,您在Storyboard
中声明<UserControl.Resources>
,如下所示:
<Storyboard x:Name="SwitchSidesAnimation">
我建议使用x:Key
代替x:Name
,原因有两个:
资源词典中的所有项目(隐式样式除外)必须有x:Key
或x:Name
才能识别它们。 VS支持使用x:Name
代替x:Key
,但仅支持legacy support mechanism。
在用户控件XAML中的元素中使用x:Name
会导致VS在InitializeComponent()
类的自动生成部分Tile
中创建具有该名称的字段(在Tile.g.cs
obj \ Debug中的某个地方)。但是,仅仅因为您可以在元素上粘贴x:Name
并不一定意味着您将能够访问生成的字段中的相应对象。由于SwitchSidesAnimation
中没有名为Tile.xaml
的UIElement(故事板不是UIElements),因此SwitchSidesAnimation
字段始终为null
。
确实,MSDN documentation for the x:Key attribute(也与上述相关)提及
使用键值的FindName调用不会检索键控资源
(FindName
是用于按名称查找控件的方法。如果您查看Tile.g.cs
,您会看到它在那里使用。)
我建议您始终在资源词典中使用x:Key
,这样您就不会相信您可以直接在代码隐藏中访问此Storyboard。
要在代码隐藏中访问故事板,请使用
this.Resources["SwitchSidesAnimation"] as Storyboard
事实上,如果您添加以下属性,则无需更改startAnimations
方法:
private Storyboard SwitchSidesAnimation
{
get { return this.Resources["SwitchSidesAnimation"] as Storyboard; }
}
答案 1 :(得分:0)
我有类似的问题(在开发Silverlight 5时),这花了我差不多3天的时间来挣扎,并且肯定会更多,但幸运的是我在这里找到了解决方案(由Luke Woodward提供)。
就我而言,我正在使用:
public static readonly DependencyProperty MyStyleProperty =
DependencyProperty.Register(
"MyStyle",
typeof(Style),
typeof(MainButton),
new PropertyMetadata(
// using this construct as a default value
// makes VS 2010 SP1 to crush!
Application.Current.Resources["SomeStyle"] as Style,
OnPropertyChanged
)
);
因此,两个问题的共同点是使用一些资源值作为DependencyProperty的默认值。
但更悲惨的是,这个问题发生在我为VS 2010应用SP1之后(因为我想在Silverlight 5中开发,需要SP1 for VS 2010)。
这给我带来了头痛和大量的搜索时间。
幸运的是现在它已经解决了,谢谢!