How I can add pushpins with name and description to bing maps - windows-phone-7

I creating app for windows phone 7 (bing maps) , and I need add something like this:
when I tap on the map, I want add pushpin there, but I also need to add some information to that pushpin (like name and description)
how can i do this? or where can i find some tutorial for that ?

You could subscribe to the Tap event of the map and add Pushpins.
XAML:
<Maps:Map x:Name="Map" CredentialsProvider="Your-Creds" Tap="Map_OnTap" />
Code:
private void Map_OnTap(object sender, GestureEventArgs e)
{
GeoCoordinate location;
if (Map.TryViewportPointToLocation(e.GetPosition(Map), out location))
{
Map.Children.Add(new Pushpin
{
Location = location,
Background = new SolidColorBrush(Colors.Blue),
Content = "Hello world"
});
}
}
If you would like an MVVM approach you could have a collection of objects that you add to.
Xaml:
<Maps:Map x:Name="Map" CredentialsProvider="Your-creds"
Tap="Map_OnTap">
<Maps:MapItemsControl ItemsSource="{Binding Locations}">
<Maps:MapItemsControl.ItemTemplate>
<DataTemplate>
<Maps:Pushpin Location="{Binding Location}" Content="{Binding Name}"
Background="{StaticResource PhoneAccentBrush}">
</Maps:Pushpin>
</DataTemplate>
</Maps:MapItemsControl.ItemTemplate>
</Maps:MapItemsControl>
</Maps:Map>
Code:
public ObservableCollection<MapLocation> Locations { get; private set; }
private void Map_OnTap(object sender, GestureEventArgs e)
{
GeoCoordinate location;
if (Map.TryViewportPointToLocation(e.GetPosition(Map), out location))
{
Locations.Add(new MapLocation
{
Location = location,
Name = "Hello world"
});
}
}
public class MapLocation
{
public GeoCoordinate Location { get; set; }
public string Name { get; set; }
}
This example simply sets the DataContext of the page to be itself. A better approach is to set the DataContext of the page to be a view model with the Locations property on it.

Related

Unable to display DataTemplate from DataTemplateSelector in CarouselView in Xamarin Forms

I have a carouselview in my Xam.Forms project. I have also created 3 ContentViews (one for each DataTemplate). My template selector class looks like this
public class DashboardTemplateSelector : DataTemplateSelector
{
public DataTemplate QuickMessageTemplate { get; set; }
public DataTemplate DataViewTemplate { get; set; }
public DataTemplate LastUsedTemplate { get; set; }
public DashboardTemplateSelector()
{
QuickMessageTemplate = new DataTemplate(typeof(QuickMessage));
DataViewTemplate = new DataTemplate(typeof(DataView));
LastUsedTemplate = new DataTemplate(typeof(LastusedView));
}
protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
{
var cv = (Frame)item;
DataTemplate rv = null;
switch(cv.ClassId)
{
case "data":
rv = DataViewTemplate;
break;
case "quick":
rv= QuickMessageTemplate;
break;
case "last":
rv = LastUsedTemplate;
break;
}
return rv;
}
Nothing out of the ordinary and I have the ClassId on each frame within the ContentView set to match the name in the switch.
When I build the app and run it, it looks fine but there is nothing in the CarouselView and a break point set in the OnSelectTemplate method (the first line) is never hit.
My XAML for the carouselview is this
<ContentPage.Resources>
<ResourceDictionary>
<local:DashboardTemplateSelector x:Key="templateSelector" />
</ResourceDictionary>
</ContentPage.Resources>
<CarouselView Grid.Row="2" PeekAreaInsets="12" Margin="8" ItemTemplate="{StaticResource templateSelector}" HeightRequest="200" BackgroundColor="BlueViolet" />
The view shows (can see the background colour) but nothing in the view itself.
I've only checked this on a physical android device and not on iOS, but I'm guessing the same retult. My guess is that I can't cast to a Frame for the object, but I'm not sure.
To populate data, you have to set an ItemsSource via DataBinding or Code-Behind.
Then your DataTemplateSelector will be hit with each item of the ItemsSource as object item. Please see the documentation here: https://learn.microsoft.com/fr-fr/xamarin/xamarin-forms/user-interface/carouselview/layout
<CarouselView Grid.Row="2" PeekAreaInsets="12" Margin="8" ItemsSource="{Binding ViewsViewModels} ItemTemplate="{StaticResource templateSelector}" HeightRequest="200" BackgroundColor="BlueViolet" />
The item in OnSelectTemplate is itemsource data. Change the item to container and use CarouselView instead of Frame. Do not forget to set the ClassId of your CarouselView.
The whold project for your reference.
Xaml:
<ContentPage.Resources>
<ResourceDictionary>
<local:DashboardTemplateSelector x:Key="templateSelector" />
</ResourceDictionary>
</ContentPage.Resources>
<CarouselView
Grid.Row="2"
Margin="8"
ClassId="data"
HeightRequest="200"
ItemTemplate="{StaticResource templateSelector}"
ItemsSource="{Binding infos}"
PeekAreaInsets="12" />
Code behind:
public partial class MainPage : ContentPage
{
public ObservableCollection<Info> infos { get; set; }
public MainPage()
{
InitializeComponent();
infos = new ObservableCollection<Info>()
{
new Info{ DataViewText="DataViewText1", LastusedViewText="LastusedViewText1", QuickMessageText="QuickMessageText1"},
new Info{ DataViewText="DataViewText2", LastusedViewText="LastusedViewText2", QuickMessageText="QuickMessageText2"},
new Info{ DataViewText="DataViewText3", LastusedViewText="LastusedViewText3", QuickMessageText="QuickMessageText3"},
new Info{ DataViewText="DataViewText4", LastusedViewText="LastusedViewText4", QuickMessageText="QuickMessageText4"},
};
this.BindingContext = this;
}
}
public class Info
{
public string QuickMessageText { get; set; }
public string DataViewText { get; set; }
public string LastusedViewText { get; set; }
}
QuickMessage, DataView and LastusedView is a contentview with label which binding a text.
QuickMessage:
<Label Text="{Binding QuickMessageText}" />
DataView:
<Label Text="{Binding DataViewText}" />
LastusedView:
<Label Text="{Binding LastusedViewText}" />
DashboardTemplateSelector:
public class DashboardTemplateSelector : DataTemplateSelector
{
public DataTemplate QuickMessageTemplate { get; set; }
public DataTemplate DataViewTemplate { get; set; }
public DataTemplate LastUsedTemplate { get; set; }
public DashboardTemplateSelector()
{
QuickMessageTemplate = new DataTemplate(typeof(QuickMessage));
DataViewTemplate = new DataTemplate(typeof(DataView));
LastUsedTemplate = new DataTemplate(typeof(LastusedView));
}
protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
{
var cv = (CarouselView)container;
DataTemplate rv = null;
switch (cv.ClassId)
{
case "data":
rv = DataViewTemplate;
break;
case "quick":
rv = QuickMessageTemplate;
break;
case "last":
rv = LastUsedTemplate;
break;
}
return rv;
}
}

Carousal view bind auto duration value - Xamarin.forms

In my xamarin.forms app, I am using Cardview's link cubeview. The cubeview is similar to carousal with a property called auto sliding. We can set the duration and it will auto slide after that time. In this cube view I am using two different templates. One for showing image and another one for video. I can show these two different views by using data template selector. Everything works fine.
Now, since the auto slide property is hard coded, even if the video or image fully loaded(I am fetching them from URL.) the view will get slided. How can bind or reference the auto duration value to the loading property of Image and video?
For Image view I am using FFImageloading
For Video view I am using Rox.Xamarin.Video
In summarise, if the image is loaded, I want to make the auto slide value to 7 seconds and if it is video I want the autoslide duration needs to be the duration of video. How can I do that? Any help is apprecited.
My cube view
<cards:CubeView
x:Name="StatusCarousal"
IsClippedToBounds="True"
SlideShowDuration="7000" // This value needs to be bind according to template
IsVerticalSwipeEnabled="True"
ItemSwiped="StatusCarousal_ItemSwiped"
PropertyChanged="StatusCarousal_PropertyChanged"
ItemsSource="{Binding}"
ItemTemplate="{StaticResource personDataTemplateSelector}"
HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" >
</cards:CubeView>
Data Templates
<ContentPage.Resources>
<ResourceDictionary>
<DataTemplate x:Key="Page1Template" >
<ffimageloading:CachedImage Grid.Column="0" Grid.ColumnSpan="3"
x:Name="StoryImage"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
Source="{Binding Image}"
Aspect="AspectFill"
Success="CachedImage_Success"
FileWriteFinished="CachedImage_FileWriteFinished"
DownsampleToViewSize="true"
DownloadStarted="CachedImage_DownloadStarted"
>
</ffimageloading:CachedImage>
</DataTemplate>
<DataTemplate x:Key="Page2Template">
<rox:VideoView Source="{Binding VideoURl}" AutoPlay="True" FullScreen="True"
Grid.Column="0" Grid.ColumnSpan="3"
HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"
>
</rox:VideoView>
</Grid>
</DataTemplate>
<local:PersonDataTemplateSelector x:Key="personDataTemplateSelector"
Page1="{StaticResource Page1Template}"
Page2="{StaticResource Page2Template}" />
</ResourceDictionary>
</ContentPage.Resources>
Model class
public partial class InnerStoreisData : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public string UserName { get; set; }
public ImageSource profileImage { get; set; }
public string VideoURl { get; set; }
public string Image { get; set; }
public int Type { get; set; }
}
Template selector
public class PersonDataTemplateSelector : DataTemplateSelector
{
public DataTemplate Page1 { get; set; }
public DataTemplate Page2 { get; set; }
protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
{
switch (((InnerStoreisData)item).Type)
{
case 0:
return Page1;
case 1:
return Page2;
default:
return Page1;
}
}
}

XML into Pins on a map

I can get the list of Lat and Long in a list as a full string. I now need to be able to use this in the map.
Here is the code to display the map. Currently it's displaying two pins and the location. (xaml)
phone:PhoneApplicationPage
x:Class="BrightonHoveBuses.location"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d" d:DesignHeight="696" d:DesignWidth="480"
shell:SystemTray.IsVisible="True" xmlns:my="clr-
namespace:Microsoft.Phone.Controls.Maps;assembly=Microsoft.Phone.Controls.Maps">
<phone:PhoneApplicationPage.Resources>
<DataTemplate x:Key="PinItemTemplate">
<my:Pushpin Location="{Binding Location}"
MouseLeftButtonUp="Pushpin_MouseLeftButtonUp_1"
Content="{Binding Id}">
</my:Pushpin>
</DataTemplate>
<ControlTemplate x:Key="pinMyLoc"
TargetType="my:Pushpin">
<Grid Height="26"
Width="26"
Margin="-13,-13,0,0"
RenderTransformOrigin="0.5,0.5">
<Grid.RenderTransform>
<CompositeTransform Rotation="-45" />
</Grid.RenderTransform>
<Rectangle Fill="Black"
HorizontalAlignment="Center" Margin="0" Stroke="White" VerticalAlignment="Center" Height="26" Width="26" />
<Ellipse HorizontalAlignment="Center"
Height="16" Margin="0" VerticalAlignment="Center"
Fill="Teal"
Width="16" />
</Grid>
</ControlTemplate>
<DataTemplate x:Key="BusItemTemplate">
<my:Pushpin Location="{Binding Location}"
Name="{Binding Id}"
MouseLeftButtonUp="Pushpin_MouseLeftButtonUp_1">
<Grid>
<StackPanel Orientation="Horizontal">
<Image Source="/Images/1.png" Stretch="None" Width="20" Height="25"/>
</StackPanel>
</Grid>
</my:Pushpin>
</DataTemplate>
<ControlTemplate x:Key="stops"
TargetType="my:Pushpin">
<Grid Height="26"
Width="26"
Margin="-13,-13,0,0"
RenderTransformOrigin="0.5,0.5">
<Grid.RenderTransform>
<CompositeTransform Rotation="-45" />
</Grid.RenderTransform>
<Rectangle Fill="Black"
HorizontalAlignment="Center" Margin="0" Stroke="White" VerticalAlignment="Center" Height="26" Width="26" />
<Ellipse HorizontalAlignment="Center"
Height="16" Margin="0" VerticalAlignment="Center"
Fill="Yellow"
Width="16" />
</Grid>
</ControlTemplate>
</phone:PhoneApplicationPage.Resources>
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle" Text="BRIGHTON & HOVE BUSES" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="Stop Map" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<!--ContentPanel - place additional content here-->
<!-- This is the coding for the pushpins on the map-->
<Grid Grid.RowSpan="2">
<my:Map Name="map1" CredentialsProvider="Apg4fepCyTYEAQ0UxPKpbIljr_THidmUi7BNRth0JGtGSE0blId5FJSJqYy80kQC" Center="47.620574,-122.34942" Margin="0,139,6,99" Height="458" ZoomLevel="17" LogoVisibility="Visible" CopyrightVisibility="Visible" Loaded="startLocationButton_Click">
<my:Pushpin Location="{Binding CurrentLocation}" Template="{StaticResource pinMyLoc}" Name="myPushPin"/>
<my:MapItemsControl x:Name="MapPins"
ItemsSource="{Binding Pins}"
ItemTemplate="{StaticResource PinItemTemplate}"
/>
</my:Map>
<ListBox Height="87" HorizontalAlignment="Left" Margin="12,603,0,0" Name="listBox1" VerticalAlignment="Top" Width="460" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding latitude}"/>
<TextBlock Text="{Binding NaptanCode}"/>
<TextBlock Text=" "/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Grid>
<phone:PhoneApplicationPage.ApplicationBar>
<shell:ApplicationBar Opacity="1.0" IsMenuEnabled="True" IsVisible="True">
<shell:ApplicationBarIconButton Text="Zoom In" IconUri="/Images/add.png" Click="Buttonplus_Click" />
<shell:ApplicationBarIconButton Text="Zoom Out" IconUri="/Images/minus.png" Click="Buttonminus_Click" />
<shell:ApplicationBarIconButton Text="Me" IconUri="/Images/location.png" Click="ButtonLocation_Click" />
<shell:ApplicationBar.MenuItems>
<shell:ApplicationBarMenuItem Text="Road View" Click="ApplicationBarRoad_Click" />
<shell:ApplicationBarMenuItem Text="Aerial View" Click="ApplicationBarAerial_Click" />
</shell:ApplicationBar.MenuItems>
</shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>
</phone:PhoneApplicationPage>
Heres the code....
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Device.Location;
using Microsoft.Phone.Shell;
using System.Xml.Serialization;
using System.Xml;
using System.IO.IsolatedStorage;
using Microsoft.Phone.Controls;
using System.Runtime.Serialization.Json;
using System.Collections.ObjectModel;
using System.Runtime.Serialization;
using System.Text;
using System.Xml.Linq;
using System.Data.Linq.Mapping;
using System.ComponentModel;
using Microsoft.Phone.Controls.Maps;
using Microsoft.Phone.Controls.Maps.Platform;
namespace BrightonHoveBuses
{
public partial class location : PhoneApplicationPage
{
public location()
{
InitializeComponent();
DataContext = App.ViewMapModel;
MapViewModel view = new MapViewModel();
view.Load();
this.DataContext = view;
WebClient client = new WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
Uri url = new Uri("http://www.henry-edwards.co.uk/feed.txt", UriKind.Absolute);
client.DownloadStringAsync(url);
}
GeoCoordinateWatcher watcher;
// Click the event handler for the “Start Location” button.
private void startLocationButton_Click(object sender, RoutedEventArgs e)
{
// The watcher variable was previously declared as type GeoCoordinateWatcher.
if (watcher == null)
{
watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High); // using high accuracy
watcher.MovementThreshold = 20; // use MovementThreshold to ignore noise in the signal
watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged);
watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);
}
watcher.Start();
} // End of the Start button Click handler.
public class RootContainer
{
[DataMember]
public string StopName { get; set; }
[DataMember]
public string StopId { get; set; }
[DataMember]
public string Stop { get; set; }
[DataMember]
public string RouteId { get; set; }
[DataMember]
public string RouteName { get; set; }
[DataMember]
public string latitude { get; set; }
[DataMember]
public string longitude { get; set; }
[DataMember]
public List<Location> Location { get; set; }
}
void watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
{
if ((App.Current as App).locsettings == false)
{
MessageBoxResult m = MessageBox.Show("Do you want to allow this application to use information about your location?", "Use Location", MessageBoxButton.OKCancel);
if (m == MessageBoxResult.OK)
{
watcher.Start();
(App.Current as App).locsettings = true;
}
else if (m == MessageBoxResult.Cancel)
{
watcher.Stop();
(App.Current as App).locsettings = false;
}
}
switch (e.Status)
{
case GeoPositionStatus.Disabled:
// The Location Service is disabled or unsupported.
// Check to see whether the user has disabled the Location Service.
if (watcher.Permission == GeoPositionPermission.Denied)
{
// The user has disabled the Location Service on their device.
MessageBox.Show("Location services must be enabled in your phone settings");
}
else
{
MessageBox.Show("Location services must be enabled");
}
break;
}
}
// Click the event handler for the “Start Location” button.
private void stopLocationButton_Click(object sender, RoutedEventArgs e)
{
watcher.Stop();
}
private GeoCoordinateWatcher loc = null;
public string stopslist;
private void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
myPushPin.Location = e.Position.Location;
map1.SetView(myPushPin.Location, 17.0);
watcher.MovementThreshold = 100;
}
void loc_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
{
if (e.Status == GeoPositionStatus.Ready)
{
map1.SetView(loc.Position.Location, 17.0);
loc.Stop();
}
}
void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error == null)
{
if (e.Result != null)
{
XDocument doc = XDocument.Parse(e.Result);
XNamespace ns = "http://schemas.datacontract.org/2004/07/BusExpress.ClassLibrary";
var locations = (from n in doc.Descendants(ns + "ArrayOfStop")
select new RootContainer
{
Location = (from s in n.Elements(ns + "Stop")
select new Location
{
latitude = s.Element(ns + "Lat").Value + " ," + s.Element(ns + "Long").Value,
// longitude = s.Element(ns + "Long").Value,
}).ToList()
}).Single();
// Do something with the list of Route Names in routeNames
listBox1.ItemsSource = locations.Location;
}
}
}
public class MapViewModel : INotifyPropertyChanged
{
public void Load()
{
//Do something here to populate your view collection with pins
Pins.Add(new PinModel() { Id = 2, Name = string.Format("Pin # 2"), Location = new GeoCoordinate(39.932825, -75.168396) });
}
private ObservableCollection<PinModel> _pins = new ObservableCollection<PinModel>();
public ObservableCollection<PinModel> Pins
{
get { return _pins; }
set { _pins = value; RaisePropertyChanged("Pins"); }
}
//Event code to ensure the page updates to model changes.
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private void Pushpin_MouseLeftButtonUp_1(object sender, MouseButtonEventArgs e)
{
Pushpin pin = (Pushpin)sender;
MessageBox.Show(pin.Content.ToString());
}
public class PinModel
{
public string Name { get; set; }
public int Id { get; set; }
public GeoCoordinate Location { get; set; }
}
}
private void ButtonLocation_Click(object sender, EventArgs e)
{
loc = new GeoCoordinateWatcher(GeoPositionAccuracy.Default);
//EventHandler for location service status changes
loc.StatusChanged += loc_StatusChanged;
//If the location service is disabled or not supported
if (loc.Status == GeoPositionStatus.Disabled)
{
//Display a message
MessageBox.Show("Location services must be enabled");
return;
}
loc.Start();
}
private void Pushpin_MouseLeftButtonUp_1(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
Pushpin pin = (Pushpin)sender;
}
private void Buttonminus_Click(object sender, EventArgs e)
{
double zoom;
zoom = map1.ZoomLevel;
map1.ZoomLevel = --zoom;
}
private void Buttonplus_Click(object sender, EventArgs e)
{
double zoom;
zoom = map1.ZoomLevel;
map1.ZoomLevel = ++zoom;
}
private void ApplicationBarRoad_Click(object sender, EventArgs e)
{
map1.Mode = new RoadMode();
}
private void ApplicationBarAerial_Click(object sender, EventArgs e)
{
map1.Mode = new AerialMode();
}
}
}
EDIT:
public class PinModel : INotifyPropertyChanged
{
public string Name { get; set; }
public int Id { get; set; }
public GeoCoordinate Location { get; set; }
}
EDIT:
I am trying to get all the stops on a bing map. I can get all the locations in a list, this is populated to a list box. This all works.
It's now trying to get all these locations onto the map, also with names.
So i need to do whats on the website - but on the phone - http://www.buses.co.uk/travel/live-bus-times.aspx
Have you checked out the output of your LINQ method? It appears you are only creating one instance in your list. Try this instead of your current parsing method.
var locations = (from n in doc.Descendants(ns + "ArrayOfStop")
select new RootContainer
{
Location = (from s in n.Elements(ns + "Stop")
select new Location
{
latitude = s.Element(ns + "Lat").Value + " ," + s.Element(ns + "Long").Value,
// longitude = s.Element(ns + "Long").Value,
}).ToList()
});
I'm not pretty sure what your question is, but I think your binding with the pushpins is not working?
You have implemented INotifyPropertyChanged on your ObservableCollection. That's not necessary. To make it work, you have to implement it on the PinModel and when you set the Location property than call the RaisePropertyChanged method.
Your PinModel class should look something like below to make your binding work (Allthough it's possible that your binding is working right now, because you bind it once as a whole.)
public class PinModel : INotifyPropertyChanged
{
public string Name { get; set; }
public int Id { get; set; }
private GeoCoordinate _location;
public GeoCoordinate Location
{
get
{
return _location;
}
set
{
_location = value;
RaisePropertyChanged("Location");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Also note that het .Single() method your calling on your linq query is returning a single value instead of a list. Please have a look at the answer by Lance on how to contruct your linq query. (See this link for the .Single() documentation)

ObservableCollection not updating the listview

I have an ObservableCollection which is viewed in a custom listbox. I need the listbox to update the view according to changes applied, like inserting new feeds or removing feeds from the ObservableCollection
parts of code are available below
public class lbl
{
public ObservableCollection<feed> ModifiedItems
= new ObservableCollection<feed>();
public lbl()
{
InitializeComponent();
listBox1.ItemsSource = ModifiedItems ;
}
public void update(object sender, EventArgs e)
{
var x = ModifiedItems.Last();
listBox1.Items.Add(x);
}
}
public class feed
{
public int ID { get; set; }
public int source_id { get; set; }
public string title { get; set; }
public string source_icon { get; set; }
public string url { get; set; }
public string pudate { get; set; }
}
XAML
<ListBox x:Name="listBox1" >
<ListBox.ItemTemplate >
<DataTemplate >
<StackPanel Width="400" Margin="20" >
<Button x:Name="pic" Tag="{Binding Id}">
<Button.Template>
<ControlTemplate>
<TextBlock Text="{Binding title}" TextWrapping="Wrap" FontFamily="Arial" FontSize="28" Tag="{Binding Id}"/>
</ControlTemplate>
</Button.Template>
</Button>
<TextBlock Text="{Binding pudate}" TextWrapping="Wrap" FontSize="24"/>
<Image Source="{Binding source_icon}" Width="100" Height="60"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Note: This is not part of the code.
It gives me the error when trying to add an Item "Operation not supported on read-only collection."
I tried the solution posted here Implementing CollectionChanged and still I get the same error.
Any help please, Thanks in advance
The problem is with your update method:
public void update(object sender, EventArgs e)
{
var x = ModifiedItems.Last();
listBox1.Items.Add(x);
}
The ItemsSource of your ListBox is set to the ModifiedItems which is an ObservableCollection. Therefore if you add or remove items from this collection, the ListBox UI will update automatically. For example, to add a new items to your view simply do the following:
ModifiedItems.Add(new feed());
This is the whole point of an ObservableCollection, the view can observe it!
If, rather than adding / removing items, you are updating existing items, you will need to make feed implement INotifyPropertyChanged.
Since you're setting the ItemsSource of the ListBox, you're binding the ModifiedItems collection to it.
This means you have to modify ModifiedItems, and not the ListBox to add/remove items, which then will update accordingly.
public void update(object sender, EventArgs e)
{
var x = ModifiedItems.Last();
ModifiedItems.Items.Add(x);
}
Why you want to duplicate the last item is beyond me. But that's the change you need to do.

"Refresh" Pivot Control with Mvvm-light toolkit for WP7

I have in my Xaml a pivot control :
<controls:Pivot ItemsSource="{Binding ObjectList}">
<controls:Pivot.HeaderTemplate>
<DataTemplate>
<TextBlock />
</DataTemplate>
</controls:Pivot.HeaderTemplate>
<controls:Pivot.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Value1}" />
<TextBlock Text="{Binding Value2}" />
</StackPanel>
</DataTemplate>
</controls:Pivot.ItemTemplate>
</controls:Pivot>
My ViewModel is :
public class MyObject
{
public string Value1 { get; set; }
public string Value2 { get; set; }
}
public class MyViewModel : ViewModelBase
{
public const string ObjectListPropertyName = "ObjectList";
private ObservableCollection<MyObject> _objectList;
public ObservableCollection<MyObject> ObjectList
{
get
{
return _objectList;
}
private set
{
if (_objectList == value)
return;
_objectList = value;
RaisePropertyChanged(ObjectListPropertyName);
}
}
private DispatcherTimer timer;
public MyViewModel()
{
ObservableCollection<MyObject> collection = new ObservableCollection<MyObject>
{
new MyObject {Value1 = "One"},
new MyObject {Value1 = "Two"},
new MyObject {Value1 = "Tree"}
};
ObjectList = collection;
timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(2)};
timer.Tick += timer_Tick;
timer.Start();
}
void timer_Tick(object sender, EventArgs e)
{
foreach (MyObject myObject in _objectList)
{
myObject.Value2 = "Something";
}
Application.Current.RootVisual.Dispatcher.BeginInvoke( () => RaisePropertyChanged(ObjectListPropertyName));
}
}
When the timer_tick is reached, I supposed the pivot control to refresh with the new values ... but I can't see any changes.
What do I miss ?
Thanks in advance for your help
I'm guessing that possibly updating the members of the list without updating the list itself is the problem. When you raise the property changed event - it is for the entire collection. The collection is still pointing to an equal reference of itself, despite the fact that the members have changed.
Try placing a breakpoint in the setter and see if the property changed event is fired.

Resources