简体   繁体   English

如何将XAML内容控件转换为C#中的代码隐藏?

[英]How can I convert this XAML Content Control to Code Behind in C#?

So I'm having some trouble converting XAML to code behind. 所以我在将XAML转换为后面的代码时遇到了一些麻烦。 I wanted to create a specific animation and after asking for some help on here, a user replied showing me how to create a custom Content Control I was looking for. 我想创建一个特定的动画,并在这里寻求帮助之后,一个用户回答说给我看如何创建我想要的自定义内容控件。 He gave me two files: Popup.xaml and popup.xaml.cs. 他给了我两个文件:Popup.xaml和popup.xaml.cs。 Ideally I'd like to have it all in code behind, but I'm fairly new to C# and WPF development and I'm a bit unsure how to do it. 理想情况下,我希望将其全部包含在代码中,但是对于C#和WPF开发我还是很陌生,我不确定该怎么做。

Here is the XAML file content: 这是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>

and as far as the xaml.cs file, all it had was this: 就xaml.cs文件而言,它所具有的就是:

var openStoryboard = Resources["OpenStoryboard"] as Storyboard;
openStoryboard.Begin();

Here are my attempts so far trying to convert this all to code behind: 到目前为止,这是我尝试将所有内容转换为后面代码的尝试:

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");

What am I doing wrong? 我究竟做错了什么? I'm kind of lost here. 我有点迷路了。 Any tips/advice would be great! 任何提示/建议都很好!

Correct this line 更正此行

Storyboard.SetTargetName(cc, "ContentControl");

with

//Double animation
Storyboard.SetTarget(d, ContentControl); // <-- change here
Storyboard.SetTargetProperty(d, new PropertyPath("Opacity")); // <-- for WPF

Correct also TargetName and TargetProperty for other animations 还要更正其他动画的TargetName和TargetProperty

To add discrete animations, you use the KeyFrames property of ObjectAnimationUsingKeyFrames 若要添加离散动画,请使用ObjectAnimationUsingKeyFrames的KeyFrames属性

ObjectAnimationUsingKeyFrames oaukf = new ObjectAnimationUsingKeyFrames
{
    Duration = time,
    KeyFrames = 
    {
    new DiscreteObjectKeyFrame(Visibility.Visible, new KeyTime())
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM