简体   繁体   English

事件已触发,但文本未更改

[英]Event is fired but text doesn't change

What im trying to do is to change Splash window label content. 我试图做的是更改启动窗口标签的内容。 App code behind is as follows 后面的应用代码如下

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

Splash

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

And my Splash.xaml 还有我的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 is fired but i don't see the changes in the splash window label. Splash PropertyChangedEventHandler被触发,但我在启动窗口标签中看不到更改。

Maybe I'm missing something, but for me it looks like you need some code like the following in your startup: 也许我缺少了一些东西,但是对我来说,您似乎需要在启动时输入以下代码:

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

And some function that changes the label: 还有一些更改标签的功能:

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

Also, you seem to be setting string _SplashInfo before the window has been created, so maybe that's another reason why the window label doesn't change. 另外,您似乎在创建窗口之前设置了字符串_SplashInfo,所以这也许是窗口标签不变的另一个原因。

I think the binding is working but the window is not updated because you do Thread.Sleep in the Thread of the Splash window. 我认为绑定正在工作,但窗口未更新,因为您在“启动”窗口的“线程”中执行了Thread.Sleep。 You have to do it in its own Thread. 您必须在其自己的线程中执行此操作。

It's a treading issue as Marcel_Bonzelet said. 正如Marcel_Bonzelet所说,这是一个艰巨的问题。 Try this and you will see: 试试看,您将看到:

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

Some explanation: the OnStartup method is called on the same thread where your label would be updated. 一些解释:在将更新标签的同一线程上调用OnStartup方法。 So if you block this thread, and after that you immediately close the window, you won't be able to see the result of the binding. 因此,如果阻止此线程,然后立即关闭窗口,将无法看到绑定的结果。

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

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