【问题标题】:MasterDetailPage background image not displaying in IOS in xamarin.formsMasterDetailPage 背景图像未在 xamarin.forms 的 IOS 中显示
【发布时间】:2017-10-16 07:46:02
【问题描述】:

我在 Xamarin.Forms 中使用 MasterDetailPage。我在 OnAppearing() 方法中设置了 BackgroundImage 属性。它在android中运行良好。但是,在 IOS 中图像不显示。

我的代码:XAML

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="EzySales.Views.MainPageMaster"
         Title="Master" >
<StackLayout VerticalOptions="FillAndExpand">
    <ListView x:Name="MenuItemsListView" WidthRequest="200"
          HasUnevenRows="true"
          ItemsSource="{Binding MenuItems}"
          SeparatorColor="White">
        <ListView.Header>
            <Grid >
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="10"/>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="10"/>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="30"/>
                    <RowDefinition Height="80"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="10"/>
                </Grid.RowDefinitions>
                <Image Source="logo.png" Grid.Row="1" Grid.Column="1"></Image>
            </Grid>
        </ListView.Header>
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell Grid.Row="2">
                    <StackLayout Padding="15,10" Orientation="Horizontal">
                        <Image Source="{Binding Icon}"
                                WidthRequest="30"
                                HeightRequest="30" Margin="0,0,5,0"
                                VerticalOptions="Center" />
                        <Label VerticalOptions="FillAndExpand" 
                            VerticalTextAlignment="Center" 
                            HorizontalTextAlignment="Center"                     
                            Text="{Binding Title}" 
                            TextColor="White"                    
                            FontSize="16" FontAttributes="Bold" />
                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</StackLayout>

MainPageMaster.xaml.cs

 protected override void OnAppearing()
    {
        this.BackgroundImage = "masterpagebg.png";           
    }

图像在资源文件夹中可用。

【问题讨论】:

    标签: c# xamarin.forms


    【解决方案1】:

    我也遇到了同样的问题并解决了。 试试这个希望它会有所帮助..

    您可以使用相对布局来实现结果

    <RelativeLayout>
        <Image Source="masterpagebg.png" Opacity="1"
                    RelativeLayout.WidthConstraint=
                      "{ConstraintExpression Type=RelativeToParent, Property=Width}"
                    RelativeLayout.HeightConstraint=
                      "{ConstraintExpression Type=RelativeToParent, Property=Height}"/>
    
           <ListView RelativeLayout.WidthConstraint=
                  "{ConstraintExpression Type=RelativeToParent, Property=Width}"
                RelativeLayout.HeightConstraint=
                  "{ConstraintExpression Type=RelativeToParent, Property=Height}" x:Name="MenuItemsListView" WidthRequest="200"
          HasUnevenRows="true"
          ItemsSource="{Binding MenuItems}"
          SeparatorColor="White">
        <ListView.Header>
            <Grid >
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="10"/>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="10"/>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="30"/>
                    <RowDefinition Height="80"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="10"/>
                </Grid.RowDefinitions>
                <Image Source="logo.png" Grid.Row="1" Grid.Column="1"></Image>
            </Grid>
        </ListView.Header>
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell Grid.Row="2">
                    <StackLayout Padding="15,10" Orientation="Horizontal">
                        <Image Source="{Binding Icon}"
                                WidthRequest="30"
                                HeightRequest="30" Margin="0,0,5,0"
                                VerticalOptions="Center" />
                        <Label VerticalOptions="FillAndExpand" 
                            VerticalTextAlignment="Center" 
                            HorizontalTextAlignment="Center"                     
                            Text="{Binding Title}" 
                            TextColor="White"                    
                            FontSize="16" FontAttributes="Bold" />
                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
      </RelativeLayout>
    

    【讨论】:

    • 我认为您的图像未正确添加到您的解决方案中。所以请从您的解决方案中删除图像并重新添加。
    • 我删了又加了。但还是不行。
    • 我已将 Listview 更改为 Label。图像显示,但是当我使用 Listview 时图像不显示。
    • 添加列表视图属性BackgroundColor="Transparent"
    • 在设置背景色后,@Ziyad Godil 现在可以完美运行了。谢谢
    【解决方案2】:

    您应该检查图像是否作为 BundleResource 设置为 iOS 项目,如果没有,则应将其设置为这样。我自己也遇到了一些麻烦。

    【讨论】:

    • 是的,我已将 Build Action 设置为 BundleResource。仍然无法正常工作!。
    猜你喜欢
    • 2019-02-17
    • 1970-01-01
    • 2014-11-13
    • 1970-01-01
    • 2020-02-05
    • 2019-02-11
    • 2019-08-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多