簡體   English   中英

C#WPF-如何在運行后自動刷新UI

[英]C# WPF - How to auto refresh the UI after run it

我在XAML中創建了一個稱為TbTimer的簡單Label

我做了以下代碼:

class Level2 
{
    public Level2()
    {
        timer = new DispatcherTimer();
        timer.Interval = TimeSpan.FromSeconds(1);
        timer.Tick += timer_Tick;
    }
    public int counter;
    public void timer_Tick(object sender, EventArgs e)
    {
        counter++;
    }

    public DispatcherTimer timer;
}

public partial class MainWindow : Window
{

    public MainWindow()
    {
        InitializeComponent();

        lvl2 = new Level2();
    }

    private void MenuItemMedium_Click(object sender, RoutedEventArgs e)
    {
        lvl2.timer.Start();
        TbTimer.Content = lvl2.counter.ToString();
    }
}

然后,我有另一個按鈕,單擊該按鈕時我調用TimerUpdater

當我運行程序並單擊按鈕時,我可以看到TextBlock的內容顯示數字1 ...並且它不會繼續運行數字-5秒鍾后再次單擊按鈕時,它顯示數字6。

因此,我認為計時器在后台運行良好,但是TextBlock的內容僅在單擊按鈕時才更新。

我該怎么做才能使TextBlock內容在不單擊按鈕的情況下在幾秒鍾內更新? 希望我的解釋和問題清楚。

使用修改后的代碼,答案將完全改變。 對於內容的巨大變化,我深表歉意。 完成此操作的“最簡單”方法是為計數器更新添加事件,並讓您的UI訂閱該事件。 就像是:

class Level2
{
    public event Action<int> CounterUpdated;

    ...
    public void timer_Tick(object sender, EventArgs e)
    {
        counter++;
        if (CounterUpdated != null)
           CounterUpdated(counter);
    }
}

public class MainWindow
{
    public MainWindow()
    {
       InitializeComponent();

       lvl2 = new Level2();
       lvl2.CounterUpdated += UpdateCounterText;
    }

    private void MenuItemMedium_Click(object sender, RoutedEventArgs e)
    {
       lvl2.timer.Start();
    }

    private void UpdateCounterText(int newCounterValue)
    {
        TbTimer.Content = newCounterValue.ToString();
    }
}

順便說一句,這最終與綁定系統的設置類似。 如果您僅將文本框綁定到counter變量,它將更加干凈和易於使用。 為此,您可以將XAML更改為:

<TextBox Name="TbTimer" Text="{Binding Counter}"/>

並分配DataContext:

public class MainWindow
{
    public MainWindow()
    {
       InitializeComponent();

       lvl2 = new Level2();
       DataContext = lvl2;
    }

    private void MenuItemMedium_Click(object sender, RoutedEventArgs e)
    {
        lvl2.timer.Start();
    }
}

現在, Level2需要實現INotifyPropertyChanged,並且您必須將counter作為屬性(以便可以綁定到):

class Level2 : INotifyPropertyChanged
{
    //Notify Property Changed Implementation from MSDN:
    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
        {
           PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    private int counter = 0;
    public int Counter
    {
       get { return counter; }
       set
       {
           counter = value;
           NotifyPropertyChanged();
       }
    }

    ...
    public void timer_Tick(object sender, EventArgs e)
    {
        Counter++;
    }
}

現在,綁定系統將在計時器計時(並增加Counter屬性)時自動更新文本框。這是在WPF中采用的方式,因此在實施它時可以隨時提出任何問題。

作為參考,這是我使用的INofityPropertyChanged的實現: http : //msdn.microsoft.com/zh-cn/library/system.componentmodel.inotifypropertychanged.aspx

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM