'WPF Animation : How do i make it slide in?

So I just got into animations and I wanted to do a "Slide out" animation and I managed to do so just fine. But now I want it to slide in with the click on the same button. So like I click it and it slides out and then I want it to slide back in when I click it again.

WITHOUT any code behind, so just through xaml

enter image description here

Here is the XAML

<Grid>
    <Grid.Resources>
        <Storyboard x:Key="TransformImage">
            <DoubleAnimation
                Storyboard.TargetName="MovingImage"
                Storyboard.TargetProperty="(Image.RenderTransform).(TranslateTransform.X)"
                By="130" Duration="0:0:0.3">
            </DoubleAnimation>
        </Storyboard>

        <Storyboard x:Key="TransformButton">
            <DoubleAnimation
                Storyboard.TargetName="btnChange"
                Storyboard.TargetProperty="(Button.RenderTransform).(TranslateTransform.X)"
                By="130" Duration="0:0:0.3">
            </DoubleAnimation>
        </Storyboard>
    </Grid.Resources>


    <Grid.Triggers>
        <EventTrigger RoutedEvent="Button.Click" SourceName="btnChange">
            <BeginStoryboard Storyboard="{StaticResource TransformImage}"/>
            <BeginStoryboard Storyboard="{StaticResource TransformButton}"/>
        </EventTrigger>
    </Grid.Triggers>

    <StackPanel Orientation="Horizontal" Margin="0">
        <Image x:Name="MovingImage" Source="logo.png"
               MaxWidth="120">
            <Image.RenderTransform>
                <TranslateTransform />
            </Image.RenderTransform>
        </Image>
    </StackPanel>

    <StackPanel
        Panel.ZIndex="1"
        Height="450" 
          Width="120"
          HorizontalAlignment="Left"
          Background="Black"></StackPanel>

    <Button Margin="130,0,0,0" Height="40" Width="120"
            Content="Show Image" x:Name="btnChange"
            HorizontalAlignment="Left" >
        <Button.RenderTransform>
            <TranslateTransform />
        </Button.RenderTransform>
    </Button>
</Grid>


Solution 1:[1]

You can have a lot of improvement in your code.

First, make the server where you want to receive the file so that you can keep it running and bind the port only there and not on the client-side. Also, you are using while loop is wrong as you will never get out of it.

Server code:


import socket

host = "127.0.0.1"
port = 12000

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((host,port))

f= open('Myfile2.txt','wb')
print('New file created')
data, addr = sock.recvfrom(1024)

while data:
    print(data)
    if data.decode("utf-8")=="Now":
        break
    f.write(data)
    data, addr = sock.recvfrom(1024)

print('File is successfully received!!!')
f.close()
f = open('Myfile2.txt','r')
print(f.read)

f.close()
sock.close()
print('Connection closed!')

Client code:


import socket

host = "127.0.0.1"
port = 12000
buffer_size = 1024
file_name = 'Myfile.txt'

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

f = open(file_name, "r")
data = f.read(buffer_size)

while data:
    print(data)
    if(sock.sendto(str.encode(data), (host, port))):
        data = f.read(buffer_size)
sock.sendto(str.encode("Now"),(host, port))
sock.close()
f.close()

Note: I am sending Now just for letting the server know that the file is finished, you can send anything else.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 HARSH MITTAL