我的建议是使用选项卡式视图而不是选项卡式页面https://help.syncfusion.com/xamarin/tabbed-view/getting-started。
我有一个问题。我创建了一个有2个孩子的TabbedPage。我要在child1中显示所选的图像。在child2中,我有一个带有一些图像的网格。现在,我想使child2中的图像成为可选图像,并在单击它们时将它们插入child1中的StackLayout中。我不知道如何开始,所以有人可以给我一个例子吗?
这里是child1的xaml:
<ContentPage.Content>
<StackLayout BackgroundColor="White" x:Name="MainLayout" VerticalOptions="Center">
</StackLayout>
</ContentPage.Content>
这是child2的网格
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="10" />
<RowDefinition Height="100" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="10" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="10" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="10" />
</Grid.ColumnDefinitions>
<Image Grid.Row="1" Grid.Column="1" Source="Good_Question.png" VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand" />
<Image Grid.Row="1" Grid.Column="3" Source="Excuse_Me.png" />
</Grid>
有人可以给我一个例子,说明如何将在其他选项卡中选择的图像添加到TabbedPage中的另一个选项卡吗?
有多种方法可以这样做:
1种方式(简单):
在主页上创建public static ObservableCollection<Image> imageCollection= new ObservableCollection<Image>();
。
然后在第2页上:将选择的图像添加到imageCollection
中。
在第1页上:添加imageCollection.CollectionChanged += AddImageInsdeFirstPageLayout();
。每当在CollectionChanged
中添加/删除任何元素时,都会调用ObservableCollection
。
第二种方式(通过MVVM逻辑和首选方式工作)
:为所有三个页面(主页面和选项卡式页面)创建一个公共ViewModel。在其中创建一个属性,即>
:使用MessagingCenter。使用非常简单。如果您想进一步了解,请仔细阅读:Xamarin.Forms MessageCnterpublic ObservableCollection<Image> _selectedImages { get; set; } public ObservableCollection<Image> SelectedImages { get { return _selectedImages; } set { _selectedImages = value; OnPropertyChanged(); } }
[在第2页的此属性内添加新元素,然后从第1页访问此属性。确保您提高OnPropertyChanged
第三种方式(我不喜欢这样)
我的建议是使用选项卡式视图而不是选项卡式页面https://help.syncfusion.com/xamarin/tabbed-view/getting-started。
在两个子选项卡中都使用两个ListView,并且在选择第二个子选项卡的图像后,从ItemSelectedEvent中获取所选项目并将其绑定到第一个子选项卡的列表视图。并且,如果需要,可以从“第二个选项卡”的ListView中删除选定的项目。
由于同一页面中的两个列表视图,您都不必担心页面之间传递数据。
您可以使用MessageCenter转换图像源。
使用Google Xamarin api添加它,然后一切正常。
我的建议是使用选项卡式视图而不是选项卡式页面https://help.syncfusion.com/xamarin/tabbed-view/getting-started。
您可以使用MessageCenter转换图像源。