当我单击列表视图切换按钮外部或列表视图切换按钮内部的事件时,将同时影响事件类中的事件. Xamarin形式 [英] When i click the event on outside of listview switch button or inside of listview switch button, it will affect bothin event class. xamarin forms
本文介绍了当我单击列表视图切换按钮外部或列表视图切换按钮内部的事件时,将同时影响事件类中的事件. Xamarin形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用xamarin表单listview. 我在列表视图和列表视图之外都使用了开关控件: 当我单击列表视图切换按钮外部或列表视图切换按钮内部的事件时,将同时影响事件类中的事件. 如何避免这些问题.
I am using xamarin forms listview. I am using switch control on both listview and outside of listview: When i click the event on outside of listview switch button or inside of listview switch button, it will affect bothin event class. How to avoid these issue.
我的xaml文件代码如下:
My code for xaml file is below:
<Label Margin="0,0,10,0" Text="Select All" TextColor="Black" FontSize="Medium" HorizontalOptions="End" VerticalOptions="Center"></Label>
<Switch Margin="0,0,10,0" x:Name="SelectToggled" HorizontalOptions="End" VerticalOptions="Center" Toggled="Switch_Toggled" ></Switch>
<ListView x:Name="LocationListView" HasUnevenRows="True" SeparatorVisibility="None" VerticalOptions="FillAndExpand">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid Margin="0,0,0,0" BackgroundColor="{Binding backgroundColor}" HeightRequest="47" RowSpacing="0" HorizontalOptions="FillAndExpand" Padding="20,0,0,0">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80*"/>
<ColumnDefinition Width="20*"/>
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" Text="{Binding CountryName}" HorizontalOptions="Start" VerticalOptions="Center" TextColor="Black" FontFamily="Roboto" FontSize="Medium"></Label>
<Switch Grid.Row="0" Grid.Column="1" HorizontalOptions="CenterAndExpand" Toggled="SwitchList_Toggled" VerticalOptions="CenterAndExpand" IsToggled="{Binding IsToggle,Mode=TwoWay}"></Switch>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
我对xaml.cs的代码是:
My code for xaml.cs is:
public MarketLanding()
{
InitializeComponent();
ObservableCollection<Country> countrylist = new ObservableCollection<Country>();
countrylist.Add(new Country { CountryName = "India" });
countrylist.Add(new Country { CountryName = "America" });
countrylist.Add(new Country { CountryName = "England" });
for (int i = 0; i < countrylist.Count; i++)
{
if (i % 2 == 0)
{
countrylist[i].backgroundColor = Color.LightGray;
}
else
{
countrylist[i].backgroundColor = Color.White;
}
}
LocationListView.ItemsSource = countrylist;
}
private void Switch_Toggled(object sender, ToggledEventArgs e)
{
}
private void SwitchList_Toggled(object sender, ToggledEventArgs e)
{
}
任何人都可以帮助我解决此问题.
Anyone please help me to sort out this issue.
推荐答案
您可以遍历CountryList,我做了一个示例,您可以看一下:
You can foreach through CountryList, I do one sample that you can take a look:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
x:Class="Test1.MainPage"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Test1">
<StackLayout>
<Label
Margin="0,0,10,0"
FontSize="Medium"
HorizontalOptions="End"
Text="Select All"
TextColor="White"
VerticalOptions="Center" />
<Switch
x:Name="SelectToggled"
Margin="0,0,10,0"
BackgroundColor="BlueViolet"
HorizontalOptions="End"
Toggled="Switch_Toggled"
VerticalOptions="Center" />
<ListView
x:Name="LocationListView"
BackgroundColor="AliceBlue"
HasUnevenRows="True"
SeparatorVisibility="None"
VerticalOptions="FillAndExpand">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid
Margin="0,0,0,0"
Padding="20,0,0,0"
BackgroundColor="{Binding backgroundColor}"
HeightRequest="47"
HorizontalOptions="FillAndExpand"
RowSpacing="0">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80*" />
<ColumnDefinition Width="20*" />
</Grid.ColumnDefinitions>
<Label
Grid.Row="0"
Grid.Column="0"
FontFamily="Roboto"
FontSize="Medium"
HorizontalOptions="Start"
Text="{Binding CountryName}"
TextColor="Black"
VerticalOptions="Center" />
<Switch
x:Name="switch1"
Grid.Row="0"
Grid.Column="1"
HorizontalOptions="CenterAndExpand"
IsToggled="{Binding IsToggle, Mode=TwoWay}"
Toggled="SwitchList_Toggled"
VerticalOptions="CenterAndExpand" />
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
public ObservableCollection<Country> countrylist { get; set; }
public MainPage()
{
InitializeComponent();
countrylist = new ObservableCollection<Country>();
countrylist.Add(new Country { CountryName = "India",IsToggle=false });
countrylist.Add(new Country { CountryName = "America" ,IsToggle=true});
countrylist.Add(new Country { CountryName = "England" ,IsToggle=false});
for (int i = 0; i < countrylist.Count; i++)
{
if (i % 2 == 0)
{
countrylist[i].backgroundColor = Color.LightGray;
}
else
{
countrylist[i].backgroundColor = Color.White;
}
}
LocationListView.ItemsSource = countrylist;
}
private void Switch_Toggled(object sender, ToggledEventArgs e)
{
Console.WriteLine(" outside switch clicked");
if(SelectToggled.IsToggled)
{
for (Int32 i = 0; i < countrylist.Count; i++)
{
countrylist[i].IsToggle = true;
}
}
else
{
for (Int32 i = 0; i < countrylist.Count; i++)
{
countrylist[i].IsToggle = false;
}
}
}
private void SwitchList_Toggled(object sender, ToggledEventArgs e)
{
Console.WriteLine(" inside switch clicked");
}
public class Country:INotifyPropertyChanged
{
private string _CountryName;
public string CountryName
{
get { return _CountryName; }
set
{
_CountryName = value;
RaisePropertyChanged("CountryName");
}
}
private Color _backgroundColor;
public Color backgroundColor
{
get { return _backgroundColor; }
set
{
_backgroundColor = value;
RaisePropertyChanged("backgroundColor");
}
}
private bool _IsToggle;
public bool IsToggle
{
get { return _IsToggle; }
set
{
_IsToggle = value;
RaisePropertyChanged("IsToggle");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if(handler!=null)
{
handler(this,new PropertyChangedEventArgs(propertyName));
}
}
}
这篇关于当我单击列表视图切换按钮外部或列表视图切换按钮内部的事件时,将同时影响事件类中的事件. Xamarin形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文