KeyFrame对象上的KeyTime属性必须设置为非负TimeSpan值

时间:2014-10-30 08:22:04

标签: c# xaml windows-phone-8

我正在尝试编写广告控件来实现我的所有应用页面。 这个usercontrol只包含" layoutRoot"网格,以及" AdMob"网格,起初我想隐藏,只显示在" time_show_banner"时间。我尝试绑定此变量,但它只返回标题处的错误。

在c#代码中:

public string time_show_banner { get; set; }  

我的用户控制:

<Grid x:Name="LayoutRoot" Background="Black" Height="60">   
    <Grid x:Name="AdMob" RenderTransformOrigin="0.5,0.5">            
        <Grid.RenderTransform>
            <CompositeTransform TranslateY="70"/>
        </Grid.RenderTransform>
    </Grid>
    <Grid.Triggers>
        <EventTrigger RoutedEvent="Grid.Loaded">
            <BeginStoryboard>
                <Storyboard>
                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.TranslateY)" Storyboard.TargetName="AdMob">
                        <DiscreteObjectKeyFrame Value="0"
            KeyTime="{Binding time_show_banner ,Converter={StaticResource ConvertKeyTime}}"/>
                    </ObjectAnimationUsingKeyFrames>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Grid.Triggers>
</Grid>

转换:

public class ConvertKeyTime : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        string d = value.ToString();
        string a = "0:0:" + d;
        return a;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

1 个答案:

答案 0 :(得分:2)

您的转换器应返回TimeSpan,而不是字符串:

public object Convert(
    object value, Type targetType, object parameter, CultureInfo culture)
{
    var seconds = (double)value;
    return TimeSpan.FromSeconds(seconds);
}

Convert方法假定属性的类型为double,并以秒为单位返回时间:

public double time_show_banner { get; set; } // seconds

尽管如此,更好的解决方案可能是直接将属性声明为TimeSpan。你根本不需要转换器:

public TimeSpan time_show_banner { get; set; }