So I am trying to navigate to a content page that is supposed to have all the movie details from the movie the user selects in the collection view but every time I select a movie in my collection view the error "Specified cast is not valid", which occurs at the Navigation.PushAsync(new MovieDetails((MovieData)e.CurrentSelection)); line, pops up.
I'm very new to C# and using Xamarin forms so I'm not sure if this is the right way to be doing things.
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using Listly.Models;
using Xamarin.Forms;
namespace Listly
{
public partial class SearchPage : ContentPage
{
public ObservableCollection<MovieData> allMovies = new ObservableCollection<MovieData>();
public int NumClicked { get; set; }
public bool BtnClicked = false;
public string Poster_Path_End { get; set; }
public MovieList newList = new MovieList();
public SearchPage()
{
InitializeComponent();
contentPage.BackgroundColor = Color.FromHex("1D1D1D");
searchBtn.BackgroundColor = Color.FromHex("28D7B5");
searchBtn.TextColor = Color.FromHex("1D1D1D");
searchBar.PlaceholderColor = Color.FromHex("A0A0A0");
searchBar.TextColor = Color.Black;
collectionView.SelectionMode = SelectionMode.Single;
collectionView.ItemsSource = allMovies;
collectionView.SelectionChanged += CollectionView_SelectionChanged;
searchBtn.Clicked += SearchBtn_Clicked;
}
private void CollectionView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.CurrentSelection != null)
{
Navigation.PushAsync(new MovieDetails((MovieData)e.CurrentSelection));
}
}
private async void SearchBtn_Clicked(object sender, EventArgs e)
{
if (BtnClicked == false)
{
BtnClicked = true;
NumClicked += 1;
DataManager dataManager = new DataManager(searchBar.Text);
dataManager.GetMovie(searchBar.Text);
newList = await dataManager.GetMovie(searchBar.Text);
foreach (var movie in newList.searchList)
{
if (movie.Title == null)
{
bool answer = await DisplayAlert("Movie Not Found", "The movie searched was not found. Please try again.", "Okay", "Cancel");
searchBar.Text = "";
searchBar.Placeholder = "Enter Movie Name";
break;
}
allMovies.Add(movie);
}
}
else if (BtnClicked == true)
{
if (NumClicked == 1 || NumClicked > 1)
{
allMovies.Clear();
DataManager dataManager = new DataManager(searchBar.Text);
newList = await dataManager.GetMovie(searchBar.Text);
foreach (var movie in newList.searchList)
{
if (movie.Title == null)
{
bool answer = await DisplayAlert("Movie Not Found", "The movie searched was not found. Please try again.", "Okay", "Cancel");
break;
}
allMovies.Add(movie);
}
}
}
collectionView.ItemsSource = allMovies;
}
protected override void OnAppearing()
{
base.OnAppearing();
collectionView.ItemsSource = allMovies;
}
}
}
Here's the MovieDetails page
using System;
using System.Collections.Generic;
using Listly.Models;
using Xamarin.Forms;
namespace Listly
{
public partial class MovieDetails : ContentPage
{
public MovieData details = new MovieData();
public MovieData selectedMovie = new MovieData();
public string Poster_Path_End { get; set; }
public MovieDetails(MovieData selectedMovie)
{
InitializeComponent();
details = selectedMovie;
details.Title = selectedMovie.Title;
details.Overview = selectedMovie.Overview;
}
}
}
As Jason said,
CurrentSelection – the list of items that are selected, after the
selection change.
So you could set SelectionMode to Single,then use SelectedItem.
or do like this:
MovieData movie = e.CurrentSelection.FirstOrDefault() as MovieData ;
Navigation.PushAsync(new MovieDetails(movie);
the more you could look at CollectionView Selection.
Related
Please help me with this issue.
I was making a custom renderer in xamarin using this article:
Xamarin Form Custom Renderer
Everything worked fine until I made the pin information into a JSON file.
So basically now the LAT, LNG, label, name and URL were stored in a JSON file instead of a C# file.
For testing, I had made 3 temporary pins. But only 1 of them shows the custom rendered window. When I click the other 2 pins an exception occurs as custom pin not found. But the custom pin is found for the last pin but not the 1st and 2nd pin which confuses me.
My code:
JSON file:
[
{"Label" :"Country1",
"Address":"A multine paragraph",
"Lat":"-12",
"Lng":"14",
"Name":"Xamarin",
"Url": "http://xamarin.com/about/"
},
{ "Label" :"Country2",
"Address":"Multiline paragraph",
"Lat":"-25",
"Lng":"45",
"Name":"Xamarin",
"Url": "http://xamarin.com/about/"
},
{ "Label" :"Country3",
"Address":"Multiline paragraph",
"Lat":"-5",
"Lng":"45",
"Name":"Xamarin",
"Url": "http://xamarin.com/about/"
}
]
MapPage.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Maps;
using Newtonsoft.Json;
using System.IO;
using Xamarin.Forms.Xaml;
using System.Reflection;
namespace Orbage
{
public partial class MapPage : ContentPage
{
public MapPage()
{
CustomMap customMap = new CustomMap
{
MapType = MapType.Hybrid
};
Content = customMap;
var assembly = IntrospectionExtensions.GetTypeInfo(typeof(MapPage)).Assembly;
Stream stream = assembly.GetManifestResourceStream("Orbage.Mydata.json");
string json = "";
using (var reader = new System.IO.StreamReader(stream))
{
json = reader.ReadToEnd();
}
var places = JsonConvert.DeserializeObject<List<Mydata>>(json);
List<CustomPin> custompinList = new List<CustomPin>();
foreach (var place in places)
{
CustomPin pin = new CustomPin
{
Type = PinType.Place,
Position = new Position(Double.Parse(place.Lat), Double.Parse(place.Lng)),
Label = place.Label,
Address = place.Address,
Name = place.Name,
Url = place.Url
};
customMap.Pins.Add(pin);
customMap.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(37.79752, -122.40183), Distance.FromMiles(1.0)));
customMap.CustomPins = custompinList;
}
}
}
public class Mydata
{
public string Label { get; set; }
public string Address { get; set; }
public string Lat { get; set; }
public string Lng { get; set; }
public string Name { get; set; }
public string Url { get; set; }
}
}
CustomPin:
using System;
using System.Collections.Generic;
using System.Text;
using Xamarin.Forms.Maps;
namespace Orbage
{
public class CustomPin : Pin
{
public string Name { get; set; }
public string Url { get; set; }
public string Adress { get; set; }
public string Lat { get; set; }
public string Lng { get; set; }
}
}
CustomMap:
using System;
using System.Collections.Generic;
using System.Text;
using Xamarin.Forms.Maps;
namespace Orbage
{
public class CustomMap : Map
{
public List<CustomPin> CustomPins { get; set; }
}
}
CustomMapRenderer.cs:
using System;
using System.Collections.Generic;
using Android.Content;
using Android.Gms.Maps;
using Android.Gms.Maps.Model;
using Android.Widget;
using ----;
using ----.Droid;
using Xamarin.Forms;
using Xamarin.Forms.Maps;
using Xamarin.Forms.Maps.Android;
[assembly: ExportRenderer(typeof(CustomMap), typeof(CustomMapRenderer))]
namespace ----.Droid
{
public class CustomMapRenderer : MapRenderer, GoogleMap.IInfoWindowAdapter
{
List<CustomPin> customPins;
public CustomMapRenderer(Context context) : base(context)
{
}
protected override void OnElementChanged(Xamarin.Forms.Platform.Android.ElementChangedEventArgs<Map> e)
{
base.OnElementChanged(e);
if (e.OldElement != null)
{
NativeMap.InfoWindowClick -= OnInfoWindowClick;
}
if (e.NewElement != null)
{
var formsMap = (CustomMap)e.NewElement;
customPins = formsMap.CustomPins;
}
}
protected override void OnMapReady(GoogleMap map)
{
base.OnMapReady(map);
NativeMap.InfoWindowClick += OnInfoWindowClick;
NativeMap.SetInfoWindowAdapter(this);
}
protected override MarkerOptions CreateMarker(Pin pin)
{
var marker = new MarkerOptions();
marker.SetPosition(new LatLng(pin.Position.Latitude, pin.Position.Longitude));
marker.SetTitle(pin.Label);
marker.SetSnippet(pin.Address);
marker.SetIcon(BitmapDescriptorFactory.FromResource(Resource.Drawable.pin));
return marker;
}
void OnInfoWindowClick(object sender, GoogleMap.InfoWindowClickEventArgs e)
{
var customPin = GetCustomPin(e.Marker);
if (customPin == null)
{
throw new Exception("Custom pin not found");
}
if (!string.IsNullOrWhiteSpace(customPin.Url))
{
var url = Android.Net.Uri.Parse(customPin.Url);
var intent = new Intent(Intent.ActionView, url);
intent.AddFlags(ActivityFlags.NewTask);
Android.App.Application.Context.StartActivity(intent);
}
}
public Android.Views.View GetInfoContents(Marker marker)
{
var inflater = Android.App.Application.Context.GetSystemService(Context.LayoutInflaterService) as Android.Views.LayoutInflater;
if (inflater != null)
{
Android.Views.View view;
var customPin = GetCustomPin(marker);
if (customPin == null)
{
throw new Exception("Custom pin not found");
}
if (customPin.Name.Equals("Xamarin"))
{
view = inflater.Inflate(Resource.Layout.XamarinMapInfoWindow, null);
}
else
{
view = inflater.Inflate(Resource.Layout.MapInfoWindow, null);
}
var infoTitle = view.FindViewById<TextView>(Resource.Id.InfoWindowTitle);
var infoSubtitle = view.FindViewById<TextView>(Resource.Id.InfoWindowSubtitle);
if (infoTitle != null)
{
infoTitle.Text = marker.Title;
}
if (infoSubtitle != null)
{
infoSubtitle.Text = marker.Snippet;
}
return view;
}
return null;
}
public Android.Views.View GetInfoWindow(Marker marker)
{
return null;
}
CustomPin GetCustomPin(Marker annotation)
{
var position = new Position(annotation.Position.Latitude, annotation.Position.Longitude);
foreach (var pin in customPins)
{
if (pin.Position == position)
{
return pin;
}
}
return null;
}
}
}
Thanks for seeing this and I hope this get's answered and someone with the same error gets it fixed.
:)
I tried debugging and here is a image of what somewhat might help:
foreach (var place in places)
{
CustomPin pin = new CustomPin
{
Type = PinType.Place,
Position = new Position(Double.Parse(place.Lat), Double.Parse(place.Lng)),
Label = place.Label,
Address = place.Address,
Name = place.Name,
Url = place.Url
};
customMap.CustomPins = new List<CustomPin> { pin }; //this will cause your customMap.CustomPins always has only the last custompin.
customMap.Pins.Add(pin);
customMap.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(37.79752, -122.40183), Distance.FromMiles(1.0)));
}
The issue is here,you add the customMap.CustomPins inside the loop,so it always has only the third CustomPin.
Try to move it out of the foreach:
List<CustomPin> custompinList = new List<CustomPin>();
foreach (var place in places)
{
CustomPin pin = new CustomPin
{
Type = PinType.Place,
Position = new Position(Double.Parse(place.Lat), Double.Parse(place.Lng)),
Label = place.Label,
Address = place.Address,
Name = place.Name,
Url = place.Url
};
custompinList.Add(pin);
customMap.Pins.Add(pin);
customMap.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(37.79752, -122.40183), Distance.FromMiles(1.0)));
}
customMap.CustomPins = custompinList;
Update the effect like below:
Hello so I am trying to code a simple web view, with asp.net and NEST library, that will take my ElasticSearch database, and show it in textview on button click.
This is the code that I input when my button is clicked,
would you please look at it and tell me am I on a good path or something is not good.
using Elasticsearch.Net;
using Nest;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ElasticsearchWeb
{
public class shekspir
{
public string type { get; set; }
public int line_id { get; set; }
public string play_name { get; set; }
public int speech_number { get; set; }
public float line_number { get; set; }
public string speaker { get; set; }
public string text_entry { get; set; }
}
public partial class Default : System.Web.UI.Page
{
public static Uri GetElasticHost()
{
var host = "http://localhost:9200";
return new Uri(host);
}
public static ElasticClient GetElasticClient(ConnectionSettings settings = null)
{
if (settings == null)
{
var node = GetElasticHost();
var pool = new SingleNodeConnectionPool(node);
settings = new ConnectionSettings(pool);
}
settings.DisableDirectStreaming(true);
var client = new ElasticClient(settings);
return client;
}
public static List<shekspir> GetAllShekspir(int ID)
{
var workОfShakespeare = GetElasticClient();
ISearchResponse<shekspir> result = null;
result = workОfShakespeare.Search<shekspir>(x => x
.Index("shekspir")
.Query(q => q
.MatchAll())
.Size(100)
);
List<shekspir> list = new List<shekspir>();
foreach (var r in result.Hits)
{
shekspir a = r.Source;
list.Add(a);
}
return list;
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
List<shekspir> list = GetAllShekspir (1);
foreach (shekspir u in list)
{
litInfo.Text += u.play_name + ": " + u.text_entry + "<br>";
}
}
}
}
List<shekspir> list = new List<shekspir>();
foreach (var r in result.Hits)
{
shekspir a = r.Source;
list.Add(a);
}
If you just want the returned documents above can be replaced by
var list= result.Documents
I'm getting this error on DataBind(), and I don't know why since there shouldn't be anything selected.
DdState.Items.Clear();
DdState.DataSource = UsStates;
DdState.DataTextField = "Title";
DdState.DataValueField = "Title";
DdState.Items.Insert(0, String.Empty);
if (DdState.SelectedItem != null)
{
DdState.SelectedItem.Selected = false;
}
DdState.DataBind();
private IEnumerable<IStateItem> UsStates
{
get
{
var statesFolder = _sitecoreService.GetItem<ISitecoreItem>(ItemReference.BcsUs_ProductData_States.Guid);
if (statesFolder == null)
return new List<IStateItem>();
List<IStateItem> usStates = _sitecoreService.QueryChildren<IStateItem>(statesFolder).OrderBy(s => s.Title).ToList();
return usStates;
}
}
I tried putting in DdState.SelectedIndex = 0 before the DataBind(), but then I got an error that the selected index did not exist. What's going on?
If the DataSource is a list its much easier to implement. So just "convert" the UsStates IEnumerable to a List an then add it to the data source.
DdState.DataSource = UsStates.ToList();
Then choose the property of a list item as binding.
OR
public Form1()
{
InitializeComponent();
DdState.Items.Clear();
DdState.DataSource = UsStates;
DdState.DisplayMember = "Statename";
DdState.SelectedIndex = 0;
}
private List<IStateItem> UsStates
{
get
{
List<IStateItem> usStates = new List<IStateItem>();
usStates.Add(new IStateItem("California","status1"));
usStates.Add(new IStateItem("Ohio", "status3"));
return usStates;
}
}
private class IStateItem
{
public IStateItem(string statename, string stateStatus)
{
Statename = statename;
StateStatus = stateStatus;
}
public string Statename { get; set; }
public string StateStatus { get; set; }
}
Could there be something wrong with your IStateItem class?
I copy/pasted your code in a new asp.net application, made my own IStateItem class and it works.
using System;
using System.Collections.Generic;
namespace TestIt
{
public partial class Form1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
FillTheList();
}
private void FillTheList()
{
ddl_TheList.Items.Clear();
ddl_TheList.DataSource = UsStates;
ddl_TheList.DataTextField = "statename";
ddl_TheList.DataValueField = "stateStatus";
//ddl_TheList.Items.Insert(0, String.Empty);
ddl_TheList.DataBind();
ddl_TheList.SelectedIndex = 0;
}
private IEnumerable<IStateItem> UsStates
{
get
{
List<IStateItem> usStates = new List<IStateItem>();
for (int i = 0; i < 10; i++)
{
usStates.Add(new IStateItem { statename = "state #" + i, stateStatus = "cool state bro" });
}
return usStates;
}
}
}
public class IStateItem
{
public string statename { get; set; }
public string stateStatus { get; set; }
}
}
I am using SQLite for a data entry windows 8 app I am working on. I can create the db, insert data, retrieve a column count, and read data, but cannot get the column names.
The underlying framework is from this post.
I read about the PRAGMA table_info(table_name); command but I cannot seem to send and read back this query properly. I have been googling for 3 days!
MainPage.xaml.cs:
using SQLite;
using SqlLiteTest.Model;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Windows.Storage;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace SqlLiteTest
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
txtPath.Text = ApplicationData.Current.LocalFolder.Path;
}
private async void createDB(object sender, RoutedEventArgs e)
{
// access local folder
var qvLocalFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
try
{
//Create a blank carrier file
StorageFile qvLocalFileCarrier = await qvLocalFolder.CreateFileAsync("qvdbLocal.db", CreationCollisionOption.FailIfExists);
//Write the blank carrier file
await FileIO.WriteTextAsync(qvLocalFileCarrier, "");
}
catch { }
// connect
var path = Windows.Storage.ApplicationData.Current.LocalFolder.Path + #"\qvdbLocal.db";
var db = new SQLiteAsyncConnection(path);
// create table
await db.CreateTableAsync<qvdb>();
// insert data
var insertRecords = new List<qvdb>()
{
new qvdb
{
qvdbRecord = 1,
qvdbNotes = "Notes1",
qvdb001 = "Variable 1.1",
qvdb002 = "Variable 2.1"
},
new qvdb
{
qvdbRecord = 1,
qvdbNotes = "Notes1",
qvdb001 = "Variable 1.1",
qvdb002 = "Variable 2.1"
},
new qvdb
{
qvdbRecord = 1,
qvdbNotes = "Notes1",
qvdb001 = "Variable 1.1",
qvdb002 = "Variable 2.1"
},
};
await db.InsertAllAsync(insertRecords);
// read count
var allUsers = await db.QueryAsync<qvdb>("SELECT * FROM qvdb");
var count = allUsers.Any() ? allUsers.Count : 0;
Debug.WriteLine(count);
}
private async void updateDB(object sender, RoutedEventArgs e)
{
var path = Windows.Storage.ApplicationData.Current.LocalFolder.Path + #"\qvdbLocal.db";
var db = new SQLiteAsyncConnection(path);
var tempCell = db.QueryAsync<qvdb>("UPDATE qvdb SET qvdbNotes ='!##$%$%^^&*()+)(*&^%$##!{:L<>?' WHERE qvdbRecord = 10");
await db.UpdateAsync(tempCell);
}
private async void readDB(object sender, RoutedEventArgs e)
{
var path = Windows.Storage.ApplicationData.Current.LocalFolder.Path + #"\qvdbLocal.db";
var db = new SQLiteAsyncConnection(path);
var query = db.Table<qvdb>();
var result = await query.ToListAsync();
foreach (var item in result)
{
MessageDialog dialog = new MessageDialog(string.Format("{0} {1} {2}", item.qvdbRecord, item.qvdbNotes, item.qvdb001));
await dialog.ShowAsync();
}
}
private void readColNames(object sender, RoutedEventArgs e)
{
}
}
}
qvdb.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SQLite;
namespace SqlLiteTest.Model
{
public class qvdb
{
[PrimaryKey, AutoIncrement]
public int qvdbRecord { get; set; }
[MaxLength(3000)]
public string qvdbNotes { get; set; }
[MaxLength(1000)]
public string qvdb001 { get; set; }
[MaxLength(1000)]
public string qvdb002 { get; set; }
}
}
Thanks CL for the info. I added the class but still do not know how to access them. Some more code...
// this works
// read record count
var allRecords = await db.QueryAsync<qvdb>("SELECT * FROM qvdb");
var countRecords = allRecords.Any() ? allRecords.Count : 0;
this.textboxLog.Text = this.textboxLog.Text + Environment.NewLine + "There are " + countRecords + " records.";
// ??
// read column names
var allColumns = await db.QueryAsync<qvdb>("PRAGMA table_info(qvdb)");
foreach (var item in allColumns) {
//read name
this.textboxLog.Text = this.textboxLog.Text + Environment.NewLine + "columbn names";
}
The records returned by PRAGMA table_info look like this:
public class table_info_record
{
public int cid { get; set; }
public string name { get; set; }
public string type { get; set; }
public int notnull { get; set; }
public string dflt_value { get; set; }
public int pk { get; set; }
}
Use it like this:
db.QueryAsync<table_info_record>("PRAGMA table_info(...)");
o end the loop on CL's advice, this code successfully reads the column names:
// read column names
var query = await db.QueryAsync<table_info_record>("PRAGMA table_info(MY_TABLE_NAME_HERE)");
foreach (var item in query)
{
Debug.WriteLine(string.Format("{0}", item.name) + " is a column.");
}
I'm working on a bigger project atm, but I made this simple example to show you what happens..
using System.Collections.Generic;
using System.Windows;
namespace txt
{
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
var obsLst = new List<Info> { new Info { name = "asd" }, new Info { name = "asd" }, new Info { name = "asd" }, new Info { name = "asd" } };
var temp = new List<Info>();
for (var i = 1; i <= 3; i++)
{
temp.Add(obsLst[0]); //I add 3 of the same item from obsLst to temp
}
lst.DataContext = temp; //lst = ListBox
}
}
public class Info
{
public string name { get; set; }
}
}
The ListBox ItemsSource is set to {Binding}..
When I start the application I get 3 txt.Info objects displayed and if I click any of them, 2 or even all of them get selected aswell. From my understanding the problem relies in the fact that the listbox selector cannot differentiate between the items and therefor doesn't know which one I selected.
Here's a picture of what it looks like..
I only clicked on the second txt.Info item.
I found a solution where someone said that I have to specify the DisplayMemberPath, but I can't really do that in the other project because I have a datatemplate for the object.
Any ideas on how I could fix this would be great..
Thx in advance.
EDIT 1:
this works but it's not nice..
using System.Collections.Generic;
using System.Windows;
namespace txt
{
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
var obsLst = new List<Info> { new Info { name = "asd" }, new Info { name = "asd" }, new Info { name = "asd" }, new Info { name = "asd" } };
var temp = new List<Container>();
for (var i = 1; i <= 3; i++)
{
var t = new Container();
t.obj = obsLst[0];
temp.Add(t);
}
lst.DataContext = temp;
}
}
public class Info
{
public string name { get; set; }
}
public class Container
{
public Info obj { get; set; }
}
}
In this case you need to set DisplayMemberPath="obj"
Assigning an ID to the object, doesn't work..
using System;
using System.Collections.Generic;
using System.Windows;
namespace txt
{
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
var rand = new Random();
var obsLst = new List<Info> { new Info { name = "asd" }, new Info { name = "asd" }, new Info { name = "asd" }, new Info { name = "asd" } };
var temp = new List<Info>();
for (var i = 1; i <= 3; i++)
{
obsLst[0].id = rand.Next(10000);
temp.Add(obsLst[0]);
}
lst.DataContext = temp;
}
}
public class Info
{
public string name { get; set; }
public int id { get; set; }
}
}
I had this problem too a while ago, i fixed it by adding the id # to the item so they're always different.