【问题标题】:Silverlight Without XAML - Images Will Not Render没有 XAML 的 Silverlight - 图像不会呈现
【发布时间】:2010-06-30 04:05:27
【问题描述】:

我有一个未使用 XAML 的 Silverlight 应用程序。我在 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,则以下工作:

xaml:

<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)。

据我了解,不应该有 XAML 要求。我有什么遗漏吗?

【问题讨论】:

    标签: silverlight xaml


    【解决方案1】:

    不同之处在于,在第一种情况下,您将RootVisual 设置为Grid,但在第二种情况下,您的网格是子元素。

    RootVisual 属性的 MSDN 页面上,它显示了以下示例:

    this.RootVisual = new Page();
    

    因此,如果您创建一个 Page,然后将您的 Grid 添加到该页面,它应该可以工作。

    Page page = new Page();
    page.Content = g;
    this.RootVisual = page;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-04-29
      • 1970-01-01
      • 1970-01-01
      • 2020-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多