如何将此 XAML 内容控件转换为 C# 中的代码隐藏?
How can I convert this XAML Content Control to Code Behind in C#?
所以我在将 XAML 转换为隐藏代码时遇到了一些问题。我想创建一个特定的动画,在此处寻求帮助后,一位用户回复我,向我展示了如何创建我正在寻找的自定义内容控件。他给了我两个文件:Popup.xaml 和 popup.xaml.cs。理想情况下,我希望将其全部隐藏在代码中,但我对 C# 和 WPF 开发还很陌生,我有点不确定该怎么做。
这里是 XAML 文件内容:
<ContentControl x:Name="ContentControl"
x:Class="WpfApplication1.PopupBase"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Template="{DynamicResource ContentControlTemplate}"
Visibility="Hidden">
<ContentControl.Resources>
<Duration x:Key="OpenDuration">00:00:00.4</Duration>
<Storyboard x:Key="OpenStoryboard" Duration="{StaticResource OpenDuration}">
<DoubleAnimation Storyboard.TargetName="ContentControl" Storyboard.TargetProperty="Opacity" To="1" Duration="{StaticResource OpenDuration}">
<DoubleAnimation.EasingFunction>
<BackEase EasingMode="EaseOut" Amplitude="0.4"/>
</DoubleAnimation.EasingFunction>
</DoubleAnimation>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentControl" Storyboard.TargetProperty="Visibility" Duration="{StaticResource OpenDuration}">
<DiscreteObjectKeyFrame Value="{x:Static Visibility.Visible}" KeyTime="00:00:00" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>
就 xaml.cs 文件而言,它只有这样:
var openStoryboard = Resources["OpenStoryboard"] as Storyboard;
openStoryboard.Begin();
到目前为止,这是我试图将其全部转换为代码隐藏的尝试:
Duration time = new Duration(new System.TimeSpan(0,0,0,0,400));
ContentControl cc = new ContentControl
{
Opacity = 0,
Visibility = Visibility.Hidden
};
Storyboard openStoryboard = new Storyboard
{
Name = "openStoryboard",
Duration = time
};
DoubleAnimation d = new DoubleAnimation(1, time)
{
EasingFunction = new BackEase()
{
Amplitude = 0.4,
EasingMode = EasingMode.EaseOut,
}
};
ObjectAnimationUsingKeyFrames oaukf = new ObjectAnimationUsingKeyFrames
{
Duration = time,
};
DiscreteObjectKeyFrame dis = new DiscreteObjectKeyFrame(Visibility.Visible, new KeyTime());
openStoryboard.Children.Add(d);
openStoryboard.Children.Add(oaukf);
cc.Resources.Add(openStoryboard.Name, openStoryboard);
Storyboard.SetTargetName(cc, "ContentControl");
我做错了什么?我有点迷路了。任何 tips/advice 都会很棒!
更正此行
Storyboard.SetTargetName(cc, "ContentControl");
和
//Double animation
Storyboard.SetTarget(d, ContentControl); // <-- change here
Storyboard.SetTargetProperty(d, new PropertyPath("Opacity")); // <-- for WPF
还更正其他动画的 TargetName 和 TargetProperty
要添加离散动画,您可以使用 ObjectAnimationUsingKeyFrames
的关键帧 属性
ObjectAnimationUsingKeyFrames oaukf = new ObjectAnimationUsingKeyFrames
{
Duration = time,
KeyFrames =
{
new DiscreteObjectKeyFrame(Visibility.Visible, new KeyTime())
}
};
所以我在将 XAML 转换为隐藏代码时遇到了一些问题。我想创建一个特定的动画,在此处寻求帮助后,一位用户回复我,向我展示了如何创建我正在寻找的自定义内容控件。他给了我两个文件:Popup.xaml 和 popup.xaml.cs。理想情况下,我希望将其全部隐藏在代码中,但我对 C# 和 WPF 开发还很陌生,我有点不确定该怎么做。
这里是 XAML 文件内容:
<ContentControl x:Name="ContentControl"
x:Class="WpfApplication1.PopupBase"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Template="{DynamicResource ContentControlTemplate}"
Visibility="Hidden">
<ContentControl.Resources>
<Duration x:Key="OpenDuration">00:00:00.4</Duration>
<Storyboard x:Key="OpenStoryboard" Duration="{StaticResource OpenDuration}">
<DoubleAnimation Storyboard.TargetName="ContentControl" Storyboard.TargetProperty="Opacity" To="1" Duration="{StaticResource OpenDuration}">
<DoubleAnimation.EasingFunction>
<BackEase EasingMode="EaseOut" Amplitude="0.4"/>
</DoubleAnimation.EasingFunction>
</DoubleAnimation>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentControl" Storyboard.TargetProperty="Visibility" Duration="{StaticResource OpenDuration}">
<DiscreteObjectKeyFrame Value="{x:Static Visibility.Visible}" KeyTime="00:00:00" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>
就 xaml.cs 文件而言,它只有这样:
var openStoryboard = Resources["OpenStoryboard"] as Storyboard;
openStoryboard.Begin();
到目前为止,这是我试图将其全部转换为代码隐藏的尝试:
Duration time = new Duration(new System.TimeSpan(0,0,0,0,400));
ContentControl cc = new ContentControl
{
Opacity = 0,
Visibility = Visibility.Hidden
};
Storyboard openStoryboard = new Storyboard
{
Name = "openStoryboard",
Duration = time
};
DoubleAnimation d = new DoubleAnimation(1, time)
{
EasingFunction = new BackEase()
{
Amplitude = 0.4,
EasingMode = EasingMode.EaseOut,
}
};
ObjectAnimationUsingKeyFrames oaukf = new ObjectAnimationUsingKeyFrames
{
Duration = time,
};
DiscreteObjectKeyFrame dis = new DiscreteObjectKeyFrame(Visibility.Visible, new KeyTime());
openStoryboard.Children.Add(d);
openStoryboard.Children.Add(oaukf);
cc.Resources.Add(openStoryboard.Name, openStoryboard);
Storyboard.SetTargetName(cc, "ContentControl");
我做错了什么?我有点迷路了。任何 tips/advice 都会很棒!
更正此行
Storyboard.SetTargetName(cc, "ContentControl");
和
//Double animation
Storyboard.SetTarget(d, ContentControl); // <-- change here
Storyboard.SetTargetProperty(d, new PropertyPath("Opacity")); // <-- for WPF
还更正其他动画的 TargetName 和 TargetProperty
要添加离散动画,您可以使用 ObjectAnimationUsingKeyFrames
的关键帧 属性ObjectAnimationUsingKeyFrames oaukf = new ObjectAnimationUsingKeyFrames
{
Duration = time,
KeyFrames =
{
new DiscreteObjectKeyFrame(Visibility.Visible, new KeyTime())
}
};