Stacklayout BindableLayout 中的 Xamarin Forms Switch 使用 MVVM (Prism.Forms) 获取切换事件
Xamarin Forms Switch inside Stacklayout BindableLayout get toggled event using MVVM (Prism.Forms)
我有以下列表:
<StackLayout BindableLayout.ItemsSource="{Binding NotificationList}" Margin="0,10,0,10" >
<BindableLayout.ItemTemplate>
<DataTemplate> <StackLayout Style="{StaticResource StacklayoutAStyle}">
<Label Text="{Binding notificationLabel}" VerticalOptions="Center" VerticalTextAlignment="Center"/>
<Switch IsToggled="{Binding isNotificationToggled, Mode=TwoWay}" VerticalOptions="Center" HorizontalOptions="End" />
</StackLayout>
</DataTemplate>
</BindableLayout.ItemTemplate>
</StackLayout>
我想在切换列表中的相应开关时获取 notificationLabel 文本。我的 ViewModel 看起来像这样:
private ObservableCollection<NotificationModel> _notificationList = new ObservableCollection<NotificationModel>
{
new NotificationModel { notificationLabel = "Notification1", isNotificationToggled = true },
new NotificationModel { notificationLabel = "Notification2", isNotificationToggled = false },
new NotificationModel { notificationLabel = "Notification3", isNotificationToggled = false },
new NotificationModel { notificationLabel = "Notification4", isNotificationToggled = true },
};
public ObservableCollection<NotificationModel> NotificationList
{
get { return _notificationList; }
set { SetProperty(ref _notificationList, value); }
}
例如,当我切换通知 3 时,它会切换到打开状态 (true),但是如何使用“通知 3”捕获该事件?
可以使用Switch.Toggled
监听toggled事件,然后获取BindingContext(NotificationModel
),然后根据NotificationModel.notificationLabel
判断打开哪个Switch。
<StackLayout BindableLayout.ItemsSource="{Binding NotificationList}" Margin="0,10,0,10" >
<BindableLayout.ItemTemplate>
<DataTemplate>
<StackLayout Style="{StaticResource StacklayoutAStyle}">
<Label Text="{Binding notificationLabel}" VerticalOptions="Center" VerticalTextAlignment="Center"/>
<Switch IsToggled="{Binding isNotificationToggled, Mode=TwoWay}" VerticalOptions="Center" HorizontalOptions="End" Toggled="Switch_Toggled" />
</StackLayout>
</DataTemplate>
</BindableLayout.ItemTemplate>
</StackLayout>
在你的 page.xaml.cs:
private void Switch_Toggled(object sender, ToggledEventArgs e)
{
Switch sw = sender as Switch;
NotificationModel notificationModel = (NotificationModel)sw.BindingContext;
if (e.Value)
{
switch (notificationModel.notificationLabel)
{
case "Notification1":
//do something
break;
case "Notification2":
//do something
break;
case "Notification3":
//do something
break;
case "Notification4":
//do something
break;
}
}
}
我有以下列表:
<StackLayout BindableLayout.ItemsSource="{Binding NotificationList}" Margin="0,10,0,10" >
<BindableLayout.ItemTemplate>
<DataTemplate> <StackLayout Style="{StaticResource StacklayoutAStyle}">
<Label Text="{Binding notificationLabel}" VerticalOptions="Center" VerticalTextAlignment="Center"/>
<Switch IsToggled="{Binding isNotificationToggled, Mode=TwoWay}" VerticalOptions="Center" HorizontalOptions="End" />
</StackLayout>
</DataTemplate>
</BindableLayout.ItemTemplate>
</StackLayout>
我想在切换列表中的相应开关时获取 notificationLabel 文本。我的 ViewModel 看起来像这样:
private ObservableCollection<NotificationModel> _notificationList = new ObservableCollection<NotificationModel>
{
new NotificationModel { notificationLabel = "Notification1", isNotificationToggled = true },
new NotificationModel { notificationLabel = "Notification2", isNotificationToggled = false },
new NotificationModel { notificationLabel = "Notification3", isNotificationToggled = false },
new NotificationModel { notificationLabel = "Notification4", isNotificationToggled = true },
};
public ObservableCollection<NotificationModel> NotificationList
{
get { return _notificationList; }
set { SetProperty(ref _notificationList, value); }
}
例如,当我切换通知 3 时,它会切换到打开状态 (true),但是如何使用“通知 3”捕获该事件?
可以使用Switch.Toggled
监听toggled事件,然后获取BindingContext(NotificationModel
),然后根据NotificationModel.notificationLabel
判断打开哪个Switch。
<StackLayout BindableLayout.ItemsSource="{Binding NotificationList}" Margin="0,10,0,10" >
<BindableLayout.ItemTemplate>
<DataTemplate>
<StackLayout Style="{StaticResource StacklayoutAStyle}">
<Label Text="{Binding notificationLabel}" VerticalOptions="Center" VerticalTextAlignment="Center"/>
<Switch IsToggled="{Binding isNotificationToggled, Mode=TwoWay}" VerticalOptions="Center" HorizontalOptions="End" Toggled="Switch_Toggled" />
</StackLayout>
</DataTemplate>
</BindableLayout.ItemTemplate>
</StackLayout>
在你的 page.xaml.cs:
private void Switch_Toggled(object sender, ToggledEventArgs e)
{
Switch sw = sender as Switch;
NotificationModel notificationModel = (NotificationModel)sw.BindingContext;
if (e.Value)
{
switch (notificationModel.notificationLabel)
{
case "Notification1":
//do something
break;
case "Notification2":
//do something
break;
case "Notification3":
//do something
break;
case "Notification4":
//do something
break;
}
}
}