-1
的意外的順序當我運行我的代碼,行:調試器控制檯輸出
WriteLine("Saved Files " + saveFiles + "\n");
它前面的其它輸出線之前出來。我怎樣才能解決這個問題?代碼如下。
這裏也是一個簡短的video我展示了我的意思,但代碼如下。完整代碼here。非常感謝。
using DemoMemento;
using System.Windows;
using static System.Diagnostics.Debug;
// This Memento patter will create a caretaker that contains the collection
// with all the Statements in it. It can add and
// retrieve Statements from the collection
namespace Memento
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
Caretaker caretaker = new Caretaker();
// The originator sets the value for the statement,
// creates a new memento with a new statement, and
// gets the statement stored in the current memento
Originator originator = new Originator();
int saveFiles = 0, currentStatement = -1;
// ---------------------------------------------
public MainWindow()
{
InitializeComponent();
}
private void btnSave_Click(object sender, RoutedEventArgs e)
{
// Get text in TextBox
string text = theStatement.Text;
// Set the value for the current memento
originator.set(text);
// Add new statement to the collection
caretaker.addMemento(originator.storeInMemento());
// saveFiles monitors how many statements are saved
// Number of mementos I have
saveFiles++;
currentStatement++;
WriteLine("Saved Files " + saveFiles + "\n");
btnUndo.IsEnabled = true;
}
private void btnUndo_Click(object sender, RoutedEventArgs e)
{
if (currentStatement >= 1)
{
currentStatement--;
string textBoxString = originator.restoreFromMemento(caretaker.getMemento(currentStatement));
theStatement.Text = textBoxString;
btnRedo.IsEnabled = true;
}
else {
btnUndo.IsEnabled = false;
}
}
private void btnRedo_Click(object sender, RoutedEventArgs e)
{
if ((saveFiles - 1)> currentStatement)
{
currentStatement++;
string textBoxString = originator.restoreFromMemento(caretaker.getMemento(currentStatement));
theStatement.Text = textBoxString;
btnUndo.IsEnabled = false;
}
else
{
btnRedo.IsEnabled = false;
}
btnUndo.IsEnabled = true;
}
}
}
'System.Threading.Thread.Sleep(500);'在UI線程中永遠不會是正確的事情。此外,您需要提出您的問題[最小完整可驗證示例](http://stackoverflow.com/help/mcve),否則您的問題可能會被關閉。應該不需要轉到「完整代碼」鏈接,以便我們能夠幫助解決您的問題,所有內容都應該位於代碼段內,並且應該是重新創建問題的最小可能代碼段。 –
我在帖子中刪除了那條睡眠線,但我估計你的速度不夠快,看不到它。這是一個臨時解決方案。 – myfunnyfella