2009-03-25 2 views
6

Следующий код WPF получает ошибку: Имя «zipButtonOut» не существует в текущем контексте.Почему доступ к моей раскадровке x: Имя работает в Silverlight, но не в WPF?

Однако, идентичный код работает в Silverlight, как я показываю здесь: http://tanguay.info/web/index.php?pg=codeExamples&id=65

Что я должен сделать, чтобы код WPF, чтобы иметь возможность получить доступ к раскадровке в Window.Resources? Я тоже пробовал его в WPF UserControl, но получил ту же ошибку.

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. 
     } 
    } 
} 

ответ

12

Я не знаю, почему это работает в Silverlight, но в WPF управляет добавить к коллекции ресурсов являются не доступны по их x: имя в коде позади. Они доступны через сбор ресурсов по их x: Key, поэтому вы можете удалить атрибут x: Name и добавить следующую строку кода непосредственно перед строкой вашего кода, за которой закомментирован, и он будет работать (раскомментировать линия в вопросе, конечно):

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

Обратите внимание, что для этого необходимо следующее, используя оператор:

using System.Windows.Media.Animation; 
0

Похоже инструменты VS Silverlight генерировать «zipButtonOut» аксессор для х: Название ресурсов также , В будущем просто взгляните на сгенерированный файл (возможно, «test1.g.cs») в папке obj, чтобы узнать, какой код создан для x: Имена.