1

次の 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 を記述する可能性はありますか?

4

1 に答える 1

1
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();
    }
}

...そして、XAMLから削除します。

于 2013-08-22T20:44:49.917 に答える