2017-07-20 51 views
0

我使用Xamarin中的網格創建了一個用戶界面,我已將它放入StackLayout。 以下是xaml的以下代碼。Xamarin - XAML網格用戶界面互相覆蓋

program.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="LoginPage.Page"> 
    <ContentPage.Content> 
     <StackLayout Orientation="Vertical"> 
      <StackLayout BackgroundColor="#3a4851"> 
       <Label Text="Identifications" TextColor="White" FontSize="15" Margin="10" /> 
      </StackLayout> 
      <StackLayout> 
       <Grid x:Name="identificationGridLayout" HorizontalOptions="Center"></Grid> 
      </StackLayout> 
      <StackLayout BackgroundColor="#3a4851"> 
       <Label Text="Deciles" TextColor="White" FontSize="15" Margin="10" /> 
      </StackLayout> 
      <StackLayout> 
       <Grid x:Name="decileGridLayout" HorizontalOptions="Center"></Grid> 
      </StackLayout> 
     </StackLayout> 
    </ContentPage.Content> 
</ContentPage> 

我創建網格頭和數據編程。

的Program.cs

public partial class Page : ContentPage 
{ 
    private LoadingPopUp popUp; 
    private Repository repository = new Repository(); 
    private JObject jResult; 
    private List<string> rowHeader; 
    private List<PrescriberIdentitifcation> prescriberIdentificationValue; 
    private List<PrescriberDecile> prescriberDecileValue; 

    public Page() { InitializeComponent(); } 

    public Page(string guid) 
    { 
     InitializeComponent(); 
     FetchDataForDetail(guid); 
    } 

    async void FetchDataForDetail(string guid) 
    { 
     try 
     { 
      popUp = new LoadingPopUp("Loading data ..."); 
      await PopupNavigation.PushAsync(popUp); 
      //Get Identification Data for prescriber 
      JObject identificationResult = await GetRepositoryDetailData(guid); 

      var identificationdata = JsonConvert.DeserializeObject<PrescriberIdentificationList>(identificationResult.ToString()); 
      prescriberIdentificationValue = identificationdata.value; 
      int index = 0; 
      foreach (var obj in identificationResult["value"]) 
      { 
       prescriberIdentificationValue[index].vcm_type_name = (string)obj["[email protected]"]; 
       index++; 
      } 
      drawIdentificationGrid(); 

      //Get Deciles Data for prescriber 
      JObject decileResult = await GetRepositoryDetailData(guid); 
      var deciledata = JsonConvert.DeserializeObject<PrescriberIdentificationList>(decileResult.ToString()); 
      prescriberIdentificationValue = deciledata.value; 
      Debug.WriteLine("data prescriberIdentificationValue : " + prescriberIdentificationValue); 
      drawDecilesGrid();    
      await PopupNavigation.PopAllAsync(); 
     } 
     catch (Exception ex) { 
      Debug.WriteLine("error in identification loading : " + ex.Message); 
      await PopupNavigation.PopAllAsync(); 
     } 

    } 

    public async Task<JObject> GetRepositoryDetailData(string guid, string entity, string filter) 
    { 
     try 
     { 
      jResult = await repository.Retrieve(GlobalVariables.AuthToken, entity, filter + guid); 
      return jResult; 
     } 
     catch (Exception err) 
     { 
      Debug.WriteLine(err.Message); 
      await PopupNavigation.PopAllAsync(); 
      await DisplayAlert("Error", "Oops something went wrong", "Ok"); 
     } 
     return jResult; 
    } 

    void drawIdentificationGrid() 
    { 
     try 
     { 
      rowHeader = new List<string>{"Type", "Number", "License State", "Sampleability","Expiration Date","License Status" }; 
      drawGridHeader(identificationGridLayout,1, rowHeader.Count,rowHeader); 

      for (int rowIndex = 1; rowIndex <= prescriberIdentificationValue.Count; rowIndex++) 
      { 
       var datatypeLabel = new Label { Text = prescriberIdentificationValue[rowIndex-1].vcm_type_name.ToString(),FontSize=12, VerticalOptions = LayoutOptions.Center, HorizontalOptions = LayoutOptions.Center }; 
       var datanumberLabel = new Label { Text = prescriberIdentificationValue[rowIndex-1].vcm_name.ToString(),FontSize=12, VerticalOptions = LayoutOptions.Center, HorizontalOptions = LayoutOptions.Center }; 
       var datalicenseStateLabel = new Label { Text = (prescriberIdentificationValue[rowIndex-1]._vcm_licensestate_value != null) ? prescriberIdentificationValue[rowIndex-1]._vcm_licensestate_value : "-" , FontSize=12, VerticalOptions = LayoutOptions.Center, HorizontalOptions = LayoutOptions.Center }; 
       var datasampleabiltiyLabel = new Label { Text = (prescriberIdentificationValue[rowIndex - 1].vcm_sampleability == false) ? "No": "Yes", FontSize=12, VerticalOptions = LayoutOptions.Center, HorizontalOptions = LayoutOptions.Center }; 
       var dataexpirationDateLabel = new Label { Text = (prescriberIdentificationValue[rowIndex-1].vcm_expirationdate != null) ? prescriberIdentificationValue[rowIndex-1].vcm_expirationdate : "-", FontSize=12, VerticalOptions = LayoutOptions.Center, HorizontalOptions = LayoutOptions.Center }; 
       Debug.WriteLine("data datatypeLabel {0} datanumberLabel {1} datalicenseStateLabel {2} datasampleabiltiyLabel {3} dataexpirationDateLabel {4} rowIndex {5}",datatypeLabel.Text,datanumberLabel.Text,datalicenseStateLabel.Text, datasampleabiltiyLabel.Text, dataexpirationDateLabel.Text, rowIndex); 

       identificationGridLayout.Children.Add(datatypeLabel, 0, rowIndex); 
       identificationGridLayout.Children.Add(datanumberLabel, 1, rowIndex); 
       identificationGridLayout.Children.Add(datalicenseStateLabel, 2, rowIndex); 
       identificationGridLayout.Children.Add(datasampleabiltiyLabel, 3, rowIndex);  
       identificationGridLayout.Children.Add(dataexpirationDateLabel, 4, rowIndex); 

      } 

     } 
     catch (Exception ex) { Debug.WriteLine("Error in drawing Identifications: "+ ex.Message);} 

    } 

    void drawDecilesGrid() 
    { 
     try 
     { 
      rowHeader = new List<string>{"Quarter", "Decile Type", "Decile", "Rank","Organization Type" }; 
       drawGridHeader(decileGridLayout,1, rowHeader.Count, rowHeader); 

      //for (int rowIndex = 1; rowIndex <= prescriberIdentificationValue.Count; rowIndex++) 
      //{ 
      // var datatypeLabel = new Label { Text = prescriberIdentificationValue[rowIndex - 1].vcm_type_name.ToString(), FontSize = 12, VerticalOptions = LayoutOptions.Center, HorizontalOptions = LayoutOptions.Center }; 
      // var datanumberLabel = new Label { Text = prescriberIdentificationValue[rowIndex - 1].vcm_name.ToString(), FontSize = 12, VerticalOptions = LayoutOptions.Center, HorizontalOptions = LayoutOptions.Center }; 
      // var datalicenseStateLabel = new Label { Text = (prescriberIdentificationValue[rowIndex - 1]._vcm_licensestate_value != null) ? prescriberIdentificationValue[rowIndex - 1]._vcm_licensestate_value : "-", FontSize = 12, VerticalOptions = LayoutOptions.Center, HorizontalOptions = LayoutOptions.Center }; 
      // var datasampleabiltiyLabel = new Label { Text = (prescriberIdentificationValue[rowIndex - 1].vcm_sampleability == false) ? "No" : "Yes", FontSize = 12, VerticalOptions = LayoutOptions.Center, HorizontalOptions = LayoutOptions.Center }; 
      // var dataexpirationDateLabel = new Label { Text = (prescriberIdentificationValue[rowIndex - 1].vcm_expirationdate != null) ? prescriberIdentificationValue[rowIndex - 1].vcm_expirationdate : "-", FontSize = 12, VerticalOptions = LayoutOptions.Center, HorizontalOptions = LayoutOptions.Center }; 
      // Debug.WriteLine("data datatypeLabel {0} datanumberLabel {1} datalicenseStateLabel {2} datasampleabiltiyLabel {3} dataexpirationDateLabel {4} rowIndex {5}",datatypeLabel.Text,datanumberLabel.Text,datalicenseStateLabel.Text, datasampleabiltiyLabel.Text, dataexpirationDateLabel.Text, rowIndex); 
      // identificationGridLayout.Children.Add(datatypeLabel, 0, rowIndex); 
      // identificationGridLayout.Children.Add(datanumberLabel, 1, rowIndex); 
      // identificationGridLayout.Children.Add(datalicenseStateLabel, 2, rowIndex); 
      // identificationGridLayout.Children.Add(datasampleabiltiyLabel, 3, rowIndex); 
      // identificationGridLayout.Children.Add(dataexpirationDateLabel, 4, rowIndex); 

      //} 

     } 
     catch (Exception ex) { Debug.WriteLine("Error in drawing Identifications: "+ ex.Message);} 
    } 

    void drawGridHeader(Grid gridLayout, int rowIndexLength, int columnIndexLength, List<string> rowHeaders) 
    { 
     try 
     { 
      gridLayout.RowDefinitions = new RowDefinitionCollection(); 
      gridLayout.ColumnDefinitions = new ColumnDefinitionCollection(); 

      for (int rowIndex = 0; rowIndex < rowIndexLength; rowIndex++) 
      { 
       gridLayout.RowDefinitions.Add(new RowDefinition()); 
      } 
      for (int columnIndex = 0; columnIndex <= columnIndexLength; columnIndex++) 
      { 
       gridLayout.ColumnDefinitions.Add(new ColumnDefinition()); 
      } 

      for (int i = 0; i < columnIndexLength; i++) 
      { 
       var label = new Label { Text = rowHeaders[i], FontSize = 14, VerticalOptions = LayoutOptions.Center, HorizontalOptions = LayoutOptions.Center }; 
       identificationGridLayout.Children.Add(label, i, 0); 
      } 

     } 
     catch (Exception ex) { Debug.WriteLine("Error in drawing grid header: "+ ex.Message);} 
    } 
} 

}

drawGridHeader()是常見的用於不同網格創建報頭。單元格的數據通過不同的函數傳遞,並將網格引用傳遞給它。

例如: -

rowHeader = new List<string>{"Quarter", "Decile Type", "Decile", "Rank","Organization Type" }; 
drawGridHeader(decileGridLayout,1, rowHeader.Count, rowHeader); 

但獲取數據後,網格是在對方的寫作。

enter image description here

我試着用行項目創建人工電網,他們是爲了正確地堆放,但這樣做編程時,網格在彼此堆疊。

它應該低於Label Decile。

回答

0

裏面的最後一個for循環drawGridHeader(),你不引用添加標籤時,通過電網:

for (int i = 0; i < columnIndexLength; i++) 
{ 
    var label = new Label { Text = rowHeaders[i], FontSize = 14, VerticalOptions = LayoutOptions.Center, HorizontalOptions = LayoutOptions.Center }; 
    // This references the wrong Grid 
    // identificationGridLayout.Children.Add(label, i, 0); 
    gridLayout.Children.Add(label, i, 0); 
} 
+0

完全同意。我怎麼錯過這張支票。謝謝@Jon – beingsrv