force instantiation of xaml resource

Let's consider a WPF application with the following XAML (App.xaml):

<Application
  x:Class="My.App"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:my="clr-namespace:My.Namespace;assembly=My.Assembly"
  ShutdownMode="OnExplicitShutdown"
  >
  <Application.Resources>
    <my:NotificationIcon x:Key="notificationIcon" ApplicationExit="notificationIcon_ApplicationExit" />
  </Application.Resources>
</Application>

And App.xaml.cs:

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        FindResource("notificationIcon");
    }

    void notificationIcon_ApplicationExit(object sender, EventArgs eventArgs)
    {
        Application.Current.Shutdown();
    }
}

It looks as if the notificationIcon resource is not being instantiated until I call this code:

FindResource("notificationIcon");

in OnStartup() method. Is there any possibility to write XAML in such a way that this FindResource() call is not needed and that object is instantiated automatically?

Answers


public partial class App : Application
{
    public NotificationIcon NotifyIcon {get;set;}

    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);

        NotifyIcon = new NotificationIcon();
        NotifyIcon.ApplicationExit += notificationIcon_ApplicationExit;
    }

    void notificationIcon_ApplicationExit(object sender, EventArgs eventArgs)
    {
        Application.Current.Shutdown();
    }
}

... And remove it from XAML.


Need Your Help

Excel VBA - count the duplicates

vba excel-vba

I have two arrays which contains string values in them. Let us consider the example as below.

Groovy/Grails SimpleTemplateEngine Freezes

templates grails groovy

I'm using Grails to send a large number of HTML emails. I use the SimpleTemplateEngine to create my email bodies in this fashion:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.