I need an equivalent event for an iOS renderer:
public class MyMapRenderer : Xamarin.Forms.Maps.Android.MapRenderer
{
...
protected override void OnMapReady(GoogleMap map)
{
base.OnMapReady(map);
// need to do things here
}
...
}
Thanks
You can use MKMapViewDelegate_MapLoaded to handle your actions in iOS renderer:
public class CustomMapRenderer : MapRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<View> e)
{
base.OnElementChanged(e);
var nativeMap = Control as MKMapView;
nativeMap.MapLoaded += NativeMap_MapLoaded;
}
private void NativeMap_MapLoaded(object sender, EventArgs e)
{
Console.WriteLine("NativeMap_MapLoaded");
}
}
Related
If my entry field has focus and i click on hardware back button, the entry field looses focus but i am not able to navigate to previous page. I have to hit the back button again. I know this is the expected behaviour, but i need to navigate to the previous page on the first back button press itself. Need some help regarding this.
This behavior is by design.But you could use custom renderer and custom a EditText to achieve the effect.
1.create a CustomEntry :
public partial class CustomEntry:Entry
{
public static readonly BindableProperty BackPressProperty=BindableProperty.Create("BackPress", typeof(EventHandler), typeof(CustomEntry), null);
public event EventHandler BackPress;
public void OnBack()
{
EventHandler eventHandler = this.BackPress;
eventHandler?.Invoke((object)this, EventArgs.Empty);
}
}
2.create a custom EditText MyEditText in your Android project:
class MyEditText: FormsEditText
{
public MyEditText(Context context) : base(context)
{
}
public override bool OnKeyPreIme([GeneratedEnum] Keycode keyCode, KeyEvent e)
{
if (keyCode == Keycode.Back)
{
if (listener != null)
{
listener.onKeyBack();
return true;
}
}
return base.OnKeyPreIme(keyCode, e);
}
private OnEditTextKeyBackListener listener;
public void setOnEditTextKeyBackListener(OnEditTextKeyBackListener listener)
{
this.listener = listener;
}
public interface OnEditTextKeyBackListener
{
void onKeyBack();
}
}
3.custom EntryRenderer MyEntryRenderer:
[assembly: ExportRenderer(typeof(CustomEntry), typeof(MyEntryRenderer))]
namespace EntryCa.Droid
{
class MyEntryRenderer:EntryRenderer,OnEditTextKeyBackListener
{
private Context context;
private EditText editText;
public MyEntryRenderer(Context context) : base(context)
{
this.context = context;
}
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
MyEditText editText = new MyEditText(context);
editText.setOnEditTextKeyBackListener(this);
editText.SetSingleLine(true); //this and below line to set done key
editText.ImeOptions = ImeAction.Done;
SetNativeControl(editText);
}
public void onKeyBack()
{
hideSoftInput();
((CustomEntry)Element).OnBack();
}
InputMethodManager mInputMethodManager = (InputMethodManager)Android.App.Application.Context.GetSystemService(Context.InputMethodService);
public void hideSoftInput()
{
if (mInputMethodManager != null)
{
mInputMethodManager.HideSoftInputFromWindow(WindowToken, HideSoftInputFlags.NotAlways);
}
}
}
}
4.use in your page.xaml:
<local:CustomEntry HorizontalOptions="StartAndExpand" WidthRequest="200" BackPress="CustomEntry_BackPress"></local:CustomEntry>
in your page.xaml.cs:
private void CustomEntry_BackPress(object sender, EventArgs e)
{
Navigation.PopAsync();
}
When I run my method in global.asax it doesn't run and when I use IHttp module it is working. Please give any advice.
Maybe it is caused of :
context.PostRequestHandlerExecute += new EventHandler(Application_EndRequest);
Is it possible to call it without Module?
Code Example:
Method that I run:
public static void EndSession()
{
HttpContext context = HttpContext.Current;
if (context.Session != null)
{
ISession session = context.Session["Session"] as ISession;
if (context.Session["Session"] != null)
{
if (!session.Transaction.IsActive)
OpenTransaction(session);
session.Flush();
CommitTransaction(session);
session.Close();
context.Session["Session"] = null;
}
}
}
Global:
private void Application_EndRequest(object sender, EventArgs e)
{
NhSessionHelper.EndSession();
}
IHTTPMODULE:
namespace MME.DAL.SesionManager
{
internal class SessionRequest : IHttpModule
{
#region Public Methods
public void Dispose()
{
}
public void Init(HttpApplication context)
{
context.PostRequestHandlerExecute += new EventHandler(Application_EndRequest);
}
#endregion
#region Private Methods
private void Application_EndRequest(object sender, EventArgs e)
{
NhSessionHelper.EndSession();
}
#endregion
}
}
Ok I understand now PostRequestHandlerExecute fires page finishes execution so the name of
private void Application_EndRequest(object sender, EventArgs e)
was little confusing and that is why there was a problem.
I got this code which extends the tablerow to make them clickable.
namespace ClickableTableRow
{
public class ClickableTableRow : TableRow, IPostBackEventHandler
{
public ClickableTableRow()
: base()
{ }
private EventHandler _click;
public event EventHandler Click
{
add { _click += value; }
remove { _click -= value; }
}
protected virtual void FireClickEvent()
{
if (_click != null)
_click(this, new EventArgs());
}
protected override void AddAttributesToRender(HtmlTextWriter writer)
{
writer.AddAttribute(HtmlTextWriterAttribute.Onclick,
Page.ClientScript.GetPostBackEventReference(this, String.Empty));
base.RenderAttributes(writer);
}
public void RaisePostBackEvent(string eventArgument)
{
FireClickEvent();
}
}
The code above works well if I create the table manually, but when i create the table in the code behind it seems to be firing postbacks but no resuts returned. is there any thing wrong i might doing. my codebehind looks like:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void row1_Click(object sender, EventArgs e)
{
Messagebox.Text = "message";
}
ClickableTableRow.ClickableTableRow row1;
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
Table Table1 = new Table();
TableCell cell1 = new TableCell();
row1= new ClickableTableRow.ClickableTableRow();
row1.Attributes.Add("onmouseover", "this.style.cursor='hand'");
cell1 = new TableCell();
cell1.Text = "llllll";
row1.Cells.Add(cell1);
Table1.Rows.Add(row1);
row1.Click += new EventHandler(row1_Click);
this.pnl2.Controls.Add(Table1);
}
}
Try moving this code to Init or PreInit. This is the typical part in the lifecycle where dynamic controls should be added.
We can programatically add HttpModules using DynamicModuleUtility.RegisterModule(typeof (SomeHttpModule)) - is there a way to remove them?
Instantiate the HTTP Module in Init method in Global.asax
Call Module.Init() as described here
In Init method of your module, hook required eventhandlers.
Override the Dispose method in the handler and unhook the
eventhandlers.
Expose the instance as public property on global.asax so that you
can call Dispose when you want to unregister the module
// Global.asax
public IHttpModule MyModuleInstance { get; private set; }
public override void Init()
{
base.Init();
MyModuleInstance = new MyModule();
MyModuleInstance.Init(this);
}
// MyModule.cs
public void Dispose()
{
_context.BeginRequest -= context_BeginRequest;
}
public void Init(HttpApplication context)
{
_context = context;
context.BeginRequest += context_BeginRequest;
}
private void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication app = sender as HttpApplication;
app.Context.Response.Write("Hello from OnBeginRequest in custom module.<br>");
}
// TO unregister
protected void Button1_Click(object sender, EventArgs e)
{
this.ApplicationInstance.MyModuleInstance.Dispose();
}
When is the earliest point in which I can access HttpContext.User?
You could use the AuthenticateRequest event of the HttpApplication. Here is some sample code:
public class MyHttpModule : IHttpModule
{
public void Init(HttpApplication context)
{
context.AuthenticateRequest += context_AuthenticateRequest;
}
void context_AuthenticateRequest(object sender, EventArgs e)
{
var application = (HttpApplication) sender;
var name = application.Context.User.Identity.Name;
}
public void Dispose()
{
}
}