• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

Using namespaces and time dispatcher for images animation (WPF C#)

MrSeanKon

New Member
Joined
Nov 14, 2006
Messages
267 (0.04/day)
Location
Athens in love with Anna :)
The first attachment includes a non ultimate code; imagine that you have to animate 10 cards => 10 different time dispatchers??? => this sucks! :laugh:
But I uploaded it just to show you how we can bypass this problem by reusing code.
Yeap the solution is an extra namespace. Someone may say why don't you write an extra class inside the major (main) namespace?
Well it is possible but I prefer to avoid it. Cos we must use static functions and variables and this is an extra headache.
Download the files and let's discuss :)
 

Attachments

  • Works but code sucks!.zip
    63.2 KB · Views: 213
  • Deal 4 cards.zip
    63.6 KB · Views: 214
Joined
Dec 2, 2009
Messages
3,351 (0.64/day)
System Name Dark Stealth
Processor Ryzen 5 5600x
Motherboard Gigabyte B450M Gaming rev 1.0
Cooling Snowman, arctic p12 x2 fans
Memory 16x2 DDR4 Corsair Dominator Pro
Video Card(s) 3080 10gb
Storage 2TB NVME PCIE 4.0 Crucial P3 Plus, 1TB Crucial MX500 SSD, 4TB WD RED HDD
Display(s) HP Omen 34c (34" monitor 3440x1440 165Hz VA panel)
Case Zalman S2
Power Supply Corsair 750TX
Mouse Logitech pro superlight, mx mouse s3, Razer Basiliskx with battery
Keyboard Custom mechanical keyboard tm680
Software Windows 11
Benchmark Scores 70-80 fps 3440x1440 on cyberpunk 2077 max settings
Ok, you mean adding namespaces will add animation to the cards?
And you had only 4 card animations, which means 4 namespaces?
Nice work though

Why don't you add a function for the namespaces?
Or make a single namespace for 10 namespaces :)
 

MrSeanKon

New Member
Joined
Nov 14, 2006
Messages
267 (0.04/day)
Location
Athens in love with Anna :)
No, total namespaces are two. The main (its name is Game) and the secondary (its name is paixnidi => παιχνίδι = means game and pronounced paixnidi).
Try to download the files first. Have you got Visual Studio?
If not, it is not a problem => open with Notepad MainWindow.xaml.cs files.
 
Joined
Dec 2, 2009
Messages
3,351 (0.64/day)
System Name Dark Stealth
Processor Ryzen 5 5600x
Motherboard Gigabyte B450M Gaming rev 1.0
Cooling Snowman, arctic p12 x2 fans
Memory 16x2 DDR4 Corsair Dominator Pro
Video Card(s) 3080 10gb
Storage 2TB NVME PCIE 4.0 Crucial P3 Plus, 1TB Crucial MX500 SSD, 4TB WD RED HDD
Display(s) HP Omen 34c (34" monitor 3440x1440 165Hz VA panel)
Case Zalman S2
Power Supply Corsair 750TX
Mouse Logitech pro superlight, mx mouse s3, Razer Basiliskx with battery
Keyboard Custom mechanical keyboard tm680
Software Windows 11
Benchmark Scores 70-80 fps 3440x1440 on cyberpunk 2077 max settings
I have already downloaded
This is why i said 4 card animations
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.63/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
Uh...there's absolutely no reason to use two namespaces in your case. I moved "attempt" class into the "Game" namespace, removed all references to "paixnidi" and it works fine:
Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using System.Windows.Threading;


namespace Game
{
    class attempt
    {
        int delay;
        double x, y, X, Y;
        Image card;
        DispatcherTimer Timer;

        public void move(int lag, double startX, double startY, double endX, double endY, Image karta)
        {
            delay = lag;
            x = startX;
            y = startY;
            X = endX;
            Y = endY;
            card = karta;
            Timer = new DispatcherTimer();
            Timer.Tick += new EventHandler(tick);
            Timer.Interval = new TimeSpan(0, 0, delay);
            Timer.Start();
        }
        void animation()
        {
            if (delay == 0)
                delay = 1;
            ThicknessAnimation myanimation = new ThicknessAnimation(new Thickness(x, y, 0, 0), new Thickness(X, Y, 0, 0),
                  TimeSpan.FromSeconds(delay));
            card.BeginAnimation(Image.MarginProperty, myanimation);
        }
        void tick(object sender, EventArgs e)
        {
            Timer.Stop();
            Timer = null;
            if (delay > 1)
                delay = 1;
            animation();
        }
    }
  public partial class MainWindow:Window
  {
      public MainWindow()
      {
         InitializeComponent();         
      }      
      private void button1_Click(object sender, RoutedEventArgs e)
      {
         attempt card1=new attempt();
         attempt card2=new attempt();
         attempt card3=new attempt();
         attempt card4=new attempt();
         
         card1.move(0,image1.Margin.Left,image1.Margin.Top,image5.Margin.Left,image5.Margin.Top,image1);
         card2.move(1,image2.Margin.Left,image2.Margin.Top,image6.Margin.Left,image6.Margin.Top,image2);
         card3.move(2,image3.Margin.Left,image3.Margin.Top,image7.Margin.Left,image7.Margin.Top,image3);
         card4.move(3,image4.Margin.Left,image4.Margin.Top,image8.Margin.Left,image8.Margin.Top,image4); 
         button1.Visibility=Visibility.Hidden;
      }      
   }
}
Namespaces are only to avoid collisions of same-named classes. For example, you might have zero-terminated String class under a network namespace while having a standard String class under a text namespace:
Text.String
Network.String

Different classes, same name.


You should only have one dispatcher handle all animations. If more than one animation needs to be done, they need to be done in order. Use a System.Collections.Generic.Queue<> for example.
 

MrSeanKon

New Member
Joined
Nov 14, 2006
Messages
267 (0.04/day)
Location
Athens in love with Anna :)
Uh...there's absolutely no reason to use two namespaces in your case. I moved "attempt" class into the "Game" namespace, removed all references to "paixnidi" and it works fine.
Readers download the new attachment.

Namespaces are only to avoid collisions of same-named classes. For example, you might have zero-terminated String class under a network namespace while having a standard String class under a text namespace:
Text.String
Network.String

Different classes, same name.
Although I am new to OOP (object orient programming) I knew that. But I was too nervous and hurried for almost 2 days :confused: and I would to solve my issue as soon as possible. That's why I searched to many pages and this one confused me enough, because it says about static functions...
I should not hurry :D

You should only have one dispatcher handle all animations. If more than one animation needs to be done, they need to be done in order. Use a System.Collections.Generic.Queue<> for example.
Why?
What's the problem?
 

Attachments

  • One namespace & 2 classes.zip
    63.5 KB · Views: 216
Top