Xamarin Forms Unfocused Event Not Firing on User Control - xamarin.forms

I created a simple Xamarin Forms user control based on a ContentView. Since the ContentView is (ultimately) a VisualElement, I assumed the built-in Unfocused event would work as expected. However, while I can subscribe to my user control's Unfocused event, the event never fires. (Same with the FocusChangeRequested event, as it turns out.) The Xamarin documentation for the Xamarin.Forms.VisualElement.Unfocused event is cryptic:
This event is not bubbled through the Forms stack and is received directly from the native control.
How can I implement an Unfocused event within a Xamarin Forms user control? Is there something within the Xamarin Forms environment that I can intercept when the user control loses focus or, alternatively, when another control receives focus that will allow me to fire an Unfocused event?

Related

Multiple User Control On Same Page And reference issue with Javascript

I have one usercontrol which contain one textbox, few buttons and one calander.
I had place two instance of user control in one page as design time. It works fine all working from server side...mean server side events reference proper control at runtime.
Problem start after raising server side events from javascript. I succeed to fire proper event from javascript as per suggetion from other thread. Still I am not able to reference specific control at server side event. It always reference to control of last user control placed on page.
In my senerio I am firing calander's selected change event by buttons click event from javascript. The event fires as I want, but always it reference to last usercontrol's Calander.
Firing server side events from jabvascript is one step towards performance of user control.
Please suggest me.
On button click event call function of specific button and find calendar by id.

Why event bubbling and why not directly subscribe the click event?

I was going through an article on event bubbling in asp.net and came to know that although it is possible to subscribe to the click event of a user control's button from the containing page, "doing so would break some of the object oriented rules of encapsulation". A better idea is to publish an event in the user control to allow any interested parties to handle the event.
My question is that exactly how does a direct subscription to the button's click event from a containing page would break the object oriented rules of encapsulation?
Apologies if its a dumb question. :|
Thanks!
The Button is supposed to be encapsulated by the UserControl.
If the Page binds directly to events on the button, then the page is now dependent on the inner workings of the UserControl.
The Page should be consuming the UserControl, not the UserControl's button. If the author of the UserControl later wants to remove the button and use some fancy new method of firing its "Submit" event, your page could be broken because the button may no longer exist.
For that matter, if the owner of the UserControl decides in v1.1 to rename the button from btnSubmit to SubmissionButton, it could break your page, as well.
Better to consume the UserControl and let it be concerned with its own inner workings.
The idea is that the button of the control is an implementation detail of the UI of the control. If you republish the click event you could reimplement that button as an ImageButton, LinkButton, etc.
I think it's OK to attach an event handler at the page level to the button if the button is a permanent fixture of the UI. It saves a lot of event code, especially with a lot of buttons.

How can i raise activate event in Asp.net?Is there any possibility?

We have activate event in windows application like that is there any event in asp.net? How can i raise activate like event in Asp.net?Is there any possibility?
Have a look at DOM events and especially the focus event, section "Focusing and blurring the entire window".
You need to write a Javascript handler for the required events which either updates the page via Ajax or raises an ASP.Net event.

Asp.net pagelifecycle page_loadComplete

In my web application I load user controls in the page_loadComplete event. This works fine, however when a button is clicked in a user control, the click event is never fired. Does this has something to do with the page lifecycle? That button click (UI) events occur before the LoadComplete event?
You need to make sure the click event of the button is once again subscribed to, before event handlers fire. LoadComplete happens after control events. For a reference, the ASP.NET Page Life Cycle Overview gives a pretty nice summary.
Snippet:
...
Load
Control events
LoadComplete
PreRender
...
You also need to make sure that the controls that you dynamically load all end up in the same place, so viewstate and controlstate can be reapplied to the same hierarchy as before postback.
Basically, you need to load all dynamic controls on each postback.
Here's someone with the same problem, and solutions to some of them: ASP.NET dynamic controls
Actually what is happening to your situation is, when you click on the button, before event raising, LoadCoplete event fire first in the page lifecycle and same control is again created and here is your event is lost.
Event Handling in ASP.NET page happens after Validation and before Rendering phase. And Validation phase happens after Load.
LoadComplete happens after Control events and before RreRender event.

ASP.Net User Control - VS Event Wireup

So i've got a custom user control. I have an event (SelectionChanged) and I'm wanting to have who ever uses my control to do the following to hook up the event:
drag the control to the page
in designer mode, click on the control
view the controls event handlers (from the properties window)
find the SelectionChanged event
double click and let visual studio create the code behind function and the wire-up on the aspx page.
How do i get this done? I've got the control setup so that the user can manually type in the event wire-up and code behind event by hand, but i want Visual Studio to do this.
Currently, when the a developer has dropped my control on the page, they can click on it and see the properties but no events are available (the lightning bolt isn't even there).
My events are public. Here they are:
public delegate void SelectionChangedDelegate(object sender, SelectionChangedEventArgs e);
public event SelectionChangedDelegate SelectionChangedEvent;
I don't understand how the lightning bolt isn't there. Is your control inheriting from System.Web.UI.HtmlControls.HtmlControl or System.Web.UI.Control or another derive control?
If you are inheriting from one of these controls, you should see the inherited events in the properties window.
The fact the lightning bolt isn't there leads me to believe that you aren't inheriting from one of the the control classes.
EDIT:
Well, you're not going to like this. UserControl doesn't behave the same as a WebControl. And as such, the VS Editor doesn't wireup the events.
If the Event Wireup is critcal for you (If this is going to be a sold library) I would recommend that you rewrite the control as a WebControl. This will require you to add all the controls programatically in the CreateChildControls override method.
You might also be able to wrap your UserControl inside of a WebControl and bubble up all the events that way.
Best of luck!

Resources