map mode button in WP7 - windows-phone-7

I'm trying to make an ApplicationBarMenuItem that, when clicked, switches my bing map between RoadMode and AerialMode. my pseudocode looks something like this:
private void changeMap_Click(object sender, EventArgs e)
{
if(map1.Mode == RoadMode)
map1.Mode = new Microsoft.Phone.Controls.Maps.AerialMode();
else
map1.Mode = new Microsoft.Phone.Controls.Maps.RoadMode();
}
However, it says I cannot use RoadMode, which is a 'type', like a variable. Does anyone have a way around this?

Because RoadMode is a class and you are trying to compare class to an object.
Try (edit)
if(map1.Mode is RoadMode)

Related

Manipulation_started doesn't work on a map

Hi to all I'm trying to set a listener for the ManipulationStarted, ManipulationDelta, ManipulationCompleted of a map to detect if the user drag a map around, but looks like none of those events are launched if I drag the map. If I set a tap listener for the map ManipulationStarted is correctly launched.
What I'm doing wrong?
xaml code:
<Controls:Map x:Name="myMap"
Grid.Row="0"
Loaded="myMap_Loaded"
ManipulationDelta="myMap_ManipulationDelta"
ManipulationCompleted="myMap_ManipulationCompleted"
ManipulationStarted="myMap_ManipulationStarted"
Tap="myMap_Tap">
code behind:
private void myMap_ManipulationDelta(object sender, System.Windows.Input.ManipulationDeltaEventArgs e)
{
Debug.WriteLine("Event:: MyMap_manipulationdelta");
}
private void myMap_ManipulationCompleted(object sender, System.Windows.Input.ManipulationCompletedEventArgs e)
{
Debug.WriteLine("Event:: MyMap_manipulationcompleted");
}
private void myMap_ManipulationStarted(object sender, System.Windows.Input.ManipulationStartedEventArgs e)
{
Debug.WriteLine("Event:: MyMap_manipulationstarted");
}
private void myMap_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
Debug.WriteLine("Event:: MyMap_tap");
}
I'm on a normal page, no pivot or panorama.
I'm afraid that you won't be able to handle those events because Map control intercepts them. Although there is a property UseOptimizedManipulationRouting, but as I've tested it - it doesn't help much in this situation.
I dont't know what you are trying to achieve, but if you don't need ManipulationDeltaEventArgs then maybe you consider using different events such as: MouseEnter, ResolveCompleted and CenterChanged.
If you need them then as JustinAngel suggested here you can follow these instructions and use Touch.FrameReported event for your purpose.
EDIT - code sample
If I've understood you properly, you would like to know when the User touches the Map, MouseEnter won't be the best choice as it will work only first time, then if mouse didn't leave the Map (user touched somewhere else), it won't fire again. Better solution here (following instructions above) can be such a code:
public MainPage()
{
InitializeComponent();
Touch.FrameReported += Touch_FrameReported;
}
private void Touch_FrameReported(object sender, TouchFrameEventArgs e)
{
TouchPoint point = e.GetPrimaryTouchPoint(myMap);
if (point.Action == TouchAction.Move && point.Position.Y > 0)
{
MessageBox.Show("User is Moving Finger over the Map!");
}
}

How do I detect NSDatePicker ValueChanged in MonoMac?

Unlike UIDatePicker in Monotouch, the NSDatePicker in MonoMac does not appear to have "ValueChanged". How do I detect changes?
Ideally where datePicker is an NSDatePicker, I want to
datePicker.ValueChanged += DatePickerOnValueChanged
For events, I see "Activated" and "ValidateProposedValue", could it be the latter that I want?
Update. I think I can do something like below. Is it equivalent?
datePicker.Action = new MonoMac.ObjCRuntime.Selector ("datePickerAction:");
and
[Export("datePickerAction:")]
private void datePickerAction()
{
// stuff
}
In your ideal case, you say you want to use an event. The ValidateProposedValue event is fine for this:
picker.ValidateProposedDateValue += (object sender, NSDatePickerValidatorEventArgs e) => {
Console.WriteLine(e.ProposedDateValue);
};
It seems to correspond to the NSDatePickerCellDelegate method datePickerCell:validateProposedDateValue:timeInterval:, which is what I would use in Objective C.

WP7 How to write a function: binding data method will be called every 10s?

Could you show me the way or the idea how to execute biding data every 10s??
Let's see the code below:
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
proxy.DanhSachPhongChoiCompleted += new
EventHandler<DanhSachPhongChoiCompletedEventArgs>(proxy_DanhSachPhongChoiCompleted);
proxy.DanhSachPhongChoiAsync();
}
void proxy_DanhSachPhongChoiCompleted(object sender, DanhSachPhongChoiCompletedEventArgs e)
{
Room[] table = e.Result;
listDSPhong.ItemsSource = e.Result;
}
We can see: ater my page loaded, the binding data will execute ONLY ONE TIME. I need to call 2 methods below every 10s. How should I do? Thanks for teaching me!
proxy.DanhSachPhongChoiCompleted += new
EventHandler<DanhSachPhongChoiCompletedEventArgs>(proxy_DanhSachPhongChoiCompleted);
proxy.DanhSachPhongChoiAsync();
You can use e.g. DispatcherTimer class.
Here's the sample:
https://stackoverflow.com/a/3266071/126995
You better start your timer in NavigatedTo, and kill it in NavigatedFrom.
P.S. I suspect there's a memory leak in your code. Please read this.
you can use Microsoft's reactive library
http://msdn.microsoft.com/en-us/data/gg577609.aspx
and do something like this :
public void callfunction()
{
IScheduler scheduler = NewThreadScheduler.Default;
scheduler.Schedule(TimeSpan.FromSeconds(5), new Action<Action<TimeSpan>>(myRepeatingFunction));
}
private void myRepeatingFunction(Action<TimeSpan> action)
{
//process here
action(TimeSpan.FromSeconds(5)); // five second interval
}

Navigate to a xaml page from the isolated storage

I would like to know if anyone has done this before or tried to do it. I would like to navigate from my current page to a page generated and stored on the isolated storage.
Is that possible?
I already found a way of generating the xaml code and I'm working on generating the xaml.cs file but I can't seem to find a way of navigating to the newly created and existing file in the isolated storage.
I am using the "isostore" URI schema but it throws an exception in RootFrame_NavigationFailed:
[System.InvalidOperationException] = {"No XAML was found at the location '/isostore;/screenTest.xaml'."} . Thank you in advance.
Best regards,
Cipri
I think the closest you can get is:
Store usercontrols (rather than pages) in the isolated storage
Load them using XamlReader.Load
Inject the loaded UserControl inside of the current page
Drawbacks:
You can forget about the code-behind file as you can't compile it. You'll have to find another way to wire-up the events. I suggest taking a MVVM approach, by binding actions and using the same viewmodel for every usercontrol
You're not using the NavigationService, so you have to handle the navigation stuff (back button and application resuming after tombstoning)
public MainPage()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(MainPage_Loaded);
}
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
string UserName;
IsolatedStorageSettings.ApplicationSettings.TryGetValue<string>("UserName", out UserName);
if (!string.IsNullOrEmpty(UserName))
{
txtUserName.Text = UserName;
txtPassword.Focus();
}
else
txtUserName.Focus();
}
private void btnLogin_Click(object sender, RoutedEventArgs e)
{
IsolatedStorageSettings.ApplicationSettings["UserName"] = txtUserName.Text;
}

Phone 7 Bing map control - Add a pushpin when tap

I am using the latest Phone 7 RTM tools ( downloaded it today, October 7 2010).
I am trying to do a simple thing here:
when the user taps once on the map control, i want to put a pushpin there.
also, i want to keep the regular built-in behavior of the map control ( tap twice to zoom).
(If it's not possible to keep both behaviors , then maybe a long press on the map to put pushpin).
When trying figuring this out, i came across this documentation of the changes made to the control map for Phone7:
http://msdn.microsoft.com/en-us/library/ff955762.aspx
Then i saw the new class MapInputEventArgs, which has a ViewportPoint member.
When looking at code examples on the regular SilverLight map control i saw something like this:
private void OnMouseClick(object sender, MapMouseEventArgs e)
{
Point clickLocation = e.ViewportPoint;
Location location = x_Map.ViewportPointToLocation(clickLocation);
Pushpin pushpin = new Pushpin();
m_PushpinLayer.AddChild(pushpin, new Location(latitude, longitude));
}
But in Phone7 case, I can't find the appropriate event handler, and I could not find who uses MapInputEventArgs in the map control.
Searching it on google gets me only 1 result !!
So , where is the appropriate event for "Tap once", and how can i get a ViewportPoint after this event has been fired ?
Thanks in advance.
Just figured this out if you are still having problems.
The MouseLeftButtonUp and MouseLeftButtonDown Events have a GetPosition Method that will return the point your looking for
private void MapMain_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
Point p = e.GetPosition(this.MapMain);
GeoCoordinate geo = new GeoCoordinate();
geo = MapMain.ViewportPointToLocation(p);
MapMain.ZoomLevel = 17;
MapMain.Center = geo;
//---create a new pushpin---
Pushpin pin = new Pushpin();
//---set the location for the pushpin---
pin.Location = geo;
//---add the pushpin to the map---
MapMain.Children.Add(pin);
}
Unless I'm reading your question wrong, this seems to be exactly what you're looking for:
Silverlight - Add Pushpin to Bing Maps via C#
Okay, it's not pretty, but I have the same problem and I came up with a workaround of sorts that kicks in when you release your fingers from the screen. I instantiate a boolean:
bool noPin = false;
I then use this to determine whether zoom or pan is being done by the user (these fire between the MouseLeftButtonDown and MouseLeftButtonUp events). On the Up event, I then check whether the user was zooming or panning and, if not, place my pushpin.
private void mHome_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
noPin = false;
}
private void mHome_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (!noPin)
PlacePushPin();
}
private void mHome_MapPan(object sender, MapDragEventArgs e)
{
tbTemp.Text += "pan";
}
private void mHome_MapZoom(object sender, MapZoomEventArgs e)
{
tbTemp.Text += "zoom";
}
It's not beautiful but, well, it was the best I could manage.

Resources