How do I detect NSDatePicker ValueChanged in MonoMac? - xamarin

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.

Related

Xamarin Forms Map Viewable Area event handler

I have a Xamarin form map on my screen and I'm using PropertyChanged event to retrieve geolocation information from my server and display the proper pins on screen.
While coding the solution I noticed the PropertyChanged event is triggered multiple times (up to 10 times) with a single zoom or drag action on the map. This causes unnecessary calls to server which I want to avoid.
Ideally I want to make only one call to server when the final PropertyChanged event is called but I cant's find an easy solution to implement this.
At this point I've added a refresh button to my page that becomes enabled when a PropertyChanged event happens and I disable it after user uses the button.
Obviously this fixed the too many calls to server but made the solution manual.
I was wondering if there is a more elegant way to make the server call but do it automatically.
Thanks in advance.
I just test the PropertyChanged event on iOS side and it just triggered one time with a single zoom or drag action on the map.
While if it really triggered multiple times, you can use a timer to call the server when the final PropertyChanged event is called, for example:
public partial class MapPage : ContentPage
{
Timer aTimer;
public MapPage()
{
InitializeComponent();
customMap.PropertyChanged += CustomMap_PropertyChanged;
}
private void CustomMap_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (aTimer != null)
{
aTimer.Enabled = false;
aTimer.Stop();
aTimer.Close();
}
aTimer = new Timer();
aTimer.Interval = 1000;
aTimer.Enabled = true;
aTimer.Elapsed += ATimer_Elapsed;
aTimer.Start();
}
private void ATimer_Elapsed(object sender, ElapsedEventArgs e)
{
aTimer.Stop();
//do web request
Console.WriteLine(sender);
Console.WriteLine("CustomMap_PropertyChanged");
}
}
In the above code, I set the Interval = 1 second, that means in 1 second, whatever how many times PropertyChanged triggered, only the last call will trigger the ATimer_Elapsed function.
The Interval can be set to any value depending on your requirement.

Mono GTK# - PropertyNotifyEvent of window or widget

I want to be able to react if a property of a given window or widget has been changed and found the Gtk.Widget.PropertyNotifyEvent event. The (mono) documentation says that it will be fired if any property has been changed. So I've tried to make use of it but my event handler method is never invoked:
protected void DoSomething()
{
Gtk.Window __Window = new Gtk.Window(Gtk.WindowType.Toplevel);
Gtk.Button __Button = new Gtk.Button();
__Window.Add(__Button);
__Button.PropertyNotifyEvent += this.ButtonPropertyChangedEventHandler;
... // Show the window
__Button.Label = Mono.Unix.Catalog.GetString("This is a test button");
}
protected void ButtonPropertyChangedEventHandler(object o, PropertyNotifyEventArgs e)
{
// Handle the event
}
Do I miss something? Or did I understand something very basic wrong? Or is there another approach to achieve my goal?
Property change notifications need to be explicitly enabled by setting the GDK_PROPERTY_CHANGE_MASK flag, as explained here:
https://developer.gnome.org/gtk3/stable/GtkWidget.html#GtkWidget-property-notify-event
In Gtk# you just need to add the following line to your code:
__Button.Events |= Gdk.EventMask.PropertyChangeMask;
And the handler assigned to the PropertyNotifyEvent event will start to receive property change notifications. Gtk# enables some events implicitly but all others should be enabled from user code or from the stetic designer.

How to trigger datatemplate selector in Windows Phone?

I have a property and depending upon it's state (say A and B) I either show a usercontrol of animation or a image.
Now, if the property changes, I want to trigger the datatemplate selector again. On searching, I found that in WPF I could have used DataTemplate.Trigger but it's not available in WP.
So, my question is
Is their a way to trigger datatemplate selector so when property changes from state A to B, then appropriate usercontrol gets selected. If yes, then please give some example how to implement it.
Also, as there are only two states, if think I can use Converter to collapse the visibility. For basic if else situation, I will need to write two converters.( Can I somehow do it using one converter only?)
Here is the exact situation.
If state == A :
select userControl_A
else : select userControl_B
Also,
Will the animation usercontrol will effect the performance when it's in Collapsed state?
EDIT- Just realized, I can use parameter object to write just one converter.
You could implement a DataTemplateSelector like described here.
I use it and it works pretty well.
EDIT:
If you need to update the DataTemplate when the property changes, you should subscribe to the PropertyChanged event of the data object in the TemplateSelector and execute the SelectTemplate method again.
Here is the code sample:
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
City itemAux = item as City;
// Subscribe to the PropertyChanged event
itemAux.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(itemAux_PropertyChanged);
return GetTemplate(itemAux, container);
}
private DataTemplate GetTemplate(City itemAux, DependencyObject container)
{
if (itemAux != null)
{
if (itemAux.Country == "Brazil")
return BrazilTemplate;
if (itemAux.Country == "USA")
return UsaTemplate;
if (itemAux.Country == "England")
return EnglandTemplate;
}
return base.SelectTemplate(itemAux, container);
}
void itemAux_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
// A property has changed, we need to reevaluate the template
this.ContentTemplate = GetTemplate(sender as City, this);
}

map mode button in WP7

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)

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