【发布时间】:2016-12-06 18:04:54
【问题描述】:
我正在尝试在 xamarin.forms 中创建一个有点复杂的列表。用户应该能够单击列表中的一个项目,并且它应该扩展成更大的东西。较大的项目现在应该显示一些与该视图特别关联的额外 UI 组件。
我想知道我应该如何解决这个问题。这是关于具有动态大小项目的列表视图。单击该项目后,将显示与该项目相关的其他视图。这应该在 Xamarin.ios 和 Xamarin.droid 中完成,还是建议尝试在 xamarin.forms 中实现?
我会发布一张图片,它不是很好,可能需要放大一些,但它显示了 4 个项目。第三个已展开,因此您可以看到其上的微调器和按钮。
一次只能展开一个项目(我可能必须在 ViewModel 中处理它)并且在按下另一个项目时应该隐藏旧项目。
编辑:
多亏了 Rohit,我开始在 Xamarin.Forms 中实现一个解决方案,但它仍然不能正常工作,只是行扩展方式存在一些小问题。见下图。为了简单起见,我跳过了微调器,只使用了一个按钮。展开的行与自身下方的行重叠。第一张是点击前,第二张是点击“二”项后。
查看
<?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="simpletest.PlayGroundPage">
<ContentPage.Content>
<StackLayout>
<ListView VerticalOptions="FillAndExpand" HasUnevenRows="True" ItemsSource="{Binding AllItems}" SelectedItem="{Binding MySelectedItem}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout VerticalOptions="FillAndExpand">
<StackLayout VerticalOptions="FillAndExpand" Orientation="Horizontal">
<Label Text="{Binding MyText}" />
<Image Source="{Binding MyImage}" />
</StackLayout>
<Button Text="button1" IsVisible="{Binding IsExtraControlsVisible}" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage.Content>
</ContentPage>
物品
public class Item : INotifyPropertyChanged
{
public Item(string text, string image, int id)
{
_myText = text;
_myImage = image;
_id = id;
_isExtraControlsVisible = false;
}
private string _myText;
private string _myImage;
private bool _isExtraControlsVisible;
private int _id;
public event PropertyChangedEventHandler PropertyChanged;
public int Id { get { return _id; } set { _id = value; } }
public string MyText
{
get { return _myText; }
set { _myText = value; OnPropertyChanged("MyText"); }
}
public string MyImage
{
get { return _myImage; }
set { _myImage = value; OnPropertyChanged("MyImage"); }
}
public bool IsExtraControlsVisible
{
get { return _isExtraControlsVisible; }
set { _isExtraControlsVisible = value; OnPropertyChanged("IsExtraControlsVisible"); }
}
private void OnPropertyChanged(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
}
视图模型:
class PlayGroundViewModel : INotifyPropertyChanged
{
private Item _mySelectedItem;
public PlayGroundViewModel(ObservableCollection<Item> allItems)
{
AllItems = allItems;
_mySelectedItem = allItems.First();
}
public ObservableCollection<Item> AllItems { get; set; }
public Item MySelectedItem
{
get { return _mySelectedItem; } //Added a field for this one, mainly for debugging.
set
{
foreach (Item x in AllItems) //Changed to non-linq since it is not a list.
{
x.IsExtraControlsVisible = false;
}
if (value != null)
{
foreach (Item x in AllItems)
{
if (x.Id.Equals(value.Id))
{
x.IsExtraControlsVisible = true;
_mySelectedItem = x;
}
}
}
SetChangedProperty("MySelectedItem");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void SetChangedProperty(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
}
代码隐藏:
public partial class PlayGroundPage : ContentPage
{
public PlayGroundPage()
{
InitializeComponent();
ObservableCollection<Item> items = new ObservableCollection<Item>();
items.Add(new Item("One", "", 0));
items.Add(new Item("Two", "", 1));
items.Add(new Item("Three", "", 2));
PlayGroundViewModel weekViewModel = new PlayGroundViewModel(items);
BindingContext = weekViewModel;
}
}
【问题讨论】:
-
我以前做过这个。如果您使用 MVVM 并且您的 ItemsSource 绑定到 ViewModel 上的 IList,那么您只需将 Button 和 Spinner 的 IsVisible 属性绑定到 ViewModel 上的 2 个 bool 属性。当用户单击该项目时,这应该在您的 ViewModel 上调用 ICommand,您可以将这两个 IsVisible 属性设置为 true。
-
@Mangist 你能展示你的代码吗?
-
@JeremyThompson 你能先试试我的建议吗?
-
@Mangist 为什么?我不是 OP。
-
@JeremyThompson ?和?
标签: c# xamarin xamarin.ios xamarin.android xamarin.forms