为什么访问我的故事板X:名称工作在Silverlight但不是在WPF?

StackOverflow https://stackoverflow.com/questions/681054

  •  22-08-2019
  •  | 
  •  

下面的WPF代码获取错误:<强>“zipButtonOut”名称不在当前上下文中存在

然而,相同的代码工作在Silverlight为我展示这里: HTTP:/ /tanguay.info/web/index.php?pg=codeExamples&id=65

我有什么做的WPF代码能够Window.Resources内访问故事板吗我试了一下在WPF用户控件很好,但得到了同样的错误。

<强> XAML:

<Window x:Class="TestDataGrid566.Test1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Test1" Height="300" Width="300">
    <Window.Resources>
        <Storyboard x:Name="zipButtonOut" x:Key="zipButtonOut">
            <DoubleAnimation Storyboard.TargetName="buttonContinue"
Storyboard.TargetProperty="Width"
From="0" To="300" Duration="0:0:.2"></DoubleAnimation>
            <DoubleAnimation Storyboard.TargetName="buttonContinue"
Storyboard.TargetProperty="Height"
From="2" To="50" Duration="0:0:.4"></DoubleAnimation>
        </Storyboard>
    </Window.Resources>

    <Grid x:Name="LayoutRoot" Background="White">
        <StackPanel HorizontalAlignment="Center" Margin="20">
            <Button x:Name="buttonBegin" Content="Click here to begin" Background="Green" Click="buttonBegin_Click"/>
            <Button x:Name="buttonContinue" Margin="0 70 0 0" Width="160" Height="2" FontSize="18" Background="Yellow"
Content="Click here to continue" Visibility="Collapsed"></Button>
        </StackPanel>
    </Grid>
</Window>

<强>代码隐藏:

using System.Windows;

namespace TestDataGrid566
{
    public partial class Test1 : Window
    {
        public Test1()
        {
            InitializeComponent();
        }

        private void buttonBegin_Click(object sender, RoutedEventArgs e)
        {
            buttonBegin.Visibility = Visibility.Collapsed;
            buttonContinue.Visibility = Visibility.Visible;
            //zipButtonOut.Begin(); //GETS ERROR: The name 'zipButtonOut' does not exist in the current context.
        }
    }
}
有帮助吗?

解决方案

我不知道为什么它的工作原理在Silverlight,但在WPF控件添加到收藏资源是不可用的X:代码名称后面。他们是通过资源的收集访问由它们的X:键,这样你就可以删除X:名称属性,只是在你的代码行之前添加以下代码行后面被注释掉,它会工作(取消注释所讨论的行,当然):

Storyboard zipButtonOut = (Storyboard)Resources["zipButtonOut"];

注意,这需要以下使用语句:

using System.Windows.Media.Animation;

其他提示

貌似VS的Silverlight工具生成用于X A“zipButtonOut”存取器:名称资源也。在未来,只是看看生成的文件(可能是“test1.g.cs”)在OBJ文件夹以查看关于x产生什么样的代码:名称

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top