动态添加的控件不渲染,silverlight

发布于 2024-11-26 18:36:34 字数 1166 浏览 3 评论 0原文

你好,我有一个名为布局的画布。我想在后面的代码中添加一些子项。这是代码。

private void AddItem(int TruePosition, int CurrentPosition, string ImageFileName)
{
    Image img = new Image();
    img.Source = new BitmapImage(new Uri(@"/Images/" + ImageFileName, UriKind.Relative));
    img.Width = 100.0;
    img.Height = 400.0;
    img.VerticalAlignment = System.Windows.VerticalAlignment.Bottom;
    img.Stretch = Stretch.None;

    Border b = new Border();
    b.SetValue(Canvas.TopProperty, 200.0);
    b.SetValue(Canvas.LeftProperty, (double)CurrentPosition);
    b.SetValue(Canvas.ZIndexProperty, 1);
    b.Background = new SolidColorBrush(Colors.Blue);

    b.MouseMove += new MouseEventHandler(Border_MouseMove);
    b.MouseLeftButtonDown += new MouseButtonEventHandler(Border_MouseLeftButtonDown);
    b.MouseLeftButtonUp += new MouseButtonEventHandler(Border_MouseLeftButtonUp);

    b.Child = img;
    Layout.Children.Add(b);
    UpdateLayout();
}

图像 Uri 可能很好,因为如果我尝试仅添加边框(没有图像),它仍然不会渲染。另外,我从按钮单击事件处理程序中调用此 AddItem 函数,因此初始化应该不是问题。可能我在这里缺少一些非常基本的东西。欢迎任何想法,谢谢您的帮助。

更新:好吧,我的浏览器缓存可能有问题,添加了边框,但边框中的图像没有显示。在 Visual Studio 项目中,我有一个包含图像文件的图像目录。我是否设置了错误的路径?

Hello i have a canvas with the name Layout. I want to add some childs to it in code behind. Here is the code.

private void AddItem(int TruePosition, int CurrentPosition, string ImageFileName)
{
    Image img = new Image();
    img.Source = new BitmapImage(new Uri(@"/Images/" + ImageFileName, UriKind.Relative));
    img.Width = 100.0;
    img.Height = 400.0;
    img.VerticalAlignment = System.Windows.VerticalAlignment.Bottom;
    img.Stretch = Stretch.None;

    Border b = new Border();
    b.SetValue(Canvas.TopProperty, 200.0);
    b.SetValue(Canvas.LeftProperty, (double)CurrentPosition);
    b.SetValue(Canvas.ZIndexProperty, 1);
    b.Background = new SolidColorBrush(Colors.Blue);

    b.MouseMove += new MouseEventHandler(Border_MouseMove);
    b.MouseLeftButtonDown += new MouseButtonEventHandler(Border_MouseLeftButtonDown);
    b.MouseLeftButtonUp += new MouseButtonEventHandler(Border_MouseLeftButtonUp);

    b.Child = img;
    Layout.Children.Add(b);
    UpdateLayout();
}

The image Uri is probably good because if i try to add only the border(without the image) it's still not rendering. Also i call this AddItem function from a button click event handler so initializations should not be a problem. Probably i am missing a very basic stuff here. Any ideas are welcome, thank you for your help.

Update: Ok i had something wrong with the browser cache probably, the border is added, but the image in the border isn't showing up. In the Visual Studio project i have an Images dir with the image files. Am i setting the path to them wrong?

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

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

发布评论

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

评论(1

贩梦商人 2024-12-03 18:36:34

所以,现在我可以回答这个问题了。好吧,这是一个新手错误。
不需要第一个/之前的图像。 new Uri(@"Images/" + ImageFileName, UriKind.Relative) 是正确的。

So, now i am allowed to answer it. Well it was a newbie mistake.
The first / before Images is not needed. new Uri(@"Images/" + ImageFileName, UriKind.Relative) is correct.

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