为什么要使用“new Pushpin()”创建先前图钉的引用?

发布于 2024-12-10 05:17:09 字数 985 浏览 5 评论 0原文

我想创建 2 个图钉,将它们添加到 Windows Phone 7 应用程序上的 bing 地图的图层中。 我创建了一个函数,用于创建具有一些预定义属性的图钉。 奇怪的是,当我使用此函数创建第一个引脚时
<代码> 图钉;
pin = new Pushpin(); 按预期工作。它创建了一个新的图钉... 但是,当此函数适用于第二个图钉时,上面的代码会创建对第一个图钉的引用...... 为什么?

我使用的代码如下

Pushpin pin0 = createDefaultPushpin(38.0, 23.0, "test0");
Pushpin pin1 = createDefaultPushpin(39.0, 24.0, "test1");


private Pushpin createDefaultPushpin(double lat, double lon, String name)
        {
            Pushpin pin;
            pin = new Pushpin();


            pin.Location.Latitude = lat;
            pin.Location.Longitude = lon;
            pin.Location.Altitude = 0;
            pin.Name = name;

            ScaleTransform st = new ScaleTransform();
            st.ScaleX = 0.25;
            st.ScaleY = 0.25;
            st.CenterX = 0;
            st.CenterY = 60;

            pin.RenderTransform = st;
            pin.Background = new SolidColorBrush(Colors.Blue);
            return pin;
        }

I want to create 2 pushpins to add them in a layer in bing maps on windows phone 7 app.
I have created a function that creates a pushpin with some predefined attributes.
The odd thing is that when I create the first pin using this function the

Pushpin pin;
pin = new Pushpin();
works as expected. It creates a new pushpin...
But when this functions works for the second pushpin the above code creates a reference to the first push pin...
Why?

The code i use is the bellow

Pushpin pin0 = createDefaultPushpin(38.0, 23.0, "test0");
Pushpin pin1 = createDefaultPushpin(39.0, 24.0, "test1");


private Pushpin createDefaultPushpin(double lat, double lon, String name)
        {
            Pushpin pin;
            pin = new Pushpin();


            pin.Location.Latitude = lat;
            pin.Location.Longitude = lon;
            pin.Location.Altitude = 0;
            pin.Name = name;

            ScaleTransform st = new ScaleTransform();
            st.ScaleX = 0.25;
            st.ScaleY = 0.25;
            st.CenterX = 0;
            st.CenterY = 60;

            pin.RenderTransform = st;
            pin.Background = new SolidColorBrush(Colors.Blue);
            return pin;
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清泪尽 2024-12-17 05:17:09

我从未使用过这些课程或这个平台,但让我们接受您上面所说的一切作为事实。那么我认为罪魁祸首一定是“位置”对象在您的三个图钉之间无意中共享。 (我认为 Pushpin 构造函数这样做是非常不负责任的,但同样,我只是根据您提供的证据,而不是我自己的经验)

您可以

pin.Location=new Location(lat, lon, 0);

在上面代码中的适当位置尝试一下,然后告诉我们会发生什么?

I've never used these classes or this platform, but let's accept everything that you've said above as fact. Then I think the culprit must be that the "Location" object is being inadvertently shared between your three Pushpins. (I think it's pretty irresponsible of the Pushpin constructor to do so, but again, I'm just going from the evidence you've provided, not my own experience)

Can you try

pin.Location=new Location(lat, lon, 0);

at the appropriate place in the code above, and tell us what happens?

筱果果 2024-12-17 05:17:09

与同事交谈后发现,Pushpin 类有一个附加到位置的依赖属性。这就是为什么它会有这样的行为。
解决方案是执行以下操作。

Pushpin pin = new Pushpin(){
                  Location = new Location(){
                      Latitude = lat,Longitude = lon, Altitude = 0
                  }
              };

After talking with colleagues, it turned out that the class Pushpin has a dependency property attached to location. That is why it behaves like that.
The solution is to do the following.

Pushpin pin = new Pushpin(){
                  Location = new Location(){
                      Latitude = lat,Longitude = lon, Altitude = 0
                  }
              };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文