为什么MainWindow不处理App.xaml.cs静态事件?

时间:2018-04-26 21:20:16

标签: c# wpf events squirrel.windows

我正在使用Squirrel进行安装和更新过程,这就是我在App.xaml.cs中处理自定义Squirrel事件的原因

App.xaml.cs

public static event EventHandler ApplicationUpdated;
protected virtual void OnApplicationUpdated()
{
   ApplicationUpdated?.Invoke(this, EventArgs.Empty);
}

protected override void OnStartup(StartupEventArgs e)
{
  base.OnStartup(e);
  try
  {
      using (var mgr = new UpdateManager(updateUrl))
      {
          SquirrelAwareApp.HandleEvents(
            onInitialInstall: OnInitialInstall,
            onAppUpdate: OnApplicationUpdate,
            onAppUninstall: OnApplicationUninstall,
            onFirstRun: OnFirstRun);
      }
  }
  catch (Exception ex)
  { }
}

private void OnApplicationUpdate(Version version)
{
  using (var manager = new UpdateManager(updateUrl))
  {
      manager.CreateShortcutsForExecutable(appFullName, ShortcutLocation.Desktop, true);
      manager.CreateShortcutsForExecutable(appFullName, ShortcutLocation.StartMenu, true);
      manager.CreateShortcutsForExecutable(appFullName, ShortcutLocation.AppRoot, true);

      manager.RemoveUninstallerRegistryEntry();
      manager.CreateUninstallerRegistryEntry();
  }

  System.Windows.Forms.MessageBox.Show("OnApplicationUpdated"); // this mbox is showing up normally
  OnApplicationUpdated(); // here is where I fire event that notifies (or at least should notify) MainWindow about update
}

更新进程和其他所有内容(OnInitiallInstall等)正常工作,但是当我尝试触发OnApplicationUpdated时(它可能有点令人困惑 - 这是我自己的静态事件)它不会触发(?)或MainWindow无法处理它是正确的,因为方法不会在输出中写入单行,也不会更改控件的任何属性

MainWindow.xaml.cs

public MainWindow()
{
   // some code above

   App.ApplicationUpdated += App_ApplicationUpdated;

   // some code under
}

private void App_ApplicationUpdated(object sender, EventArgs e)
{
    Console.WriteLine("ApplicationUpdated");
    Dispatcher.Invoke(() => 
    {
        updateLabel.Visibility = Visibility.Collapsed;
        updateButton.Visibility = Visibility.Collapsed;
        updateProgressRing.Visibility = Visibility.Collapsed;

        restartButton.Visibility = Visibility.Visible;
    });
}

你能告诉我为什么会这样,为什么MainWindow不处理这个事件?

我一直在使用像这样的静态事件很长一段时间,从来没有任何类似的问题

0 个答案:

没有答案