2017-03-13 12 views
0

它會從代碼隱藏firstGridsecondGrid對象恢復。 但是這些都在DataTemplate中,因爲我可以從代碼隱藏中回想起它們?調用變量從DataTemplate內部的代碼隱藏與Xamarin形式

謝謝。

這裏是我的代碼:

<control:CarouselView x:Name="carouselView"> 
          <control:CarouselView.ItemTemplate> 
           <DataTemplate> 
            <Grid> 
             <Grid.ColumnDefinitions> 
              <ColumnDefinition Width="*"></ColumnDefinition> 
             </Grid.ColumnDefinitions> 
             <Grid.RowDefinitions> 
              <RowDefinition Height="46*"></RowDefinition> 
              <RowDefinition Height="0.2*"></RowDefinition> 
              <RowDefinition Height="53.8*"></RowDefinition> 
             </Grid.RowDefinitions> 
             <!-- griglia del carousel, tutta la struttura inizia qui--> 
             <Grid Grid.Row="0"> 
              <Grid.ColumnDefinitions> 
               <ColumnDefinition Width="10*"></ColumnDefinition> 
               <ColumnDefinition Width="35*"></ColumnDefinition> 
               <ColumnDefinition Width="45*"></ColumnDefinition> 
               <ColumnDefinition Width="10*"></ColumnDefinition> 
              </Grid.ColumnDefinitions> 
              <Grid.RowDefinitions> 
               <RowDefinition Height="*"></RowDefinition> 
              </Grid.RowDefinitions> 
              <Button Image="backBlueArrow.png" BorderColor="Transparent" BackgroundColor="Transparent" Clicked="clickmePrev" Grid.Column="0"></Button> 
              <!--<Image Source="backBlueArrow.png" x:Name="backClickArrow" Aspect="AspectFit" Grid.Column="0"></Image>--> 
              <StackLayout Margin="0,0,10,0" VerticalOptions="Center" HorizontalOptions="End" Grid.Column="1"> 
               <Image HeightRequest="70" Source="{Binding Immagine}" Aspect="AspectFit"></Image> 
              </StackLayout> 
              <!-- griglia superiore, riprende i dati tra le barre bianche--> 
              <Grid Grid.Row="0" Grid.Column="2"> 
               <Grid.ColumnDefinitions> 
                <ColumnDefinition Width="*"></ColumnDefinition> 
               </Grid.ColumnDefinitions> 
               <Grid.RowDefinitions> 
                <RowDefinition Height="40*"></RowDefinition> 
                <RowDefinition Height="60*"></RowDefinition> 
               </Grid.RowDefinitions> 
               <Label Text="{Binding Descrizione}" Margin="{Binding Margine}" TextColor="#fff" FontSize="Small" VerticalTextAlignment="End" HorizontalTextAlignment="Start" Grid.Row="0"/> 
               <Label Text="{Binding Valore}" FontSize="Large" TextColor="#fff" VerticalTextAlignment="Start" HorizontalTextAlignment="Start" Grid.Row="1"/> 
               <Image Margin="-25,-5,0,0" Source="{Binding ImmagineBatteriaEmoticon}" HeightRequest="50" x:Name="emoticonBattery" Grid.Row="1" VerticalOptions="Start" HorizontalOptions="Start"></Image> 
              </Grid> 
              <Button Image="nextBlueArrow.png" BorderColor="Transparent" BackgroundColor="Transparent" Clicked="clickmeNext" Grid.Column="3"></Button> 
              <!--<Image Source="nextBlueArrow.png" x:Name="nextClickArrow" Aspect="AspectFit" Grid.Column="3"></Image>--> 
             </Grid> 
             <!-- griglia fare la riga bianca --> 
             <Grid BackgroundColor="White" Grid.Row="1"> 
              <Grid.ColumnDefinitions> 
               <ColumnDefinition Width="*"></ColumnDefinition> 
              </Grid.ColumnDefinitions> 
              <Grid.RowDefinitions> 
               <RowDefinition Height="*"></RowDefinition> 
              </Grid.RowDefinitions> 
             </Grid> 
             <!-- griglia inferiore, ritrare i dati dei testi --> 
             <ScrollView Grid.Row="2"> 
              <Grid Padding="5" Grid.Row="2" x:Name="firstGrid"> 
               <Grid.ColumnDefinitions> 
                <ColumnDefinition Width="65*"></ColumnDefinition> 
                <ColumnDefinition Width="35*"></ColumnDefinition> 
               </Grid.ColumnDefinitions> 
               <Grid.RowDefinitions> 
                <RowDefinition Height="*"></RowDefinition> 
                <RowDefinition Height="*"></RowDefinition> 
                <RowDefinition Height="*"></RowDefinition> 
                <RowDefinition Height="*"></RowDefinition> 
                <RowDefinition Height="*"></RowDefinition> 
                <RowDefinition Height="*"></RowDefinition> 
                <RowDefinition Height="*"></RowDefinition> 
                <RowDefinition Height="*"></RowDefinition> 
               </Grid.RowDefinitions> 
               <Label HorizontalTextAlignment="Start" TextColor="#5B6667" Grid.Column="0" Grid.Row="0" Text="{Binding Testo1}"></Label> 
               <Label HorizontalTextAlignment="End" TextColor="#5B6667" Grid.Column="1" Grid.Row="0" Text="{Binding Valore1}"></Label> 
               <Label HorizontalTextAlignment="Start" TextColor="#5B6667" Grid.Column="0" Grid.Row="1" Text="{Binding Testo2}"></Label> 
               <Label HorizontalTextAlignment="End" TextColor="#5B6667" Grid.Column="1" Grid.Row="1" Text="{Binding Valore2}"></Label> 
               <Label HorizontalTextAlignment="Start" TextColor="#5B6667" Grid.Column="0" Grid.Row="2" Text="{Binding Testo3}"></Label> 
               <Label HorizontalTextAlignment="End" TextColor="#5B6667" Grid.Column="1" Grid.Row="2" Text="{Binding Valore3}"></Label> 
               <Label HorizontalTextAlignment="Start" TextColor="#5B6667" Grid.Column="0" Grid.Row="3" Text="{Binding Testo4}"></Label> 
               <Label HorizontalTextAlignment="End" TextColor="#5B6667" Grid.Column="1" Grid.Row="3" Text="{Binding Valore4}"></Label> 
               <Label HorizontalTextAlignment="Start" TextColor="#5B6667" Grid.Column="0" Grid.Row="4" Text="{Binding Testo5}"></Label> 
               <Label HorizontalTextAlignment="End" TextColor="#5B6667" Grid.Column="1" Grid.Row="4" Text="{Binding Valore5}"></Label> 
               <Label HorizontalTextAlignment="Start" TextColor="#5B6667" Grid.Column="0" Grid.Row="5" Text="{Binding Testo6}"></Label> 
               <Label HorizontalTextAlignment="End" TextColor="#5B6667" Grid.Column="1" Grid.Row="5" Text="{Binding Valore6}"></Label> 
               <Label HorizontalTextAlignment="Start" TextColor="#5B6667" Grid.Column="0" Grid.Row="6" Text="{Binding Testo7}"></Label> 
               <Label HorizontalTextAlignment="End" TextColor="#5B6667" Grid.Column="1" Grid.Row="6" Text="{Binding Valore7}"></Label> 
               <Label HorizontalTextAlignment="Start" TextColor="#5B6667" Grid.Column="0" Grid.Row="7" Text="{Binding Testo8}"></Label> 
               <Label HorizontalTextAlignment="End" TextColor="#5B6667" Grid.Column="1" Grid.Row="7" Text="{Binding Valore8}"></Label> 
              </Grid> 
              <RelativeLayout Grid.Row="2" x:Name="secondGrid" BackgroundColor="Fuchsia" HeightRequest="50" WidthRequest="100"> 
               <Label Text="ciao"></Label> 
              </RelativeLayout> 
             </ScrollView> 
            </Grid> 
            <!--<Label Text="{Binding Testo}" />--> 
           </DataTemplate> 
          </control:CarouselView.ItemTemplate> 
         </control:CarouselView> 

警告:在此之前,被複制的代碼是Xamarin形成PCL。它與c#和WPF略有不同。

感謝

+0

你想用電網做什麼?也許我們可以找到一個解決方法 – Daniel

+0

我必須給她回電話以根據旋轉木馬的索引隱藏它。 –

+0

添加屬性isVisible到您的viewmodel並相應地設置 – Daniel

回答

1

當你使用一個DataTemplate,綁定關注的對象,你有約束力,而不是您可能使用的視圖模型。

但是不要害怕,因爲Xamarin爲我們提供了附加處理這種物體的方法!

比方說,你要點擊你的形象:

<Button Image="nextBlueArrow.png" BorderColor="Transparent" BackgroundColor="Transparent" Clicked="clickmeNext" Grid.Column="3"></Button> 

有可能成爲

<Image Source="nextBlueArrow.png"> 
    <Image.GestureRecognizers> 
    <TapGestureRecognizer 
     Tapped="clickmeNext" 
     CommandParameter="{Binding .}" /> 
    </Image.GestureRecognizers> 
</Image> 

隨着clickmeNext方法在後面的代碼是這樣的:

public async void clickmeNext(object sender, EventArgs e) 
     { 
      Image imageSender = (Image)sender; 
      YourObject yourObject= (YourObject)imageSender.BindingContext; 

      //some actions on your instance of YourObject, 
      //which is the instance linked to the image 
     } 

如果我這樣做不回答你的問題,不要猶豫,以增加你的問題,使其更清楚。

+0

是的,在這裏他講的是後面的代碼,而不是ViewModel,但是你展示的方法應該可以工作...... :) –

+0

當你點擊...不要爲我 –