Hi, I'm making a wpf app in C#.
I'm trying to figure out a way to get live values of an instance of a boolean variable into another method
In this code, I want to grab the values of bouncerX and bouncerY for each instance of it in a new onClick() Method, so every time the button is clicked the current values of a particular instance are grabbed. I can't figure it out.
I've commented IMPORTANT on the parts i think are most relevant.
[/CODE]
Thanks!
I'm trying to figure out a way to get live values of an instance of a boolean variable into another method
In this code, I want to grab the values of bouncerX and bouncerY for each instance of it in a new onClick() Method, so every time the button is clicked the current values of a particular instance are grabbed. I can't figure it out.
I've commented IMPORTANT on the parts i think are most relevant.
C#:
private void Loadinitaldata(object sender, RoutedEventArgs e)
{
//Store data from file as List<>
List<string> lines = fileManager.ReadDataFromFile("ImageData.txt");
if (lines != null)
{
//New instance of MyClass, passing through data to it.
MyClass myClass = new MyClass(lines);
//Fetching data from MyClass
myDict = myClass.GetData();
//Iterating through each entry
foreach (KeyValuePair<string, List<string>> entry in myDict)
{
string animal = entry.Key + ": " + string.Join(", ", entry.Value);
//Checking if animal allready exists in ListBox1.
if (!ListBox1.Items.Contains(animal))
{
ListBox1.Items.Add(animal);
var imageControl = Canvas1.Children.OfType<Image>().FirstOrDefault(i => i.Name == entry.Key);
if (imageControl != null)
{
Canvas.SetLeft(imageControl, int.Parse(entry.Value[0]));
Canvas.SetTop(imageControl, int.Parse(entry.Value[1]));
}
//IMPORTANT
AnimationIndicators indicators = new AnimationIndicators { bouncerX = true, bouncerY = true };
ImageAnimation(entry.Key, indicators);
}
}
}
else
{
MessageBox.Show("Failed to read data from file.");
}
}
//IMPORTANT
public class AnimationIndicators
{
public bool bouncerX { get; set; }
public bool bouncerY { get; set; }
}
//A method to animate the image
//IMPORTANT
public void ImageAnimation(string key, AnimationIndicators animationIndicators)
{
if (!imageTimers.TryGetValue(key, out DispatcherTimer timer))
{
// Create a new DispatcherTimer if one doesn't exist
timer = new DispatcherTimer();
timer.Tick += new EventHandler((sender, e) => Timer_y(sender, e, key, animationIndicators));
timer.Tick += new EventHandler((sender, e) => Timer_x(sender, e, key, animationIndicators)); // use a lambda to pass in the key
timer.Interval = new TimeSpan(0, 0, 0, 0, 2);
imageTimers.Add(key, timer);
}
// Start the DispatcherTimer
timer.Start();
}
private void Timer_x(object sender, EventArgs e, string key, AnimationIndicators animationIndicators)
{
string imageName = key.ToString();
int xspeed = int.Parse(myDict[key][2]);
var imageControl = Canvas1.Children.OfType<Image>().FirstOrDefault(i => i.Name == imageName);
if (imageControl != null)
{
imageControl.Visibility = Visibility.Visible;
}
if (imageControl != null)
{
long positionX = Convert.ToInt64(Canvas.GetLeft(imageControl));
//IMPORTANT
if (positionX >= 450)
{
animationIndicators.bouncerX = true;
}
if (animationIndicators.bouncerX)
{
Canvas.SetLeft(imageControl, positionX - xspeed);
}
if (positionX <= 0)
{
animationIndicators.bouncerX = false;
}
if (!animationIndicators.bouncerX)
{
Canvas.SetLeft(imageControl, positionX + xspeed);
}
}
else
{
MessageBox.Show("whoopsie... error");
}
}
Thanks!
Last edited: