I have a SwitchCell implemented within a ListView; I want to be able to access the properties of the SwitchCell: On and text. I want to be able to get and set the OnProperty of the SwitchCell to change/read the Switch state from within the xaml.cs class.
when I run the code, I get the Unhandled Exception error. i am very new to both Xamarin and C# so any help/advise/examples of solving the problem will be greatly appreciated.
The exception is happening at var selectedItem = ((SwitchCell)sender).BindingContext as Relays; in the SwitchCell.xaml.cs.
My Relay.cs class is as follows:
using System;
using System.Collections.Generic;
using System.Text;
namespace Socket.Models
{
public class Relays
{
public Boolean isOn { get; set; } // Set the state of the
switch
public string State { get; set; } // Get the state of the
switch based on the isOn property
public string Name { get; set; } // Set the name of the
relay in the list
}
}
My SwitchCell.xaml is as follows:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Socket.SwitchCell"
Title="Relay Control Page">
<ContentPage.Content>
<StackLayout Padding="10,0,0,0">
<ListView x:Name="lstView" SelectionMode="None">
<ListView.ItemTemplate>
<DataTemplate>
<SwitchCell x:Name="Sw1" Text="{Binding Name}" On="
{Binding isOn, Mode=TwoWay}"
OnChanged="SwitchCell_OnChanged_2"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage.Content>
</ContentPage>
My SwitchCell.xaml.cs is as follows:
using Socket.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace Socket
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class SwitchCell : ContentPage
{
public SwitchCell ()
{
InitializeComponent ();
loadSampleData();
}
private void loadSampleData()
{
// Create sample data
ObservableCollection<Relays> listRelays = new
ObservableCollection<Relays>();
listRelays.Add(new Relays { Name ="Relay 1", State = "",
isOn=false });
listRelays.Add(new Relays { Name ="Relay 2", State = "",
isOn=false });
listRelays.Add(new Relays { Name ="Relay 3", State = "",
isOn=false });
lstView.ItemsSource = listRelays;
}
private void SwitchCell_OnChanged_2(object sender, ToggledEventArgs
e)
{
var selectedItem = ((SwitchCell)sender).BindingContext as
Relays;
if (true)
{
bool IsToggled = e.Value;
string name = IsToggled.ToString();
if (name == "True")
{
//DisplayAlert("ON", "Relay 1 On", "Cancel");
BackgroundColor = Color.Silver;
if (selectedItem.isOn == false)
{
BackgroundColor = Color.Gold;
selectedItem.Name = "Changed";
}
}
else
{
//DisplayAlert("OFF", "Relay 1 OFF", "Cancel");
BackgroundColor = Color.LightSkyBlue;
}
}
}
}
}
This is the error I get in VS 2017: Unhandled Exception: System.InvalidCastException: Specified cast is not valid. occurred.
I am not sure if this is useful but this is what I get from the Call Stack:
0x1 in Socket.SwitchCell.SwitchCell_OnChanged_2 at C:\Users\ryno\Desktop\Xamarin\Socket\Socket\Socket\SwitchCell.xaml.cs:42,13.
I have no idea what I am doing wrong. Any help will be appreciated. Thanks.
SwitchCell
... (there is probably a name collision with the XAML ListView SwitchCell control) – Benl