Question

If you are implementing the Search Contract on a Windows Store App, then in your App.xaml.cs you override the OnSearchActivated method like this:

protected override void OnSearchActivated(SearchActivatedEventArgs args)
{
    (Window.Current.Content as Frame).Navigate(typeof(Contracts.Search), args.QueryText);
}

But if you are paying attention, then you can see that there is another override in the App class called OnActivated that has event arguments indicating a search activation, like this:

protected override void OnActivated(IActivatedEventArgs args)
{
    if (args.Kind == ActivationKind.Search)
    {
        (Window.Current.Content as Frame).Navigate(typeof(Contracts.Search), args.QueryText);
    }
}

When I implement one or the other the result seems to be the same. That begs the question: what is the difference between the two? Are they really the same?

Was it helpful?

Solution

Yes, they are the same.

The XAML team made a design decision to implement a generic OnActivated override as well as strongly typed overrides for the most common types of app activation. It is a best practice that, if there is a specific override, you use the specific override (like OnSearchActivated). But some advanced scenarios, like file or protocol activation, require OnActivated.

Note: in the Page pipeline, OnActivated fires first, the typed overrides follow. Since an app can only be activated by a single kind at a time, the order of execution doesn't matter.

Best of luck!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top