ListPicker windows phone 7 giving error - windows-phone-7

I have list picker in my windows phone 7 app & it works fine but whenever i try to add more options in it... it crashes i can add upto 4 or 5 options only in this list picker is there anything i can do aboout it
My working code is
xaml code
<toolkit:ListPicker x:Name="ListPicker" Margin="12,3,12,12" Foreground="#FF00C000" >
<toolkit:ListPicker.Items>
<toolkit:ListPickerItem Content="Item1"/>
<toolkit:ListPickerItem Content="Item2"/>
</toolkit:ListPicker.Items>
</toolkit:ListPicker>
& .cs code
string ListPickerOperator = (this.ListPicker.SelectedItem as ListPickerItem).Content as string;
switch (ListPickerOperator )
{
case "Item1":
break;
case "Item2":
break;
}
but whenever i try to make this list bigger it crashes after 4 5 items

I think you should use item template. You couldn't show more than 4 or 5 items because you have to use "Full Mode template".
to use item template you should do something like this.
<toolkit:ListPicker x:Name="ListPicker" Margin="12,3,12,12" Foreground="#FF00C000" SelectionChanged="ListPicker_SelectionChanged" >
<!--Normal Item template-->
<toolkit:ListPicker.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}"/>
</DataTemplate>
</toolkit:ListPicker.ItemTemplate>
<!--Full Mode template-->
<toolkit:ListPicker.FullModeItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}"/>
</DataTemplate>
</toolkit:ListPicker.FullModeItemTemplate>
</toolkit:ListPicker>
To populate the listpicker you only have to use itemSource propetary
//list string
List<String> itemsList = new List<String>();
// create 100 items
for (int j = 0; j < 100; j++)
{
itemsList.Add("item" + j);
}
//itemsource
this.ListPicker.ItemsSource = itemsList;
to get the selectedItem you could do something like this
String ListPickerOperator= ((String)this.ListPicker.SelectedItem);
switch (ListPickerOperator)
{
case "item1":
MessageBox.Show("item 1 was selected");
break;
case "item2":
MessageBox.Show("item 2 was selected");
break;
/*
.
* .
* .
* .
* .
*/
}

Related

How to know which indicator you are on with CarouselView in Xamarin Forms?

I a using the CarouselView and I can successfully load in items! Yey, so far so good. But now when i reach the 2nd item, I try to do a specific function that will only happen in the 2nd index.
Right now i seem to have figured out when im on the 2nd page al though the log is writing a lot of things in the log, but when i transition to the 3rd page, or back to the 1st page, i have lost count (programmatically) of where I am.
<CarouselView BackgroundColor="Transparent"
HorizontalScrollBarVisibility="Never"
IndicatorView="indicatorView"
VerticalOptions="CenterAndExpand"
Margin="25,0"
Scrolled="CarouselView_Scrolled"
ItemsSource="{Binding BindingContext.Intro, Source={x:Reference ParentView}}">
<CarouselView.ItemsLayout>
<LinearItemsLayout Orientation="Horizontal"
SnapPointsAlignment="Center"
SnapPointsType="MandatorySingle"/>
</CarouselView.ItemsLayout>
<CarouselView.ItemTemplate>
<DataTemplate>
<Label Text="{Binding Text}"
FontSize="Large"
HorizontalTextAlignment="Start"
FontFamily="Helvetica Neue"
TextColor="DimGray"
FontAttributes="None" />
</AbsoluteLayout>
</DataTemplate>
</CarouselView.ItemTemplate>
</CarouselView>
Code behind:
async void CarouselView_Scrolled(System.Object sender, Xamarin.Forms.ItemsViewScrolledEventArgs e)
{
System.Diagnostics.Debug.WriteLine(e.CenterItemIndex);
if (e.CenterItemIndex == 1)
{
if (Transitioning == false)
{
Transitioning = true;
await ParentView.ColorTo(Color.White, Color.FromHex("#161825"), c =>
{
ParentView.BackgroundColor = c;
Transitioning = false;
}, 500);
}
}
else
{
Transitioning = true;
await ParentView.ColorTo(Color.FromHex("#161825"), Color.White, c => ParentView.BackgroundColor = c, 500);
Transitioning = false;
}
}
What logic do i need to add in my frontend to successfully monitor if im on page 2?
ItemsViewScrolledEventArgs has a CenterItemIndex property that tells you which item is in the center view
or you could use the CurrentItemChanged event

how to arrange two images side by side in list view in windows phone RT app

I am developing windows phone app,in this app I want to arrange two images side by side using list view control. Actually what happend is I am binding images in listview ,but images are binding one image below another image was binding . how to arrange images side by side in listview and how to set width of the image in all the resolution screens.please help me.
Below is my sample code.
<ListView x:Name="listviewfreecredits" Margin="0,-1,0.643,10" Height="521" IsItemClickEnabled="True" ItemClick="listviewfreecredits_ItemClick">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel >
<Grid Margin="10,10,50,10" Width="389" >
<Image Source="{Binding DealImage}" Stretch="None" Width="400" Height="300" />
</Grid>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
string result = "";
HttpWebRequest request = HttpWebRequest.Create(Url.weburl + "FreeCredits_v2?PlatFormID=7") as HttpWebRequest;
request.Method = "GET";
WebResponse response = await request.GetResponseAsync();
using (var reader = new StreamReader(response.GetResponseStream()))
{
result = reader.ReadToEnd();
// Do anything with you content. Convert it to xml, json or anything.
}
JObject jobj = JObject.Parse(result);
JArray jarr = (JArray)jobj["Deals"];
if (jarr != null)
{
for (int i = 0; i < jarr.Count; i++)
{
string DealImage = (string)jarr[i]["DealImage"];
string DealID = (string)jarr[i]["DealID"];
string Buttontext = (string)jarr[i]["Buttontext"];
freecredit obj = new freecredit();
obj.DealImage = Url.imgurl + DealImage;
obj.DealID = DealID;
obj.ButtonText = Buttontext;
this.listviewfreecredits.Items.Add(obj);
}
Use GridView control instead of ListView, and for width for each image you can calculate it and bind width and height and height of the image in your image control. e.g.
<GridView x:Name="listviewfreecredits" Margin="0,-1,0.643,10" Height="521" IsItemClickEnabled="True" ItemClick="listviewfreecredits_ItemClick">
<GridView.ItemTemplate>
<DataTemplate>
<StackPanel >
<Grid Margin="10,10,50,10" Width="389" >
<Image Source="{Binding DealImage}" Stretch="None" Width="{Bind ImageWidth}" Height="{Bind ImageWidth}" />
</Grid>
</StackPanel>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
for (int i = 0; i < jarr.Count; i++)
{
string DealImage = (string)jarr[i]["DealImage"];
string DealID = (string)jarr[i]["DealID"];
string Buttontext = (string)jarr[i]["Buttontext"];
freecredit obj = new freecredit();
obj.DealImage = Url.imgurl + DealImage;
obj.DealID = DealID;
obj.ButtonText = Buttontext;
//set image width, height by calculating with height accordingly
obj.ImageWidth = ((Windows.UI.Xaml.Window.Current.Bounds.Width - 50/ 2);
this.listviewfreecredits.Items.Add(obj);
}
Define ImageWidth property as double in your freecredit class

Samples on Pushpin infobox in windows phone 7 bing maps?

How to show the info box when click on pushpins in windows phone 7?i.e when click on pushpin need to show some data and arrow button on the right side of data.please help me ...
when you click on pushpin at that time you display message box.
i.e In Your click event put message box.see bellow coding
pin[i] = new Pushpin();
pin[i].Location = new GeoCoordinate(Latitude, LongLatitude);
map1.Children.Add(pin[i]);
myCoorditeWatcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High);
myCoorditeWatcher.MovementThreshold = 20;
var gl = GestureService.GetGestureListener(pin[i]);
gl.Tap += new EventHandler<GestureEventArgs>(GestureListener_Stack_Tap);
private void GestureListener_Stack_Tap(object sender, Microsoft.Phone.Controls.GestureEventArgs e)
{
for (int i = 0; i <= ClsGetDeviceMap.lstLongLatitude.Count - 1; i++)
{
if (sender.Equals(pin[i]))
{
MessageBox.Show(ClsGetDeviceMap.lstLocationName.ElementAt<string>(i).Trim());
MessageBox.Show(ClsGetDeviceMap.lstLatitude.ElementAt<string>(i).Trim());
MessageBox.Show(ClsGetDeviceMap.lstLongLatitude.ElementAt<string>(i).Trim());
}
}
}
You can customize your Pushpin
for example :
<my:Map Name="map1">
<my:Map.Resources>
<DataTemplate x:Key="pushpinTpl">
<my:Pushpin PositionOrigin="{Binding position}">
<my:Pushpin.ContentTemplate>
<DataTemplate>
<ToggleButton x:Name="togButt" >
<Grid>
<TextBlock Text="click"
Visibility="{Binding ElementName=togButt,Path=IsChecked,Converter={StaticResource BoolConverter},ConverterParameter=!}"/>
<TextBlock Text="alternative content"
Visibility="{Binding ElementName=togButt,Path=IsChecked,Converter={StaticResource BoolConverter}}"/>
</Grid>
</ToggleButton>
</DataTemplate>
</my:Pushpin.ContentTemplate>
</my:Pushpin>
</DataTemplate>
</my:Map.Resources>
<my:MapItemsControl ItemsSource="{Binding items}"
ItemTemplate="{StaticResource pushpinTpl}"
/>
</my:Map>
hope this help :)
There are alot of examples on the Bind sdk site:
http://www.bingmapsportal.com/isdk/ajaxv7

Passing the value of a bound list

I had this code working before, but have somehow deleted it as I was trying to get something else fixed.
So I have a list which is bound as a View model, and the list has three lines on it.
I want to be able to click on the list, get the value of the third line on another page and use that value.
Here is the code for the selection of the list
private void List_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// If selected index is -1 (no selection) do nothing
if (List.SelectedIndex == -1)
return;
// Navigate to the new page
NavigationService.Navigate(new Uri("/Route.xaml?selectedItem=" + List.SelectedIndex, UriKind.Relative));
string urlWIthData = string.Format("/Route.xaml?name={0}", " ");
this.NavigationService.Navigate(new Uri(urlWIthData, UriKind.Relative));
// Reset selected index to -1 (no selection)
List.SelectedIndex = -1;
}
Then, on the route page I want to be able to get the value and use the value line three....
How do I do this?
EDIT:
so this one part of the list:
this.Items.Add(new ItemViewModel() { LineOne = "Images/1.png", LineTwo = "Whitehawk - County
Hospital - City Centre - Hove - Portslade - Mile Oak", LineThree = "1 Whitehawk - Mile Oak",
LineFour = "1 Mile Oak - Whitehawk", LineFive = "1149" });
I Display the list on one page :
<ListBox Margin="6,6,7,6" ItemsSource="{Binding Items}" Name="List" OpacityMask="#FFD38648" FontSize="26" SelectionChanged="List_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="0,0,0,17" DataContext="{Binding}">
<Image Name="Images" Source="{Binding LineOne}">
</Image>
<TextBlock Text="{Binding LineTwo}" Style="{StaticResource PhoneTextSubtleStyle}" Name="textblock3"></TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
You can cast the list's SelectedItem to type Item and get the value from there:
private void List_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// If selected index is -1 (no selection) do nothing
if (List.SelectedIndex == -1)
return;
// Here's the codes -----------------------
var item = Items.SelectedItem as Item;
if(item == null) //cast didn't work
return;
var lineThree = item.LIneThree;
// end of the codes -----------------------
// Navigate to the new page
NavigationService.Navigate(new Uri("/Route.xaml?selectedItem=" + List.SelectedIndex, UriKind.Relative));
string urlWIthData = string.Format("/Route.xaml?name={0}", " ");
this.NavigationService.Navigate(new Uri(urlWIthData, UriKind.Relative));
// Reset selected index to -1 (no selection)
List.SelectedIndex = -1;
}

Windows Phone 7 Back Button issue

In my app I have several pages. When I click on the Windows "Back" Button everything goes back as expected.
However I have 2 pages that are causing me grief. Page "A" is doing some binding in the XAML:
<ListBox x:Name="lbPrograms" ItemsSource="{Binding Items}" SelectionChanged="lbPrograms_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel x:Name="DataTemplateStackPanel" Orientation="Horizontal">
<Image x:Name="ItemImage" Source="/images/ArrowImg.png" Height="43" Width="43" VerticalAlignment="Top" Margin="10,0,20,0"/>
<StackPanel>
<TextBlock x:Name="ItemText" Text="{Binding programName}" Margin="-2,-13,0,0" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
<TextBlock x:Name="DetailsText" Text="{Binding createDate}" Margin="0,-6,0,3" Style="{StaticResource PhoneTextSubtleStyle}"/>
</StackPanel>
<Image x:Name="ItemFavs" Source="/images/favs.png" Height="43" Width="43" VerticalAlignment="Top" Margin="10,0,20,0"/>
<Image x:Name="ItemDelete" Source="/images/delete.png" Height="43" Width="43" VerticalAlignment="Top" Margin="10,0,20,0"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
The code behind for Page A is fairly simple:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
// Set the data context of the listbox control to the sample data
if (DataContext == null)
DataContext = App.ViewModel;
App.ViewModel.Refresh();
lbPrograms.ItemsSource = App.ViewModel.Items;
}
private void lbPrograms_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
NavigationService.Navigate(new Uri("/DisplayProgram.xaml?selectedItem=" + lbPrograms.SelectedIndex, UriKind.Relative));
}
private void BackBtn_Click(object sender, EventArgs e)
{
NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
}
Page B has no binding in the XAML as I am taking the data from the ModelView a drawing it out dynamically on the screen. Like so:
private int index;
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
string selectedIndex = "";
if (NavigationContext.QueryString.TryGetValue("selectedItem", out selectedIndex))
{
//prevents error
if (int.Parse(selectedIndex) == -1)
{
return;
}
if ((DataContext == null))
{
index = int.Parse(selectedIndex);
App.ViewModel.Refresh();
DataContext = App.ViewModel.Items[index].nValDictionary;
int i = 0;
foreach (KeyValuePair<string, string> kvp in (((System.Collections.Generic.Dictionary<string, string>)(DataContext))))
{
StackPanel sp = new StackPanel();
sp.Name = "sp" + i;
sp.Background = new SolidColorBrush(Colors.Black);
sp.Width = 460;
WrapPanel wp = new WrapPanel();
wp.Name = "test" + i;
wp.Width = 300;
wp.Height = 200;
TextBlock txt = new TextBlock();
txt.Text = kvp.Key.ToString();
txt.Foreground = new SolidColorBrush(Colors.White);
sp.Children.Add(txt);
int chkBoxesVal = 0;
if (kvp.Value == "")
{
chkBoxesVal = 0;
}else{
chkBoxesVal = Convert.ToInt32(kvp.Value.ToString());
}
int iCount = 0;
for (iCount = 0; iCount <= chkBoxesVal - 1; iCount++)
{
CheckBox chk = new CheckBox();
chk.Name = i.ToString();
chk.Width = 56;
chk.Height = 70;
chk.Content = "";
//chk.Background = new SolidColorBrush(Colors.Black);
//chk.BorderBrush = new SolidColorBrush(Colors.White);
chk.Style = (Style)Application.Current.Resources["checkBoxNG"];
wp.Children.Add(chk);
}
sp.Children.Add(wp);
lbItems.Items.Add(sp);
i += 1;
}
}
}
}
}
So when I'm going forward everything works fine, but when I hit the Windows "Back" button on Page B I get an error. I stepped through my code and when I hit the "Back" button it does go back to Page A, but then it is also going to Page B, which then throws the error and stops. So can anyone tell me why this behavior is happening? I would expect that it would go back to Page A and just stop there. Not to go back to Page B. is there something in my code that is causing it to reload Page B? Any resources that you can provide that might explain this behavior is also welcome!
Thanks!
It looks like SelectionChanged on Page A is firing as a result of the ItemsSource initialisation you're doing in OnNavigatedTo.
You could verify the SelectedIndex is -1 before taking any action in the SelectionChanged event.
Alternatively you could remove any existing event handler on SelectionChanged while doing this initialisation and restore that event handler on completion.
My assumption is that lbPrograms_SelectionChanged event occurs when you press back button and page A is loaded again.
Change your navigation design. For your DataTemplateStackPanel you could use ManipulationStarted event
and inside add
NavigationService.Navigate(new Uri("/DisplayProgram.xaml?selectedItem=" + lbPrograms.SelectedIndex, UriKind.Relative));

Resources