Overriding protected internal methods - c#

I'm rewriting a web application to ASP.Net 4.0 and have included a Menu control (bound to a SiteMap file, naturally). While I'm liking the new RenderingMode property, I'm hating the fact that it automagically includes some javascript at the bottom of your page to animate the menu.
My preference would be for greater control using jQuery, but switching that off is proving very difficult. With some help from a very heavy hitter, I've been walked through to the point where I've discovered that the ASP.Net 4.0 Menu control has an internal OnPreRender method:
internal void OnPreRender(EventArgs e, bool registerScript);
How do I override this method so that I can call:
base.OnPreRender(e, false);
When trying at the moment, I'm getting an error from Visual Studio saying that "No overload for method 'OnPreRender' takes 2 arguments".

You should be able to refer to protected internal methods as they are protected or internal. If the method is just internal you might be out of luck.
Looking at ASP.NET 4 Menu control, it only has one OnPreRender override:
protected internal override void OnPreRender(EventArgs e);
So you should be able to override it. Not sure where OnPreRender(EventArgs e, bool registerScript) comes from (perhaps it's internal), but the fact that the base class doesn't it(or it's inaccessible) is your problem.

Not a direct answer, but its pretty trivial to dump the contents of any IHeirarchicalDataSource, such as a sitemap file, to a <ul> which any number of jquery menu products can chew and make purdy.

Related

Caliburn.Micro Screen and Conductor Lifecycle documentation

Does anyone know a source for an overview of the Caliburn.Micro Screen/Conductor Lifecycle? For example a sequence diagram / flow chart that describes their call order/dependencies and the conditions when they get called or not?
Until now I have primarily uses OnViewLoaded but I want to know which are called a second time (when shown again) etc. I didn't found a good documentation about the Screen Lifecycle yet.
And yes, I know it is Open Source and I can read the source code or debug that (what I'm doing at the moment)... just thought that this requirement is somewhat basic to work with Caliburn.Micro and there must be something already done and I don't need to create that overview on my own. Maybe the answer might help someone else, too. ;-)
For example, when derriving from Conductor.Collection.OneActive
there are the following (and even more) methods that seem to play a role in the lifecycle and can be overloaded:
protected virtual void OnInitialize()
protected virtual void OnActivate()
protected virtual void OnActivationProcessed(IScreen item, bool success)
protected virtual void OnDeactivate(bool close)
protected virtual void OnViewAttached(object view, object context)
protected virtual void OnViewLoaded(object view)
protected virtual void OnViewReady(object view)
What I have seen so far this seems to be the order (app startup to exit):
OnViewAttached
OnInitialize
OnActivate
OnViewReady
OnViewLoaded
OnActivationProcessed
OnDeactivate
But what are the bullet points for each method? E.g. when is the datacontext set, the style template applied to the view and ready to be shown? When is the view shown? (difference between ViewReady and ViewLoaded?)
Not a full answer but it is a start, from the documentation of this project, which is worth reading, you can find bullet points for some of those events:
OnInitialize – Override this method to add logic which should execute only the first time that the screen is activated. After initialization is complete, IsInitialized will be true.
OnActivate – Override this method to add logic which should execute every time the screen is activated. After activation is complete, IsActive will be true.
OnDeactivate – Override this method to add custom logic which should be executed whenever the screen is deactivated or closed. The bool property will indicated if the deactivation is actually a close. After deactivation is complete, IsActive will be false.
OnViewLoaded – Since Screen implements IViewAware, it takes this as an opportunity to let you know when your view’s Loaded event is fired. Use this if you are following a SupervisingController or PassiveView style and you need to work with the view. This is also a place to put view model logic which may be dependent on the presence of a view even though you may not be working with the view directly.
There is also a good explanation on the parameters being sent to the methods, and many other topics for the layers of the screens and their life cycle.

what the equivalent to OnResume on Xamarin.forms

Right now, I am using Xamarin.Forms PLC project.
I have a label[x] on Page[x], then I will press button and go to Page[xx] ,then I will go back to Page[x] but I need to update Label[x] Text upon some Choices selected on Page[xx].
Which Event should i use to update Label.Text?
I was overriding OnResuem()Function on Xamarin.android, but it is not working on Xamarin.forms, I have no idea which is the best solution.
Some quick solution for this is:
-overriding the OnAppearing() method of the page and change the label.Text property once you change it on the other page
-Change the property to be public and change it on the other page
-Send the property to the next page as a parameter
but what you should do! is bind your property to a ViewModel and use OnPropertyChange() (Xamarin.Forms way and MVVM architecture) Events: a couple of tutorials how to understand this better:
https://blog.xamarin.com/advanced-data-binding-for-ios-android-and-windows/
https://developer.xamarin.com/guides/xamarin-forms/user-interface/xaml-basics/data_bindings_to_mvvm/
https://developer.xamarin.com/guides/xamarin-forms/user-interface/xaml-basics/data_binding_basics/
I'm not sure how youre code works because you havent said. So I'm not sure how Page[x] knows about Page[xx] but it sounds to me like you want to use the OnAppearing() override.
Which from the Xamarin.Forms Page API documentation states:
When overridden, allows application developers to
customize behavior immediately prior to the Page becoming visible.
You could do this by adding the following to your Page[x].xaml.cs file
protected override void OnAppearing()
{
//Your code here
}

"Element is already the child of another element."

At first, this exception doesn't really make sense to me. Why shouldn't i be able to duplicate this object multiple times? but thats not the point:
i use a List. Whenever i navigate to a site, it should do this:
(App.Current as App).recent.ForEach(x => container.Children.Add(x));
(container = another StackPanel)
the first time, it works. afterwards, i get the exception displayed in the questiontitle. i already tried using a listbox, but i just got a ArgumentException. I think these exceptions have the same source, but i don't know what i'm doing wrong. please help
thanks
The error is quite clear: A WPF/SL Control can only belong to 1 Parent control at a time.
So you'll either have to remove the Controls from their Parent when you are moving away from a Page or you'll have to Create (possibly Clone) new Controls in this ForEach.
When you navigate to a page, the old instance is not removed instantly, so when you add your controls to the new page, they may still be used in the old page if it's not destroyed, causing the exception.
Try overriding the OnNavigatedFrom(NavigationEventArgs e) and OnNavigatingFrom(NavigatingCancelEventArgs e) methods that are invoked when you leave a page so that you empty your StackPanel.
For example, you could do :
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
base.OnNavigatedFrom(e);
container.Children.Clear();
}
You have to remove the container's children when you navigate from the page. But not if the navigation is due to a suspend. To avoid the children clear when the navigation is due to a suspend it is better to override OnNavigatingFrom instead of OnNavigatedFrom
protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
container.Children.Clear();
base.OnNavigatingFrom(e);
}

C# Enhanced Listbox - remembers additional attributes after postback

I'm in a situation where I need a listbox to remember after postback the attributes I've added to a number of the listitems in it. I found this solution online which appears to solve the issue but am not sure how to implement it.
List box solution
He says he wrote a class that inherits from Listbox which is fine, I've done that and have called it EnhancedListBox but how do I then apply that to the Listbox I'm using on the page?
I can't just substitute
<asp:ListBox >
with
<asp:EnhancedListBox>
but how else do I let the page know I want to use my inherited code?
Thanks.
SaveViewState and LoadViewState are virutal methods that you can override. What you want to try is creating "your own" ListBox:
class EnhancedListBox : ListBox
{
protected override object SaveViewState()
{
// code here from the tutorial
}
protected override void LoadViewState(object savedState)
{
// code here from the tutorial
}
}
This is also called "Creating a custom control", it's very common to do it this way and it gives you great flexibility.
you need to create a custom control for this
check this out http://msdn.microsoft.com/en-us/library/ms366537.aspx for the startup.

Manually adding page event handlers in ASP.Net C#

When I've built applications in the past I've used AutoEventWireup to handle the page events for me. From what I've read this incurs a significant performance cost and I'd like to do it manually in my current application.
What is the correct place to set up the event handlers?
My initial thought was to just set up a constructor in my code behind file and do it there but I'm assuming that the auto generated portion of the partial class already contains a constructor that I'd be overriding.
I'm sorry to ask here on such a simple question. It seems like this should be easily searchable but I'm just not finding the answer I need. Thanks in advance for the help.
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
Load += new EventHandler(Page_Load);
}
For controls it's OnInit, since they have no OnPreInit. To be honest I've used OnInit for pages as well in the past :)
Of course, you could just do the above for all the events you need for your page, and define no event handlers whatsoever.

Categories

Resources