Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I can't get a double animation inside the vb.net working. I've successfully compiled HLSL file and when I bind it in the xaml file to a control, it works just fine. I can also use custom effect from the .xaml.vb file, but I can not get it to work with DoubleAnimation in the .xaml.vb file.

Here is the list of my codes:

----- Pixel Shader Code ------
// http://www.silverlightshow.net/items/Book-Folding-effect-using-Pixel-Shader.aspx

sampler2D input : register(s0);

/// <summary>The Left</summary>;
/// <minvalue>0</minvalue>;
/// <maxvalue>1</maxvalue>;
/// <defaultvalue>0</defaultvalue>;
 float left : register(c0);
 float4 transform(float2 uv : TEXCOORD) : COLOR
 {
        float right = 1 - left;
     // transforming the curent point (uv) according to the new boundaries.
        float2 tuv = float2((uv.x - left) / (right - left), uv.y);

     float tx = tuv.x;
     if (tx > 0.5)
     {
        tx = 1 - tx;
     }
     float top = left * tx;
     float bottom = 1 - top;
     if (uv.y >= top && uv.y <= bottom)
     {
     //linear interpolation between 0 and 1 considering the angle of folding.
         float ty = lerp(0, 1, (tuv.y - top) / (bottom - top));
        // get the pixel from the transformed x and interpolated y.
       return tex2D(input, float2(tuv.x, ty));
 }
    return 0;
 }

 float4 main(float2 uv : TEXCOORD) : COLOR
 {
     float right = 1 - left;
     if(uv.x > left && uv.x < right)
     {
        return transform(uv);
     }

     return 0;
 }



----- .xaml.vb file --------
'Procedure I call to start double animation.
 Public Sub ApplyEffect()
Dim sb As New Storyboard()
        Dim da As New DoubleAnimation()
        Storyboard.SetTarget(da, myButton)
        Storyboard.SetTargetProperty(da, New PropertyPath(WPF_Effects.clEffects.clEffects.LeftProperty))
        da.From = 0
        da.[To] = 10
        da.Duration = (TimeSpan.FromSeconds(2))
        da.AutoReverse = True
        da.RepeatBehavior = RepeatBehavior.Forever
        sb.Children.Add(da)
        sb.Begin(myButton)
End Sub




---- clEffects Class -----
Option Strict Off
Option Explicit On
Imports System
'Imports System.Net
Imports System.Windows
Imports System.Windows.Media
Imports System.Windows.Media.Effects
Imports System.Windows.Media.Media3D
#Region "FADE IN TRANSITION EFFECT"
Namespace clEffects
    ///<summary>A transition effect </summary>
    Public Class clEffects
        Inherits ShaderEffect
        Public Shared ReadOnly InputProperty As DependencyProperty = ShaderEffect.RegisterPixelShaderSamplerProperty("Input", GetType(clEffects), 0)
        Public Shared ReadOnly LeftProperty As DependencyProperty = DependencyProperty.Register("Left", GetType(Double), GetType(clEffects), New UIPropertyMetadata(CType(0.0R, Double), PixelShaderConstantCallback(0)))
        Public Sub New()
            MyBase.New()
            Dim pixelShader As PixelShader = New PixelShader()
            pixelShader.UriSource = New Uri("/WPF_Effects;component/Effect.ps", UriKind.Relative)
            Me.PixelShader = pixelShader
            Me.UpdateShaderValue(InputProperty)
            Me.UpdateShaderValue(LeftProperty)
        End Sub
        Public Property Input() As Brush
            Get
                Return CType(Me.GetValue(InputProperty), Brush)
            End Get
            Set(ByVal value As Brush)
                Me.SetValue(InputProperty, value)
            End Set
        End Property
        '''<summary>The Left </summary>
        Public Property Left() As Double
            Get
                Return CType(Me.GetValue(LeftProperty), Double)
            End Get
            Set(ByVal value As Double)
                Me.SetValue(LeftProperty, value)
            End Set
        End Property
    End Class
End Namespace
#End Region



------ XAML WPF User Control ------
<usercontrol x:class="ucWPF" xmlns:x="#unknown">
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006";
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:local="clr-namespace:WPF_Effects.clEffects"
             mc:Ignorable="d"
             d:DesignHeight="300" d:DesignWidth="300">
    <grid>

        <stackpanel>

            <Button Name="myButton" Content="Click!" Click="StartAnimation" Width="153"
            Margin="60" Height="74">

                <Button.RenderTransform>
                    <transformgroup>
                        <translatetransform x:name="myTranslateTransform" />
                        <scaletransform x:name="myScaleTransform" /><br mode=" hold=" />                    </TransformGroup><br mode="></scaletransform></transformgroup></stackpanel></grid></usercontrol>
Posted
Updated 7-Feb-11 17:07pm
v2

1 solution

I finally figured it out.
Here is what you need to do:

in the .xaml file add a Resource code to an object, in my case I used StackPanel:
<stackpanel.resources>
<storyboard x:name="MyStoryboard" x:key="myEffect" xmlns:x="#unknown">
<doubleanimation storyboard.targetproperty="clEffects.Left" storyboard.targetname="sbEffect">
/>
</doubleanimation></storyboard>
</stackpanel.resources>


add custom effect to the button in .xaml file:
<Button.Effect>
<local:cleffects x:name="myclEffects" xmlns:x="#unknown" xmlns:local="#unknown" />
</Button.Effect>




modify .xaml.vb code to this:
'This code can be placed in a button event or any other events.
Dim da as New DoubleAnimation()
Storyboard.SetTarget(da, myButton1)
da.From = 0
da.[To] = 0.5
da.Duration = (TimeSpan.FromSeconds(2))
myclEffects.BeginAnimation(clEffects.clEffects.LeftProperty, da)
'clEffects is a custom HLSL Shader effect.


I have not experimented alot with this yet, so I don't know what the draw backs are. But for now this solved my issue.
Hope someone will find a use for this too.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900