2017-08-07 48 views
-1

我的程序出現問題,我嘗試將Accesstext.text設置爲字符串,但是當我運行它時,文本不顯示。使用WPF訪問文本時遇到問題

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
    } 

    public void WindowStartupSettings() 
    { 
     TextblockAnnouncer.Text = "Press start to play"; 
    } 

    private void ButtonStart_Click(object sender, RoutedEventArgs e) 
    { 
     new Game(); 
    }  
} 

這是一個具有ShowKey

class Game : MainWindow 
{ 
    public Game() 
    { 
     WindowDefaultSettings(); 
     CurrentNumber = NewNumber; 
     CurrentKey = ConvertIntToKey();    
     ShowKey(); 
    } 

    public void UpdateTimer() 
    { 
     while (Watch.IsRunning) 
     { 
      TextBoxTimer.Text = Watch.ElapsedMilliseconds.ToString(); 
     } 
    } 

    public void ShowKey() 
    { 
     AccessTextKey.Text = CurrentKey.ToString(); 
     Watch.Start(); 
    } 

      public void WindowDefaultSettings() 
    { 
     TextBoxTimer.Text = "0.00"; 
     AccessTextKey.Text = string.Empty; 
    } 

    public int GenerateNumber() 
    { 
     return new Random().Next(0,2); 
    } 

} 誰能告訴我,如果即時通訊愚蠢或只是什麼是錯的這個類()。

+3

什麼是「AccessTextKey」? – mm8

+1

看來你應該添加更多關於你的問題的細節...... –

+0

AccessTextKey是AccessText的名稱 –

回答

0

你有一個起始窗口MainWindow最初顯示,並且你有一個窗口類Game。在某個時刻,您創建了一個Game實例,但您從不顯示新創建的遊戲窗口。試試以下內容,然後從那裏繼續:

private void ButtonStart_Click(object sender, RoutedEventArgs e) 
{ 
    var gameWindow = new Game(); 
    gameWindow.Show(); 
} 

既然你在Game設置AccessTextKey,它不會改變你的AccessTextKey主窗口。

+0

感謝您的耐心:p,這對我來說很有意義。現在就試試 –

+0

剛剛做到了這一點,它的工作原理,但這對我來說有點奇怪。我可以使窗口以某種方式覆蓋MainWindow而不是打開一個新窗口嗎? –

+0

想象得那麼多...那就是爲什麼我加了*「然後從那裏繼續」* :)您應該更多地使用窗口內容而不是窗口本身。但是,對於當前的問題,這正在擺脫主題。 – grek40