0

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

应用程序.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 不能正确处理它,因为方法不会在输出中写入单行,也不会更改控件的任何属性

主窗口.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 不处理这个事件?

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

4

0 回答 0