6

次のWPFコードでエラーが発生します。名前'zipButtonOut'は現在のコンテキストに存在しません。

ただし、ここで示すように、同じコードがSilverlightで機能します:http ://tanguay.info/web/index.php?pg = codeExamples&id = 65

Window.Resources内のストーリーボードにアクセスできるようにするには、WPFコードに対して何をする必要がありますか?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.
        }
    }
}
4

2 に答える 2

12

Silverlight で機能する理由はわかりませんが、リソース コレクションに追加する WPF コントロールは、コード ビハインドの x:Name では使用できません。x:Key によって Resources コレクションを介してアクセスできるため、x:Name 属性を削除し、コード ビハインドでコメント アウトされている行の直前に次のコード行を追加すると、機能します (コメントを解除します)。もちろん、問題の行):

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

これには、次の using ステートメントが必要であることに注意してください。

using System.Windows.Media.Animation;
于 2009-03-25T11:21:52.590 に答える
0

VS Silverlightツールは、x:Nameリソースの「zipButtonOut」アクセサーも生成するようです。将来的には、objフォルダーに生成されたファイル(おそらく「test1.g.cs」)を見て、x:Namesに対してどのコードが生成されるかを確認してください。

于 2009-07-19T07:32:44.763 に答える