【问题标题】:Image displays differently in XAML vs. C#图像在 XAML 与 C# 中的显示方式不同
【发布时间】:2014-05-12 07:55:25
【问题描述】:

我想在网格中显示图像

Only the image shows 当我使用这个 XAML 时:

<Image Source="/Assets/O.png"
       Grid.Column="6"
       Grid.Row="5"/>

在 C# 中,它显示带有边框的图片:

Image img = new Image();
BitmapImage bi = new BitmapImage();
bi.UriSource = new Uri("/Assets/O.png", UriKind.Relative);
img.Stretch = Stretch.Fill;
img.Source = bi;
Grid.SetColumn(img, 6);
Grid.SetRow(img, 7);
gridGameBoard.Children.Add(img);

Image img1 = new Image();
img1.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("/Assets/X.png", UriKind.Relative));
Grid.SetColumn(img1, 4);
Grid.SetRow(img1, 4);
gridGameBoard.Children.Add(img1);

我尝试在谷歌上搜索 BitmapImage 和其他图像类型之间的差异,但没有成功。

【问题讨论】:

    标签: c# image xaml windows-phone-8


    【解决方案1】:

    问题在于您的ImageStretch 属性。在 XAML 中,您不定义它,因此使用默认值 Uniform。在 C# 代码中,您有 img.Stretch = Stretch.Fill;

    我的猜测是边框在图像中,但在带有 Fill 的 C# 代码中,图像被缩小,带有 Uniform 的 XAML 变体放大图像并将其居中,因此边框不可见。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-13
    • 2014-10-12
    • 2018-11-13
    • 1970-01-01
    • 2016-12-12
    • 1970-01-01
    相关资源
    最近更新 更多