0

我想Grid在后面的代码中设置背景图像。我在互联网上发现我们可以像这样使用 XAML。

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Pages.PhotoPage">
    <Grid >
        <Image Source="background.png" Aspect="AspectFit" />
        <!-- Place here the rest of the layout for the page. -->
    </Grid >

但是我怎样才能在后面的代码中设置它。我看不到任何ImageBackgroundImage财产Grid。请帮我。谢谢

4

1 回答 1

1

您可以在 中创建一个空Image元素Grid并进行设置。

<Grid>
    <Image x:Name="BackgroundImage" Aspect="AspectFit" />
    <!-- Place here the rest of the layout for the page. -->
</Grid>

现在在代码中设置它:

backgroundImage.Source = ...;

如果您在代码中构建整个 UI,您也可以这样做:

var myGrid = new Grid();
var backgroundImage = new Image();
backgroundImage.Source = ...;
myGrid.Children.Add( backgroundImage );

如果您Grid有多个行和列,您将需要在图像上设置Grid.ColumnSpanGrid.RowSpan属性以使其跨越整个网格。

于 2018-05-21T08:56:54.723 回答