次の XAML (App.xaml) を使用する WPF アプリケーションを考えてみましょう。
<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>
および 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();
}
}
このコードを呼び出すまで、notificationIcon リソースがインスタンス化されていないように見えます。
FindResource("notificationIcon");
OnStartup() メソッドで。この FindResource() 呼び出しが不要で、そのオブジェクトが自動的にインスタンス化されるような方法で XAML を記述する可能性はありますか?