force sender to a type - windows-phone-7

I got the event sent from a button
private void myEvent(object sender, RoutedEventArgs e)
send is a button, is it possible to get the button handler to access the parameter(such as tag) of the button

You'd need to cast the sender parameter to it's native type in order for it to be treated as that type and therefore be able to access those properties
Something like this should be sufficient
var button = (Button)sender
You can then access the Button instance referenced by sender as a Button object

cast the sender to a Button:
var button = sender as Button;

Related

Xamarin DataPicker how to handle done button click event

I added to my DataPicker unfocused event. And in this event I would like to check if Done button was clicked.
private void DatePicker_Unfocused(object sender, FocusEventArgs e)
{
}
Or other option.
Is it possible to add listener to button Done, and if was clicked then fired some method?
How can I do it?
Thank you.
Every UI control which receives input always has some event handlers which you can listen to.
In your case the DatePicker has an Event Handler which fires when the Date property changes.
Here is the documentation:
DatePicker Event Handler - Xamarin Documentation
The implementation of this handler is simple:
DatePicker datePicker;
DateTime newDate;
DateTime oldDate;
public YourPage()
{
datePicker.DateSelected += Date_DateSelected;
}
void Date_DateSelected(object sender, DateChangedEventArgs e)
{
newDate = e.NewDate;
oldDate = e.OldDate;
}
Always read the documentation first, as that's the best way to learn how to use a specific control.

How to show the appointment page from a form region in outlook

I have a very simple outlook form region. It is configured as a separate item, and, it is set to appear whenever a we try to compose a new appointment item. (Meeting request).
Once I click the button above, it should populate the sender and go back to the main appointment page. The code to do that is:
private void button1_Click(object sender, EventArgs e)
{
item.RequiredAttendees = "John.Doe#contoso.com";
var exp = item.Application.ActiveInspector();
if (exp == null) Debug.Print("NULL");
else exp.ShowFormPage("Appointment");
}
But this doesn't do anything. What is the correct way of doing this?
Use the SetCurrentFormPage method of the Inspector class to display the specified form page or form region in the inspector.
As a workaround you may try to call the Appointment button programmatically. Use the ExecuteMso method of the CommandBars class to execute the control identified by the idMso parameter. See Office 2013 Help Files: Office Fluent User Interface Control Identifiers for the actual idMso values.

Is it possible to detect the DOM element tapped in a WP7 WebBrowser control?

Is it possible to detect the DOM elements under a spot the user taps on a website using the WP7 WebBrowser control? I would like to let the user identify certain sections of the rendered page.
Yes. This code works pretty well for me:
private void button1_Click(object sender, RoutedEventArgs e)
{
// first define a new function which serves as click handler
webBrowser1.InvokeScript("eval", "this.newfunc_eventHandler = function(e) { window.external.notify(e.srcElement.tagName); }");
// attach function to body
webBrowser1.InvokeScript("eval", "document.body.addEventListener('click', newfunc_eventHandler, false);");
}
private void webBrowser1_ScriptNotify(object sender, NotifyEventArgs e)
{
// this should be called every time you tap an element; the value should be its tag name
Debug.WriteLine(e.Value);
}
It goes along Justin's answer (who beat me while coding). It first defines a click handler which then is attached to the page's body (all via InvokeScript). Every time you tap an element ScriptNotify should be called on the WebBrowser reporting the tapped element's tag name.
Make sure you have set IsScriptEnabled true for your browser control.
I'm guessing you'd have to build something off of the ScriptNotify Event.
At that point, you would register a JavaScript Event handler as usual but that event handler would, in turn, call window.external.notify("someMessageToHandle");. You would then have to route the calls appropriately.
If you don't have the ability to add the JavaScript event handlers directly on the page, you could instead add them using WebBrowserControl.InvokeScript.

RoutedEvents in WP7 Custom Control

In normal version of silverlight you can create an event handler by registering it by EventManager. Windows Phone 7 hasn't got that class.
My question is: How to create an event, which will be handled by the parent panels.
My scenario: I've created a custom class with some textbox in it. Foreach I've added my custom behavior, which raises when textblock is clicked. Behavior works like: "When this Textblock in custom control is clicked, please raise a custom event with my custom args (i want to pass them to the Custom Control itself (for example to specify to which VisualState change it)."
Can you help me how to handle my problem?
Could you provide sample code of what you are trying to do? it seems you want to create an event for when the TextBlock is clicked.
Add an event handler to the textblock:
public Event EventHandler<RoutedEventsArgs> TextClicked;
// Fire the event
private void OnTextClicked(object sender, RoutedEventArgs e)
{
if (TextClicked != null)
{
TextClicked(sender, e);
}
}
TextBlock.Click =+ OnTextBlockClicked;
private void OnTextBlockClicked(object sender, RoutedEventArgs e)
{
// Raise event
OnTextClicked(sender, e);
}
Something along those lines I think.

how to get parent name of a context menu item?

I'm trying to get the parent name of a context menu item.
So I tried something like this on menuItem_click :
Button clikance = (Button)sender;
string ladyGaga = Convert.ToString(clikance.Content);
But it didn't work (invalid cast exception). thx for any help
i have use a different approach for getting the sender button of my context menu. i have made an event on the "hold_click"
where i have get back the content of the button in a public string
private void GestureListener_DoubleTap(object sender, GestureEventArgs e)
{
Button clikance = (Button)sender;
ButtonEnvoyeur = Convert.ToString(clikance.Content);
}
If you look in the debugger at the point where the exception is raised, you'll see that sender isn't a Button, so trying to do an explicit cast to Button will obviously throw an InvalidCastException.
You can use the VisualTreeHelper to walk up the tree from your actual sender to the Button element:
VisualTreeHelper.GetParent((sender as DependencyObject));
UPDATE: In your instance sender is the MenuItem in the ContextMenu. You can get to the parent ContextMenu from the MenuItem by using the VisualTreeHelper, but unfortunately, ContextMenu does not expose any public members that enable you to access the owner; the Owner property is internal. You could get the source code for the Toolkit and expose the Owner property as publi instead, or use a completely different approach.
Have you thought of using an MVVM framework (such as MVVM Light) to wire up commands to these context menu items? Your current approach is very fragile and will break as soon as you change the visual tree. If you used commands, you could pass any additional information that you need for processing via the command parameter.
Use the Tag property of the MenuItem to retrieve your Button :
// Object creation
Button myButtonWithContextMenu = new Button();
ContextMenu contextMenu = new ContextMenu();
MenuItem aMenuItem = new MenuItem
{
Header = "some action",
Tag = myButtonWithContextMenu, // tag contains the button
};
// Events handler
aMenuItem.Click += new RoutedEventHandler(itemClick);
private void itemClick(object sender, RoutedEventArgs e)
{
// Sender is the MenuItem
MenuItem menuItem = sender as MenuItem;
// Retrieve button from tag
Button myButtonWithContextMenu = menuItem.Tag as Button;
(...)
}
Alex.

Resources