【问题标题】:How can I convert this XAML Content Control to Code Behind in C#?如何将此 XAML 内容控件转换为 C# 中的代码隐藏?
【发布时间】:2018-08-02 03:37:53
【问题描述】:

所以我在将 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");

我做错了什么?我有点迷失在这里。任何提示/建议都会很棒!

【问题讨论】:

  • 有什么特别的原因吗?
  • 我想将此内容控件添加到按钮中,如果它全部在代码中而不是拥有一个 XAML 文件,这样做会更容易。也主要用于组织目的。

标签: c# wpf xaml code-behind


【解决方案1】:

更正这一行

Storyboard.SetTargetName(cc, "ContentControl");

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

更正其他动画的 TargetName 和 TargetProperty

要添加离散动画,请使用 ObjectAnimationUsingKeyFrames 的 KeyFrames 属性

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

【讨论】:

  • 我收到一个错误,提示无法将字符串转换为 System.Windows.PropertyPath 在第二行的不透明度。另外,如何使 DiscreteObjectKeyFrame 成为 XAML 中 ObjectAnimationUsingKeyFrame 的子对象?
  • 我已经编辑了我的答案。改用 Storyboard.SetTarget
  • 我在第一个参数中使用了 SetTarget 而不是 SetTargetName,但是我仍然在 SetTargetProperty 上遇到错误,因为第二个参数应该是一个字符串,而它应该是一个路径?
  • 我已经更新了。在 UWP 中,您可以将属性名称作为字符串传递。在 WPF 中,您需要传递一个 PropertyPath 对象。
  • 不错!对于双动画,只需访问“To”属性并设置它。像这样 d.To = 1。要设置关键时间,请改用 new Timespan ()。 PS:别忘了接受答案-)
猜你喜欢
  • 2019-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多