WPF - C# - 在代码后面创建和设置图像坐标 [英] WPF - C# - Creating and setting image coordinates in code - behind

查看:291
本文介绍了WPF - C# - 在代码后面创建和设置图像坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我的AI兴趣,我目前正在WPF应用程序上工作。
我写了一个统一成本搜索算法(寻路),并希望以图形方式呈现它。
路径应该被找到并显示在一个可以被用户调整的图表上。



我在WPF技术方面很新,我在WinForms和现在在创建和管理图形元素时遇到了问题。换句话说,我希望有机会点击数据网格并创建自己的节点(比如地点一张图),当你有几个节点时,你可以选择其中的两个并连接它们进行连接,最后你可以选择你的起点和终点,算法将显示最短路径(颜色合适连接)。



就是这样。



带界面的图片



我开始添加一个CreateNode方法来获取点击坐标并创建一个点右X和Y.现在在这个特定的地方创建一个图像时出现问题。



我读了一些questi ons on Stack,并试图用Image和BitmapImge类编写某些东西,但仍不知道如何将其放置在特定位置。
我读过关于操纵利润率的问题,但是没有更简单的解决方案吗?

以下是图片加载代码的一部分:

  public void CreateNode(Node n)
{
Point point = new Point(nX,nY);

Image node = new Image();
BitmapImage logo = new BitmapImage();
logo.BeginInit();
logo.UriSource = new Uri(point.png);
logo.CacheOption = BitmapCacheOption.OnLoad;
logo.EndInit();

node.Source = logo;





$ b如果有人有任何想法如何创建这些方法的情况下,我会真的很赞赏。



预先致谢

Paweł



编辑:据说我为我的代码问题创建了新的主题,所以这里是 here

解决方案

将我的评论移至提供更多信息的答案。 / p>

通过定位,我建议您观看高级XAML技术。他使用自定义的ItemsControl和Canvas来绑定运送卡车的经度和纬度,以在地图上显示为图形。我认为他的一些技巧可以适用于你的情况(或者给你更多的想法)。我已经链接到相关部分开始的时间戳。



相关演示的源代码是>可在此处。查看活动 - 2014 TechEd欧洲 - DEV-B311 XAML技巧 - Demo05



与您正在做的视频的主要区别是他使用路径您想要使用图像的位置。您需要更改App.xaml.cs中的 DataTemplates 以使用图片。



视频假设一些有关MVVM的知识,所以如果这对你来说是新的,那么可能值得查看教程。您可以在这个问题的答案中找到一些建议。我还会推荐一本关于 Rachel Lim的博客。我发现她的教程非常有用。



关于WPF的第一个链接教程看起来像他们会对你有用。除了基础知识之外,它还包括在 DataTemplates 中使用图像。


I currently work on WPF application according to my AI interest. I wrote a Uniform Cost Search algorithm (pathfinding) and want to present it in graphical way. Path should be found and show on a graph which could be adjusted by user.

I'm quite new in WPF technology, I worked more with WinForms and now have a problem with creating and managing graphical elements.

In other words - I want to give opportunity to click on data grid and create your own node (sth like place on a map) which is represented by a picture, when you have a few nodes you can choose two of them and connect them to make a connection, finally you can select your start and end point and algorithm will show the shortest path (color the suitable connections).

That's it.

Image with interface

I started with adding a CreateNode method which gets click's coordinates and create a point with right X and Y. Now there is a problem with creating an image in that specific place.

I read some questions on Stack and tried to write something with Image and BitmapImge classes but still don't know how to place it in specific place. I read about manipulating margins but aren't there any easier solutions?

Here's part of that image loading code:

        public void CreateNode(Node n)
        {
        Point point = new Point(n.X, n.Y);

        Image node = new Image();
        BitmapImage logo = new BitmapImage();
        logo.BeginInit();
        logo.UriSource = new Uri("point.png");
        logo.CacheOption = BitmapCacheOption.OnLoad;
        logo.EndInit();

        node.Source = logo;
        }

If someone has any ideas how to create these methods in case of graphics I will really aprreciate that.

Thanks in advance

Paweł

EDIT: I was said to create new topic for my code problem so here is that here

解决方案

Moving my comment to an answer to provide a little more information.

With the positioning, I'd recommend watching this video on advanced XAML techniques. He uses a custom ItemsControl and Canvas to bind the longitude and latitude of delivery trucks to show as graphics on a map. I think some of his techniques could work in your situation (or give you further ideas). I've linked to the a timestamp at the start of the relevant section.

The source code for the relevant demo is available here. See Events - 2014 TechEd Europe - DEV-B311 XAML Techniques - Demo05

The main difference from what you're doing to the video is that he uses Paths where you want to use images. You'd need to change the DataTemplates in App.xaml.cs to use images.

The video assumes some knowledge of MVVM, so if that's new to you, it's probably worth checking out a tutorial. You can find some recommendations in the answers to this question. I'd also recommend one on Rachel Lim's Blog. I found her tutorials very useful.

The first linked tutorials on WPF look like they will be useful to you. In addition to the basics it includes using images in DataTemplates.

这篇关于WPF - C# - 在代码后面创建和设置图像坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆