在WP8上更改按钮边框颜色时滞后

JTI*_*TIM 7 c# performance xaml visual-studio windows-phone-8

我正在一个按钮上移动一个对象,当这个对象在一个按钮上时,我改变了边框的颜色.这没问题,我可以通过绑定,故事板或样式设置器/可视状态来实现.当我在我的模拟器上运行它时,它工作得很好而且流畅,但是当我在Windows手机上运行它时,当国家的边界​​发生变化时会有一个小的滞后.有办法避免这种情况吗?

1.代码示例

<Button x:Name="Button1" BorderThickness="0" BorderBrush="Transparent">
    <Button.Template>
        <ControlTemplate x:Name="Control">
            <Path x:Name="CountryUser" Style="{StaticResource style_ColorButton}" Data="{Binding UserView.buttonData}" Fill="{StaticResource ButtonBackground}">
                <Path.Resources>
                    <Storyboard x:Name="StoryBoard1">
                        <ColorAnimation Storyboard.TargetName="User" Storyboard.TargetProperty="(Stroke).(SolidColorBrush.Color)" To="Blue" Duration="0"/>
                    </Storyboard>
                </Path.Resources>
            </Path>
        </ControlTemplate> 
    </Button.Template>
Run Code Online (Sandbox Code Playgroud)

并激活

foreach (UIElement x in ElementsAtPoint)
{
    f = x as FrameworkElement;
    if (f is Path)
    {
        try
        {
            h = f as Path;
            Storyboard sb = h.Resources["StoryBoard1"] as Storyboard;
            sb.Begin();
        }
        catch
        {
        }

        break;
    }
}
Run Code Online (Sandbox Code Playgroud)

额外

仿真器和实际设备之间想到的一个区别是触摸点的准确性.在模拟器中,您使用具有更高分辨率的鼠标.在您使用手指的设备上,精度要低得多,即毫米与像素.

我刚刚在manip_completed_event中用一个简单的计数器和一个断点来测试它.但两种情况下的计数都相同.它只试图运行故事板一次.

Extra2

为了澄清滞后,我看到:

我正在拖动一个平滑地跟随手指的元素,当我进入一个新的按钮时,我拖动的元素停止了一会儿.按钮的颜色发生变化,元素再次移动.

当按钮改变颜色后我向后移动.当我在按钮之间移动时,元素用我的手指平滑移动.

因此,当故事板被激活时存在滞后,并且可以看出我拖动的元素不能跟随手指.

因此,我试图在电话上找到改变可能具有更好性能的颜色的替代方法.因为它在模拟器上工作正常.

我测试了两种不同的lumia 920s和一种lumia 520

2.代码示例

在Shawn Kendrot的帮助下使用Visual State Manager,来自另一个问题:使用Dependency对象进行颜色动画WP8复制如下:

<Style x:Key="ButtonStyle" TargetType="Button">
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="BorderBrush" Value="{StaticResource PhoneForegroundBrush}"/>
    <Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}"/>
    <Setter Property="BorderThickness" Value="{StaticResource PhoneBorderThickness}"/>
    <Setter Property="FontFamily" Value="{StaticResource PhoneFontFamilySemiBold}"/>
    <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeMedium}"/>
    <Setter Property="Padding" Value="10,5,10,6"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Grid Background="Transparent">
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Normal"/>
                            <VisualState x:Name="MouseOver">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="White"/>
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ButtonBackground">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="Orange"/>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Pressed">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneButtonBasePressedForegroundBrush}"/>
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ButtonBackground">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneAccentBrush}"/>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Disabled">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/>
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ButtonBackground">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/>
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ButtonBackground">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent"/>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <Border x:Name="ButtonBackground" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" CornerRadius="0" Margin="{StaticResource PhoneTouchTargetOverhang}">
                        <ContentControl x:Name="ContentContainer" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Padding="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Border>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)

注意它有一个MouseOver VisualState.然后分配样式并订阅事件处理程序

<Button Content="Drag over me" Style="{StaticResource ButtonStyle}"  MouseEnter="OnButtonMouseEnter" MouseLeave="OnButtonMouseLeave"/>
Run Code Online (Sandbox Code Playgroud)

并在事件处理程序中更改可视状态.

private void OnButtonMouseEnter(object sender, MouseEventArgs e)
{
    VisualStateManager.GoToState((Control)sender, "MouseOver", true);
}

private void OnButtonMouseLeave(object sender, MouseEventArgs e)
{
    VisualStateManager.GoToState((Control)sender, "Normal", true);
}
Run Code Online (Sandbox Code Playgroud)

还有滞后,其他人都有可以测试的解决方案吗?

额外

由于问题可能是低fps,即当我想改变颜色时出现渲染问题,是否可以使用调度程序?

所以在我开始一个故事板的第一个代码示例中,我可以在视图中调用一个函数来使用调度程序?任何人都有这样做的想法,或者如果这是一个好主意?

感谢所有的帮助,因为我无法理解为什么我可以在屏幕上平滑移动物体但是当我想要改变边框的颜色时我可以看到它滞后:/

Oli*_*yen 3

我的应用程序中的动画遇到了性能问题,并且通常通过将CacheMode属性设置BitmapCache为移动的元素来解决这些问题。

这个想法是,它指示框架获取控件的屏幕截图并将生成的位图存储在 GPU 内存中,以便不必在每一帧上重新绘制控件。

你可以看看这篇文章: CacheMode 及其重要性, 以获得更多见解。