2009-03-25 19 views
6

Mã WPF sau đây bị lỗi: Tên 'zipButtonOut' không tồn tại trong ngữ cảnh hiện tại.Tại sao truy cập Bảng phân cảnh x: Tên của tôi hoạt động trong Silverlight nhưng không hoạt động trong WPF?

Tuy nhiên, mã giống hệt nhau làm việc trong Silverlight như tôi chứng minh ở đây: http://tanguay.info/web/index.php?pg=codeExamples&id=65

gì để tôi phải làm gì để mã WPF để có thể truy cập vào Storyboard trong Window.Resources? Tôi đã thử nó trong một WPF UserControl là tốt nhưng có cùng một lỗi.

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> 

code-behind:

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

Trả lời

12

Tôi không biết tại sao nó hoạt động trong Silverlight, nhưng trong WPF control bạn thêm vào bộ sưu tập nguồn lực không có sẵn bởi x của họ: Tên trong mã phía sau. Họ có thể truy cập thông qua bộ sưu tập Tài nguyên bằng x: Khóa của họ, vì vậy bạn có thể loại bỏ thuộc tính x: Name và thêm dòng mã sau ngay trước dòng trong mã của bạn phía sau được nhận xét và nó sẽ hoạt động (bỏ ghi chú dòng trong câu hỏi, tất nhiên):

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

Lưu ý rằng điều này đòi hỏi báo cáo kết quả sử dụng sau đây:

using System.Windows.Media.Animation; 
0

Hình như các công cụ VS Silverlight tạo ra một "zipButtonOut" accessor cho x: nguồn lực Tên cũng . Trong tương lai, chỉ cần xem tập tin được tạo ra (có thể là "test1.g.cs") trong thư mục obj để xem mã nào được tạo cho x: Names.

Các vấn đề liên quan