Silverlight:如何动态绘制图像?

发布于 2024-08-22 16:22:59 字数 1111 浏览 4 评论 0原文

s.jpg 通过 BuildAction==Resource 添加到解决方案中。

XAML

<UserControl x:Class=...>
    <Canvas x:Name="LayoutRoot">            
    </Canvas>
</UserControl>

CS

Image Model = new Image(); 
Model.Source = new BitmapImage(new Uri("/s.jpg", UriKind.Relative)); 
LayoutRoot.SetLeft(Model, Coor.X); 
LayoutRoot.SetTop(Model, Coor.Y); 
Model.Width = 50; 
Model.Height = 30; 
LayoutRoot.Children.Add(Model);

这是我的问题:我的图像是什么?它没有出现在屏幕上,但如果我将 XAML 更改为

<UserControl x:Class=...>
    <Canvas x:Name="LayoutRoot">
        <Image Source="s.jpg"></Image>
    </Canvas>
</UserControl>

您可以看到它,如果将 CS 更改为

Ellipse Model = new Ellipse(); 
Model.Fill = new SolidColorBrush(Colors.Gray);
Model.Stroke = new SolidColorBrush(Colors.Blue); 
LayoutRoot.SetLeft(Model, Coor.X); 
LayoutRoot.SetTop(Model, Coor.Y); 
Model.Width = 50; 
Model.Height = 30; 
LayoutRoot.Children.Add(Model);

我也会看到该椭圆。那么问题出在哪里呢?

s.jpg added to solution with BuildAction==Resource.

XAML

<UserControl x:Class=...>
    <Canvas x:Name="LayoutRoot">            
    </Canvas>
</UserControl>

CS

Image Model = new Image(); 
Model.Source = new BitmapImage(new Uri("/s.jpg", UriKind.Relative)); 
LayoutRoot.SetLeft(Model, Coor.X); 
LayoutRoot.SetTop(Model, Coor.Y); 
Model.Width = 50; 
Model.Height = 30; 
LayoutRoot.Children.Add(Model);

Here's my question: were is my image? It has't appeared on the screen, though if i change XAML to

<UserControl x:Class=...>
    <Canvas x:Name="LayoutRoot">
        <Image Source="s.jpg"></Image>
    </Canvas>
</UserControl>

you can see it, also if change CS to

Ellipse Model = new Ellipse(); 
Model.Fill = new SolidColorBrush(Colors.Gray);
Model.Stroke = new SolidColorBrush(Colors.Blue); 
LayoutRoot.SetLeft(Model, Coor.X); 
LayoutRoot.SetTop(Model, Coor.Y); 
Model.Width = 50; 
Model.Height = 30; 
LayoutRoot.Children.Add(Model);

i'll see that ellipse. so what the problem?

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

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

发布评论

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

评论(1

苏佲洛 2024-08-29 16:22:59

尝试:

ImageSource imgSrc = new BitmapImage(new Uri("http://server/path/s.jpg", UriKind.RelativeOrAbsolute));

LayoutRoot.Children.Add(imgSrc);
LayoutRoot.UpdateLayout();

Try:

ImageSource imgSrc = new BitmapImage(new Uri("http://server/path/s.jpg", UriKind.RelativeOrAbsolute));

LayoutRoot.Children.Add(imgSrc);
LayoutRoot.UpdateLayout();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文