事件被解雇但文字没有改变

时间:2016-02-07 10:34:56

标签: c# wpf

我想要做的是更改Splash窗口标签内容。 应用程序代码如下

public partial class App : Application
{
    private const int splashMinTime = 2000;

    protected override void OnStartup(StartupEventArgs e)
    {
        Splash splashScr = new Splash();
        splashScr.Show();

        splashScr.SplashInfo = "Ładowanie ....";

        Stopwatch splashTimer = new Stopwatch();
        splashTimer.Start();
        base.OnStartup(e);
        MainWindow main = new MainWindow();

        splashTimer.Stop();

        int splashRemainingTime = splashMinTime - (int)splashTimer.ElapsedMilliseconds;
        if (splashRemainingTime > 0)
            Thread.Sleep(splashRemainingTime);

        splashScr.Close();
    }
}

飞溅

public partial class Splash : Window, INotifyPropertyChanged
{
    public string _SplashInfo;
    public event PropertyChangedEventHandler PropertyChanged;

    public Splash()
    {
        this.DataContext = this;
        InitializeComponent();
    }

    public string SplashInfo
    {
        get { return _SplashInfo; }
        set { _SplashInfo = value; OnPropertyChanged("SplashInfo"); }
    }

    private void OnPropertyChanged(string PropertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(PropertyName));
        }
    }
}

我的Splash.xaml

<Grid>
    <Image Source="Img\Splash.jpg" Stretch="None"/>
    <Label x:Name="lblSplashInfo" Content="{Binding SplashInfo}" HorizontalAlignment="Left" Margin="10,204,0,0" VerticalAlignment="Top" Width="220"/>
</Grid>

启动了Splash PropertyChangedEventHandler,但我没有看到启动窗口标签中的更改。

3 个答案:

答案 0 :(得分:0)

也许我错过了一些东西,但对我来说,你的创业公司看起来需要一些类似下面的代码:

SplashScr.PropertyChanged += new PropertyChangedEventHandler<PropertyChangedEventArgs> (this.yourLabelChangerFunction);

一些改变标签的功能:

public void yourLabelChangerFunction (object sender, EventArgs e){...}

此外,您似乎在创建窗口之前设置字符串_SplashInfo,因此这可能是窗口标签不会更改的另一个原因。

答案 1 :(得分:0)

我认为绑定正在运行但是窗口没有更新,因为你在Splash窗口的Thread中执行了Thread.Sleep。你必须在它自己的线程中完成它。

答案 2 :(得分:0)

正如Marcel_Bonzelet所说,这是一个棘手的问题。试试这个,你会看到:

        protected override void OnStartup(StartupEventArgs e)
        {
            MainWindow window = new MainWindow();
            window.Visibility = Visibility.Hidden;
            new Task(() =>
            {
                Splash splashScr = null;
                Dispatcher.Invoke(() =>
                {
                    splashScr = new MainWindow();
                    splashScr.Show();
                });
                Stopwatch splashTimer = new Stopwatch();
                splashTimer.Start();

                splashScr.SplashInfo = "Ładowanie ....";

                splashTimer.Stop();

                int splashRemainingTime = splashMinTime - (int)splashTimer.ElapsedMilliseconds;
                if (splashRemainingTime > 0)
                    Thread.Sleep(splashRemainingTime);

                Dispatcher.Invoke(() =>
                {
                    splashScr.Close();
                    window.Visibility = Visibility.Visible;
                });
            }).Start();

            base.OnStartup(e);
        }

一些解释:在更新标签的同一线程上调用OnStartup方法。因此,如果您阻止此线程,然后立即关闭窗口,您将无法看到绑定的结果。