2016-04-13 68 views
0

我有一個標籤和使用taplace視單元創建的圖像的列表視圖。點擊圖片時,我想重定向到新頁面,例如頁面B,並顯示點擊項目的詳細信息。獲取列表視圖中的選定項目

image

下面是代碼:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Globalization; 
using System.Linq; 
using System.Reflection.Emit; 
using System.Text; 
using Xamarin.Forms; 

namespace ____ 
{ 
    public class SelectMultipleBasePage<T> : ContentPage 
    { 
     public class WrappedSelection<T> : INotifyPropertyChanged 
     { 
      public T Item { get; set; } 
      bool isSelected = false; 
      public bool IsSelected 
      { 
       get 
       { 
        return isSelected; 
       } 
       set 
       { 
        if (isSelected != value) 
        { 
         isSelected = value; 
         PropertyChanged(this, new PropertyChangedEventArgs("IsSelected")); 
         //      PropertyChanged (this, new PropertyChangedEventArgs (nameof (IsSelected))); // C# 6 
        } 
       } 
      } 
      public event PropertyChangedEventHandler PropertyChanged = delegate { }; 
     } 
     public class BackGroundColorConverter : IValueConverter 
     { 
      public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
      { 
       if (value is bool) 
       { 
        if ((bool)value) 
        { 
         return Color.FromHex("#DEE4EA"); 
        } 
        else 
        { 
         return Color.White; 
        } 
       } 
       else 
       { 
        return Color.White; 
       } 
      } 

      public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
      { 
       throw new NotImplementedException(); 
      } 

     } 

     public class WrappedItemSelectionTemplate : ViewCell 
     { 
      public WrappedItemSelectionTemplate() 
       : base() 
      { 
       Grid objGrid = new Grid(); 

       objGrid.RowDefinitions.Add(new RowDefinition 
       { 
        Height = new GridLength(1, GridUnitType.Star) 
       }); 

       objGrid.ColumnDefinitions.Add(new ColumnDefinition 
       { 
        Width = new GridLength(75, GridUnitType.Absolute), 
       }); 
       objGrid.ColumnDefinitions.Add(new ColumnDefinition 
       { 
        Width = new GridLength(1, GridUnitType.Star) 
       }); 
       objGrid.ColumnDefinitions.Add(new ColumnDefinition 
       { 
        Width = GridLength.Auto 
       }); 

       // 
       // Column 1:- 
       Image objImage = new Image(); 
       objImage.SetBinding(Image.SourceProperty, new Binding("Item.Image")); 
       objGrid.Children.Add(objImage, 0, 0); 


       // Column 2:- 
       StackLayout objStackLayoutCol2 = new StackLayout(); 
       objGrid.Children.Add(objStackLayoutCol2, 1, 0); 

       Label name = new Label() 
       { 
        Text = "Name", 
        Style = (Style)Application.Current.Resources["LabelStyle"], 
       }; 
       Label date = new Label() 
       { 
        Text = "Date", 
        Style = (Style)Application.Current.Resources["LabelStyleTiny"] 
       }; 
       name.SetBinding(Label.TextProperty, new Binding("Item.Name")); 
       date.SetBinding(Label.TextProperty, new Binding("Item.Date")); 
       objStackLayoutCol2.Children.Add(name); 
       objStackLayoutCol2.Children.Add(date); 
       objStackLayoutCol2.Padding = new Thickness(10); 


       Image objImageView = new Image(); 
       objImageView.GestureRecognizers.Add(new TapGestureRecognizer(OnTap)); 
       objImageView.Source = ImageSource.FromFile("Search.png"); 
       objGrid.Children.Add(objImageView, 2, 0); 

       objGrid.SetBinding(Grid.BackgroundColorProperty, "IsSelected", converter: new BackGroundColorConverter()); 

       // 
       // define context actions 
       // 
       var moreAction = new MenuItem { Text = "More" }; 

       moreAction.SetBinding(MenuItem.CommandParameterProperty, new Binding(".")); 
       moreAction.Clicked += (sender, e) => 
       { 
        var mi = ((MenuItem)sender); 
        //Debug.WriteLine("More Context Action clicked: " + mi.CommandParameter); 
       }; 

       var deleteAction = new MenuItem { Text = "Delete", IsDestructive = true }; // red background 
       deleteAction.Icon = Device.OnPlatform("Icons/cancel.png", "cancel.png", "Images/cancel.png"); 
       deleteAction.SetBinding(MenuItem.CommandParameterProperty, new Binding(".")); 
       deleteAction.Clicked += (sender, e) => 
       { 
        var mi = ((MenuItem)sender); 
        //Debug.WriteLine("Delete Context Action clicked: " + mi.CommandParameter); 
       }; 

       // 
       // add context actions to the cell 
       // 
       ContextActions.Add(moreAction); 
       ContextActions.Add(deleteAction); 
       //objGrid.Padding = new Thickness(10); 

       StackLayout st = new StackLayout(); 
       st.Children.Add(objGrid); 
       st.Children.Add(new BoxView() { Color = Color.FromHex("#A4B3C1"), WidthRequest = 100, HeightRequest = 1 }); 


       View = st; 


      } 

      public static void OnTap(View obj) 
      { 
       List<T> test= GetSelection(); 
       MessagingCenter.Send(new RedirectClass.OpenRecordingDetails(), RedirectClass.OpenRecordingDetails.Key); 
      } 
     } 
     public static List<WrappedSelection<T>> WrappedItems = new List<WrappedSelection<T>>(); 
     public SelectMultipleBasePage(List<T> items) 
     { 
      WrappedItems = items.Select(item => new WrappedSelection<T>() { Item = item, IsSelected = false }).ToList(); 
      ListView mainList = new ListView() 
      { 
       ItemsSource = WrappedItems, 
       ItemTemplate = new DataTemplate(typeof(WrappedItemSelectionTemplate)), 
      }; 

      mainList.ItemSelected += (sender, e) => 
      { 
       if (e.SelectedItem == null) return; 
       var o = (WrappedSelection<T>)e.SelectedItem; 
       o.IsSelected = !o.IsSelected; 
       ((ListView)sender).SelectedItem = null; //de-select 
      }; 
      Content = mainList; 
      mainList.HasUnevenRows = true; 


      if (Device.OS == TargetPlatform.WinPhone) 
      { // fix issue where rows are badly sized (as tall as the screen) on WinPhone8.1 
       mainList.RowHeight = 40; 
       // also need icons for Windows app bar (other platforms can just use text) 
       ToolbarItems.Add(new ToolbarItem("All", "check.png", SelectAll, ToolbarItemOrder.Primary)); 
       ToolbarItems.Add(new ToolbarItem("None", "cancel.png", SelectNone, ToolbarItemOrder.Primary)); 
      } 
      else 
      { 
       // mainList.MinimumHeightRequest = 80; 
       // mainList.RowHeight = 80; 
       ToolbarItems.Add(new ToolbarItem("All", null, SelectAll, ToolbarItemOrder.Primary)); 
       ToolbarItems.Add(new ToolbarItem("None", null, SelectNone, ToolbarItemOrder.Primary)); 
      } 
     } 
     void SelectAll() 
     { 
      foreach (var wi in WrappedItems) 
      { 
       wi.IsSelected = true; 
      } 
     } 
     void SelectNone() 
     { 
      foreach (var wi in WrappedItems) 
      { 
       wi.IsSelected = false; 
      } 
     } 
     public static List<T> GetSelection() 
     { 
      return WrappedItems.Where(item => item.IsSelected).Select(wrappedItem => wrappedItem.Item).ToList(); 
     } 
    } 
} 

我嘗試添加一個GestureRecognizers ONTAP但如果列表視圖中點擊任何地方其他然後一個圖像getselection返回的項目列表。

任何幫助?

編輯

我也嘗試添加這樣的事情:

   Button aButton = new Button { Text = "TestButton" }; 
       aButton.SetBinding(Button.CommandParameterProperty, new Binding("Item.Name")); 
       aButton.Command = new Command<Button>((Button theCellItem) => 
       { 
        var s = theCellItem.ToString(); 
        //DisplayAlert("Tadaa", theCellItem.ToString(), "Ok"); 
       }); 

,但它不能正常工作。收到錯誤:

System.InvalidCastException: Specified cast is not valid. 

回答

1

您可以通過IsEnabled屬性設置爲false。這將使列表視圖無法點擊,而在ViewCell GestureRecognizers將響應預期

For Navigation this can be of help

+0

我有ListView控件的多選功能。因此,無論何時用戶點擊列表項目(外部圖像視圖按鈕),多選功能都可以工作,但當用戶點擊圖像查看時,頁面應該重定向。 – Arti

+0

那麼問題是什麼?你可以從圖像選擇事件主體 –

+0

推動頁面導航堆棧但是,如果我禁用列表視圖選擇,那麼我的多選功能將如何工作?我在哪裏必須禁用列表視圖? – Arti

0

使用ListView控件的事件處理程序禁用列表視圖中選擇對於ItemTapped,然後在代碼隱藏中,您應該可以通過將對象參數轉換爲您的ListView中預期的類型來訪問該項目。

private void MyListView_OnItemTapped(object sender, ItemTappedEventArgs e) 
    { 
     var person = sender as Person; 
     Debug.WriteLine($"His name was {person.Name}"); 
    } 
+0

這不起作用。 'sender'的類型爲'ListView',不能將其轉換爲'Person'。 –

0

如果您使用包含您的控件的ListView ItemTemplate,用戶可以單擊項目上的任何位置(這似乎更方便)。

發件人是那麼你的ListView 所以你可以試試這個:

ListView listView = sender as ListView; 
Person selectedItem = listView == null ? null : listView.SelectedItem as Person; 
相關問題