首页 / C# / C#-在WPF中更新画布
C#-在WPF中更新画布
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C#-在WPF中更新画布,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2947字,纯文字阅读大概需要5分钟。
内容图文
我想在画布上显示移动蚂蚁的动画.因此,应更改椭圆的位置.步骤的计算有效,但是我无法显示MainWindow中椭圆位置的变化.仅在完成所有蚂蚁步骤的计算后,才会显示画布.
XAML代码:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Canvas Name="myCanvas">
<Ellipse x:Name="ant1" Width="11" Height="11" Stroke="Black" Fill="Red"/>
</Canvas>
</Window>
C#代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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.Threading;
using System.Diagnostics;
using System.Windows.Threading;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var random = new Random();
var iterations = 10000;
var numberOfAnts = 10;
var ants = CreateAntCollection(numberOfAnts);
// time-loop
for (var iteration = 0; iteration < iterations; iteration++)
{
// move ants
foreach (var ant in ants)
{
var x = (random.Next(3) - 1) + ant.Position.X;
var y = (random.Next(3) - 1) + ant.Position.Y;
ant.Move(x, y);
}
// animate the ant
// test - todo
Debug.WriteLine(ants[0].Position.X);
Canvas.SetLeft(ant1, ants[0].Position.X); // movement not shown
}
}
private static List<Ant> CreateAntCollection(int count)
{
var ants = new List<Ant>(count);
for (var i = 0; i < count; i++)
{
var name = string.Format("ant-{0}", i);
var ant = new Ant(name);
ants.Add(ant);
}
return ants;
}
}
class Ant
{
public Ant(string name)
{
Name = name;
Position = new Position(80, 80);
}
public string Name { get; private set; }
public Position Position { get; private set; }
public void Move(int x, int y)
{
Position = new Position(x, y);
}
public override string ToString()
{
return Name;
}
}
struct Position
{
public readonly int X;
public readonly int Y;
public Position(int x, int y)
{
X = x;
Y = y;
}
public override string ToString()
{
return string.Format("{0},{1}", X, Y);
}
}
}
这个“解决方案”不起作用:element.InvalidateVisual();
解决方法:
问题是您正在同步运行机芯.您需要在另一个线程中执行它.像这样:
Task.Run(() => {
for (var iteration = 0; iteration < iterations; iteration++)
{
// move ants
foreach (var ant in ants)
{
var x = (random.Next(3) - 1) + ant.Position.X;
var y = (random.Next(3) - 1) + ant.Position.Y;
ant.Move(x, y);
}
// animate the ant
Debug.WriteLine(ants[0].Position.X);
this.Dispatcher.Invoke((Action)(() =>
{
Canvas.SetLeft(ant1, ants[0].Position.X);
}));
}
});
内容总结
以上是互联网集市为您收集整理的C#-在WPF中更新画布全部内容,希望文章能够帮你解决C#-在WPF中更新画布所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。