Silverlight 没有 XAML - 图像不会呈现 [英] Silverlight Without XAML - Images Will Not Render
问题描述
我有一个 Silverlight 应用程序,我没有在其中使用 XAML.我在 Application_Startup 中有一个带有以下代码的基本应用程序:
I have a Silverlight application in which I'm not using XAML. I have a basic application with the following code in Application_Startup:
private void Application_Startup(object sender, StartupEventArgs e)
{
Grid g = new Grid();
g.Children.Add(new Image { Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("http://sstatic.net/so/img/sprites.png", UriKind.Absolute)) });
this.RootVisual = g;
}
此代码不会渲染指定的图像.但是,如果修改 App.Xaml 文件以在 Xaml 中定义 RootVisual,则以下工作:
This code will not render the specified image. If however, the App.Xaml file is modified to define the RootVisual in the Xaml the following works:
xml:
<Application.RootVisual>
<Grid>
</Grid>
</Application.RootVisual>
代码:
private void Application_Startup(object sender, StartupEventArgs e)
{
((Grid)this.RootVisual).Children.Add(new Image { Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("http://sstatic.net/so/img/sprites.png", UriKind.Absolute)) });
}
我不明白为什么一个会奏效而另一个不行.我也有使用 UserControl 的相同行为(当然使用 Content 而不是 Childern).
I don't see why one would work and the other not. I have the same behavior using a UserControl as well (using Content instead of Childern of course).
据我了解,应该没有 XAML 要求.有什么我遗漏的吗?
From what I understand, there should be not XAML requirement. Is there something I'm missing?
推荐答案
不同的是在第一种情况下,您将 RootVisual
设置为 Grid
,但在第二个你的网格是一个子元素.
The difference is in the first case you are setting the RootVisual
to be a Grid
, but in the second your grid is a child element.
在 RootVisual 属性,它显示了以下示例:
On the MSDN page for the RootVisual property it shows the following example:
this.RootVisual = new Page();
因此,如果您创建一个 Page
,然后将您的 Grid
添加到该页面,它应该可以工作.
so if you create a Page
and then add your Grid
to that page it should work.
Page page = new Page();
page.Content = g;
this.RootVisual = page;
这篇关于Silverlight 没有 XAML - 图像不会呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!