2017-10-16 69 views
1

我在Xamarin.Forms中使用MasterDetailPage。我在OnAppearing()方法中設置了BackgroundImage屬性。它在Android中正常工作。但是,在IOS圖像中不顯示。MasterDetailPage背景圖像不顯示在IOS中xamarin.forms

我的代碼: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";   
    } 

圖片是在資源文件夾中。

回答

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> 
+0

仍然不能在IOS中工作。在Android中,它工作正常。 –

+0

我認爲您的圖片未正確添加到您的解決方案中。所以請從您的解決方案中刪除圖像並再次添加。 –

+0

我已刪除並再次添加。但仍然沒有工作。 –

0

您應該檢查圖像是否設置爲iOS項目作爲BundleResource,如果不是,您應該將其設置爲如此。我自己也遇到了一些麻煩。

enter image description here

+0

是的,我已經設置生成操作爲BundleResource.Still不工作! –