내 스토리 보드 X에 액세스하는 이유 : 이름은 Silverlight에서 작동하지만 WPF에서는 작동하지 않습니까?
-
22-08-2019 - |
문제
다음 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.
}
}
}
해결책
Silverlight에서 왜 작동하는지 모르겠지만 WPF 컨트롤에서는 리소스 컬렉션에 추가하여 X : Name에 의해 사용할 수 없습니다. x : key에 의해 리소스 컬렉션을 통해 액세스 할 수 있으므로 x : 이름 속성을 제거하고 다음 코드 라인 직전에 다음 코드 줄을 추가 할 수 있습니다. 물론 문제의 라인) :
Storyboard zipButtonOut = (Storyboard)Resources["zipButtonOut"];
다음과 같은 명령문을 사용해야합니다.
using System.Windows.Media.Animation;
다른 팁
vs silverlight 도구는 X : Name Resources의 "ZipButtonout"액세서를 생성하는 것처럼 보입니다. 앞으로 OBJ 폴더의 생성 된 파일 (아마도 "test1.g.cs")을보고 x : names에 대해 어떤 코드가 생성되는지 확인하십시오.
제휴하지 않습니다 StackOverflow