Xamarin ListView内部的开关/标签';s ViewCell的名称不可访问
我使用开关切换数据,如果我添加一个事件,它就可以正常工作。我正在尝试删除数据绑定上的处理程序,以便事件处理程序保持不变。但我并不是根据元素的名称来获取它Xamarin ListView内部的开关/标签';s ViewCell的名称不可访问,xamarin,xamarin.forms,Xamarin,Xamarin.forms,我使用开关切换数据,如果我添加一个事件,它就可以正常工作。我正在尝试删除数据绑定上的处理程序,以便事件处理程序保持不变。但我并不是根据元素的名称来获取它 <ListView Grid.Column="2" x:Name="DynamicListView" IsPullToRefreshEnabled="False" ItemSelected="DynamicListViewItemSelected" IsVisible="false" WidthRequest="380" RowHeigh
<ListView Grid.Column="2" x:Name="DynamicListView" IsPullToRefreshEnabled="False" ItemSelected="DynamicListViewItemSelected" IsVisible="false" WidthRequest="380" RowHeight="75">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation ="Horizontal" >
<Label x:Name="configLabel" Text="{Binding Name}" HorizontalOptions="StartAndExpand" Style="{StaticResource BigBlackLabelLeft}" />
<Switch x:Name="configSwitch" HorizontalOptions="End" OnColor="LightSeaGreen" IsToggled="{Binding IsVisible}" Toggled="HandleSwitchToggledByUser" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
因此,在代码背后,我试图通过configLabel
/configSwitch
访问Label/Switch,但说出名称configSwitch时出错
在当前上下文中不存在
。我不确定这里出了什么问题。如果要在切换事件中获取ViewCell的BindingContext,很简单:只需将sender参数强制转换为视图,将其BindingContext强制转换为列表中显示的项类,然后从中读取所需的值:
private void HandleSwitchToggledByUser(对象发送方,ToggledEventArgs e)
{
if(发送方为视图v&&v.BindingContext为ItemClass项)
{
var switchName=item.Name;
}
}
如果ViewCell
中只有两个子视图,我将通过以下方式访问控件:
首先,我创建了一个自定义mySwitch,并向其中添加了一个可绑定属性名称
,以后您可以使用此名称来确定要切换的开关
:
public class mySwitch : Switch
{
public static readonly BindableProperty nameProperty =
BindableProperty.Create("name", typeof(string), typeof(MainPage), null);
public string name
{
get { return (string)GetValue(nameProperty); }
set { SetValue(nameProperty, value); }
}
}
在xaml中,我编写了一个listView作为示例,并将绑定设置为与标签文本相同的交换机名称:
<ContentPage.Content>
<ListView>
<ListView.ItemsSource>
<x:Array Type="{x:Type x:String}">
<x:String>wifi</x:String>
<x:String>sound</x:String>
</x:Array>
</ListView.ItemsSource>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout BackgroundColor="#eee" Orientation="Vertical">
<StackLayout Orientation="Horizontal">
<Label Text="{Binding .}" TextColor="#f35e20" />
<local:mySwitch name="{Binding .}" HorizontalOptions="End" OnColor="LightSeaGreen" IsToggled="{Binding IsVisible}" Toggled="Switch_Toggled" />
</StackLayout>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage.Content>
这是一张gif:
我还上传了我的演示,你可以查看。让我知道它是否适用于您。您无法按名称从代码隐藏中访问模板中的项目,因为在运行时可能创建了该模板的0或1000个副本。如果你能准确地解释你想做什么,我们可以建议一种替代方法。我尝试使用交换机作为用户设置,例如打开/关闭wifi。这里我使用
标签
作为文本的占位符,并使用开关
进行切换。我的想法是捕获事件上的绑定对象,以获得用户想要修改的确切值。请尝试CommandParameter=“{binding.}”
,然后您可以从事件句柄中的发送方
获取该值。我认为开关中没有可用的命令,我在看这个https://stackoverflow.com/questions/41070613/xamarin-forms-switch-toggled-event-doesnt-bind-with-viewmodel
postase只能使用发送方的BindingContext
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
private void Switch_Toggled(object sender, ToggledEventArgs e)
{
// access switch
var Switch_ToggledHandler = (mySwitch)sender;
// access Parent Layout for Sender
StackLayout ParentStackLayout = (StackLayout)Switch_ToggledHandler.Parent;
// access the Label "configLabel"
Label configLabel = (Label)ParentStackLayout.Children[0];
if (Switch_ToggledHandler.IsToggled)
{
switch (Switch_ToggledHandler.name)
{
case "wifi":
configLabel.Text = "wifi open";
break;
case "sound":
configLabel.Text = "sound is open";
break;
default:
Console.WriteLine("Default case");
break;
}
}
else {
switch (Switch_ToggledHandler.name)
{
case "wifi":
configLabel.Text = "wifi off";
break;
case "sound":
configLabel.Text = "sound is off";
break;
default:
Console.WriteLine("Default case");
break;
}
}
}
}