Use override of some methods of a contentPage in a ContentView - xamarin.forms

I need to use ContentView instead of a ContentPage, but before I had two overrides of the OnAppearing() and OnBackButtonPressed() methods. Is there any way I can use them anyway?

Related

Prism Inheriting from ViewModelBase vs BindableBase

When I create a new Xamarin.Forms application using the Prism template pack the project gets created with a MainPageViewModel that inherits from ViewModelBase
Later I create and an additional View and ViewModel say for ChatPage. This ChatPageViewModelinherits from BindableBasenot ViewModelBase as generated by the Add New dialog.
I'd like to use ViewModelBase in all my View(Models) ViewModelBase inherits from ViewModelBase : BindableBase, INavigationAware, IDestructible
I try and change the new ChatPageViewModel : BindableBase to ChatPageViewModel : ViewModelBase but the constructor gets a red squiggly error;
Error CS7036 There is no argument given that corresponds to the required formal parameter 'navigationService' of 'ViewModelBase.ViewModelBase(INavigationService)'
I see in App.xaml.cs that containerRegistry.RegisterForNavigation<NavigationPage>(); is implemented differently than the other pages containerRegistry.RegisterForNavigation<ChatPage, ChatPageViewModel>();
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterForNavigation<NavigationPage>();
containerRegistry.RegisterForNavigation<MainPage, MainPageViewModel>();
containerRegistry.RegisterForNavigation<SettingsPage, SettingsPageViewModel>();
containerRegistry.RegisterForNavigation<ChatPage, ChatPageViewModel>();
containerRegistry.RegisterSingleton<IXpdSettings, XpdSettings>();
containerRegistry.RegisterSingleton<IMqttDataService, MqttDataService>();
}
Is there a way I can inherit from ViewModelBase? Could / should it be implemented in the XamarinForms Prism templating?
The answer is contained in my question. See the syntax that the MainPageViewModel (that was created by the initial project creation dialog) uses - where MainPageViewModel inherits from ViewModelBase unlike subsequent pages created with Add New dialog that inherit from BindableBase. For instance ChatPageViewModel inheriting from ViewModelBase rather than BindableBase.
public class ChatPageViewModel : ViewModelBase
{
private IXpdSettings _xpdsettings;
public ChatPageViewModel(INavigationService navigationService, IXpdSettings xpdSettings)
: base(navigationService)
{
Title = "Mqtt Chat";
_xpdsettings = xpdSettings;
}
}

Why some public methods of EditText can't be called from Xamarin code?

I make a custom Renderer class for Xamarin.Forms.Entry to customize an Entry then i realize that some public method of EditText control of Android can't be called from my Xamarin code. For example: The method getCompoundDrawablePadding() is a public method of TextView, which is a parent class of EditText.
Is there anyway to call any public methods of EditText from Xamarin code?
Use the public property CompoundDrawablePadding. Most Android get/set methods are exposed in Xamarin as C# properties with getter/setters.

Is there any undesirable effect doing ModelState.Clear() before action executing?

If we are using ajax call to return partial view, then response from the action method may return cached output from partial view. The issue has been discussed on several SO post here, here, here
To resolve issue i have to do ModelState.Clear() in action method.
I have several action methods that returns partial view using Ajax call. So i thought instead of doing ModelState.Clear() in each method i can create custom ActionFilterAttribute like below
public class NoCacheAttribute : ActionFilterAttribute
{
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
filterContext.Controller.ViewData.ModelState.Clear();
base.OnResultExecuting(filterContext);
}
}
and then register it with global filters
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new NoCacheAttribute());
}
This seems to be working fine.
However I wanted to know if there is there any undesirable effect doing ModelState.Clear() before action executing? If not, then why that is not a default behavior in asp.net mvc

Can web forms and generic handlers (ashx files) share a common ancestor class (or base page)?

If one has an ASP.net web site whose web forms all inherit from a common base page--which checks things like authentication and redirects when a session has expired, etc--is there a way to use this base class in a ashx handler? I started going down that road by inheriting the base page class in the handler and realized this might not be a recommended practice. Any thoughts on how to avoid code duplication here?
Can this
public class FooHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
become this
public class FooHandler : BasePageClass, IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
I get the following warning, which is what made me reconsider what I was doing:
FooHandler.ProcessRequest(System.Web.HttpContext)
hides inherited member
System.Web.UI.Page.ProcessRequest(System.Web.HttpContext).
To override that implementation, add
the override keyword. Otherwise add
the new keyword.
If you set the parent class ProcessRequest as virtual you can override it in the child class and call the base class method from inside the overridden method.
public abstract class ParentHandler : IHttpHandler, IRequiresSessionState
{
protected WebConnectHandler Context;
public virtual void ProcessRequest(HttpContext context)
{
Context = new WebConnectHandler(context);
}
//...
}
//...
public class ChildHandler : ParentHandler
{
public override void ProcessRequest(HttpContext context)
{
base.ProcessRequest(context);
//the rest of your code
}
}
//...
You are now discovering why the OO guru types always vote for composition over inheritance. Anyhow, doing exactly what you want to do is pretty tricky as, while a System.Web.UI.Page implements IHttpHandler (if I recall correctly) there is lots of internal processing you can't defeat to get back to your own handler.
The easiest fix would be to try and move as many of those functions off the monster base page class into their own classes--or better yet IHttpModules where it makes sense to handle stuff like session expiration--in order to decouple things.

asp.net ItemTemplate : ITemplate

public class TheItemTemplate : ITemplate
{
//....
public void InstantiateIn(Control container)
{
//...
}
}
Who calls this method? And when is it called?
Typically, this method is called when the control tree is created, so in the CreateChildControls() method. This CreateChildControls method is part of the Control inheritance hierarchy and is typically overriden by subclassed controls.
So, the control to which the template belongs should call InstantiateIn() in CreateChildControls.

Resources