繁体   English   中英

WPF ContentRendered事件未触发?

[英]WPF ContentRendered event not firing?

我确定我忽略了一些简单的事情,但是WPF通常不是我要从事的工作,因此我对此有点挠头。

我正在尝试为要在后台运行的应用程序显示“启动屏幕”。 它基本上是Windows操作系统的进程包装。

我有这样定义的WPF Window

public partial class MainWindow : Window
{
    private Timer _timer;

    public MainWindow()
    {
        InitializeComponent();
        ContentRendered += MainWindow_ContentRendered;
    }

    private void MainWindow_ContentRendered(object sender, EventArgs e)
    {
        _timer = new Timer(5000);
        _timer.Elapsed += TimerOnElapsed;
        _timer.Enabled = true;
        _timer.Start();
    }

    private void TimerOnElapsed(object sender, ElapsedEventArgs e)
    {
        Dispatcher.Invoke(() =>
        {
            Hide();
            Close();
        });

    }

    ~MainWindow()
    {
        _timer.Dispose();
    }
}

public partial class MainWindow : System.Windows.Window, System.Windows.Markup.IComponentConnector {

    private bool _contentLoaded;

    /// <summary>
    /// InitializeComponent
    /// </summary>
    [System.Diagnostics.DebuggerNonUserCodeAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]
    public void InitializeComponent() {
        if (_contentLoaded) {
            return;
        }
        _contentLoaded = true;
        System.Uri resourceLocater = new System.Uri("/MySolution.WindowsWrapper;component/mainwindow.xaml", System.UriKind.Relative);

        #line 1 "..\..\MainWindow.xaml"
        System.Windows.Application.LoadComponent(this, resourceLocater);

        #line default
        #line hidden
    }

    [System.Diagnostics.DebuggerNonUserCodeAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]
    [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
    [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Design", "CA1033:InterfaceMethodsShouldBeCallableByChildTypes")]
    [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")]
    [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily")]
    void System.Windows.Markup.IComponentConnector.Connect(int connectionId, object target) {
        this._contentLoaded = true;
    }
}

XAML看起来像:

<Window x:Class="MySolution.WindowsWrapper.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:MySolution.WindowsWrapper"
        mc:Ignorable="d"
        Title="Splash" Height="100" Width="327" WindowStyle="None" 
        HorizontalAlignment="Center" VerticalAlignment="Center" 
        AllowsTransparency="True" WindowStartupLocation="CenterScreen" ShowInTaskbar="False" Topmost="True">
    <Window.Background>
        <ImageBrush ImageSource="my-logo-large.png"/>
    </Window.Background>
    <Grid VerticalAlignment="Center" HorizontalAlignment="Center">

    </Grid>
</Window>

...在我的Program.cs文件中,我有类似以下内容:

    [STAThread]
    public static void Main(string[] args)
    {
        var splashScreen = new MainWindow();
        splashScreen.Show();

        SetupSystemTrayIcon();

        var showConsole = args.Any(x => x.ToLowerInvariant().Contains("showconsole"));
        StartService(showConsole);
        WaitToDie();
    }

初始屏幕按预期显示...但是计时器从未初始化,也从未触发,因此它永远不会消失。

根据我的阅读,应该在显示窗口时触发ContentRendered事件……但这似乎没有发生。 我做错了什么?

我建议您删除自定义的Main方法并将代码移至App.xaml.cs类的OnStartup方法:

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

        SetupSystemTrayIcon();

        var showConsole = args.Any(x => x.ToLowerInvariant().Contains("showconsole"));
        StartService(showConsole);
    }
}

确保在App.xaml中将StartupUri属性设置为MainWindow.xaml

<Application x:Class="WpfApp2.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfApp2"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
    </Application.Resources>
</Application>

然后,您应该获得一个调度程序和一个激活的窗口。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM