我们如何识别Xamarin中 Picker 的 Selected Item 的视图?
我们有一个视图,即 SearchPage.xaml 。以及该视图的一个ViewModel,即 SearchPageVm.cs 。我们有多个选择器,并且在搜索页面中提供了控件。
我们有五个标签,即Tab1,Tab2,Tab3,Tab4,Tab5。每个选项卡都包含“搜索页面视图”。在这里请注意,我们正在创建一个Search Page视图模型的单个实例,并且同一实例已绑定到在不同选项卡中使用的每个SearchPageView新实例。
如果选择了Tab1,并且用户选择了Picker1。 SearchPageVm中提供了绑定到Picker1的SelectItem属性。我们有5个电话,我们无法确定哪个View正在为此调用。
我们只想执行一次操作。
示例代码:
SearchPage.xaml
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:System="clr-namespace:System;assembly=netstandard"
xmlns:SearchVm="clr-namespace:MyApplication.ViewModel;assembly=MyApplication"
x:Class="MyApplication.Views.SearchPage"
BindingContext="{x:Static SearchVm:CommonDataCache.SearchPageVmInstance}">
<ScrollView Orientation="Both" IsClippedToBounds="True" Padding="0,0,10,0">
<StackLayout x:Name="ControlStackLeft">
<customControls:SearchPageGrid
VisibleOnPages="Tab1,Tab2,Tab3,Tab4,Tab5">
<Label Grid.Row="0" Grid.Column="0" Text="Item1" />
<Picker Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="2"
HorizontalOptions="FillAndExpand"
ItemsSource="{Binding Item1List}"
ItemDisplayBinding="{Binding Name}"
SelectedItem="{Binding SelectItem1,Mode=TwoWay}" />
</customControls:SearchPageGrid>
</StackLayout>
</ScrollView>
SearchPageVm.cs:
private string _selectedItem1;
public string SelectedItem1
{
get => _selectedItem1;
set
{
_selectedItem1 = value;
OnPropertyChanged(nameof(SelectedItem1));
if (SelectedItem1 != null)
//Perform some Action
}
}
Tab1.xaml:
<StackLayout Orientation="Vertical" Margin="0,0,0,0" Grid.Row="0">
<ScrollView HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Orientation="Horizontal"
IsClippedToBounds="True">
<views:SearchPage x:Name="SearchView" />
</ScrollView>
</StackLayout>