gpt4 book ai didi

c# - 为什么我的用户控件会导致 Visual Studio 崩溃?

转载 作者:行者123 更新时间:2023-11-30 21:05:14 25 4
gpt4 key购买 nike

我花了一整天的时间试图弄清楚为什么这个用户控件会导致 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();
}
}
}
}

最佳答案

我已经能够重现此崩溃,诚然使用非电话版本的 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 崩溃),删除了项目的 binobj文件夹并重新启动 Visual Web Dev Express。当我重新启动 VWDX 时,它提示找不到类型 Tile , 但那是因为我删除了 binobj文件夹。重建解决了这个问题。

我只能猜测究竟是什么问题。在 Tile 点正在静态初始化类,Application.Current可能是 null , Application.Current.Resources可能是 null , 或 Application.Current.Resources["PhoneAccentColor"]可能是 null (这会导致转换为 Color 失败,因为 Colorstruct )。可能 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:Keyx:Name识别他们。 VS 支持使用 x:Name代替 x:Key , 但它以 legacy support mechanism 的形式存在仅。

  • 使用 x:Name在用户控件的元素中 XAML 导致 VS 在 InitializeComponent() 中创建一个具有该名称的字段在您的 Tile 的自动生成部分类(在 obj\Debug 中的 Tile.g.cs 中)。然而,就因为你能坚持x:Name在一个元素上并不一定意味着您将能够访问生成字段中的相应对象。因为没有名为 SwitchSidesAnimation 的 UIElement在你的Tile.xaml ( Storyboard不是 UIElements),SwitchSidesAnimation字段将始终为 null .

    的确,MSDN documentation for the x:Key attribute (也链接到上面)提到

    A FindName call using a key value will not retrieve a keyed resource

    (FindName 是用于按名称查找控件的方法。如果您查看 Tile.g.cs,您会看到它在那里使用。)

我建议始终使用 x:Key在资源字典中,这样您就不会相信您可以直接在代码隐藏中访问这个 Storyboard。

要在代码隐藏中访问 Storyboard,请使用

    this.Resources["SwitchSidesAnimation"] as Storyboard

事实上,如果您添加以下属性,则不必更改您的 startAnimations方法:

    private Storyboard SwitchSidesAnimation
{
get { return this.Resources["SwitchSidesAnimation"] as Storyboard; }
}

关于c# - 为什么我的用户控件会导致 Visual Studio 崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11814466/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com