2011-04-03 93 views
2

我的應用程序構建並運行良好。我在任務欄中看到一個圖標,顯示該窗口存在,但它從不顯示。我從app.xaml調用StartupUri =「MainWindow.Xaml」,mainwindow只包含一些函數和InitializeComponent()。調試時,它會一直運行,直到窗口應該打開,然後停止,我無法再通過任何東西。我甚至無法知道調試器的位置,因爲黃色突出顯示消失了,代碼也沒有改變。我認爲此時它正在等待用戶交互,因爲它認爲窗口已成功打開。有任何想法嗎?wpf應用程序不再顯示mainwindow

public partial class App : Application 
{ 
    private void Application_Startup(object sender, StartupEventArgs e) 
    { 
     foreach (string s in TZClock.Properties.Settings.Default.Clocks) 
     { 
      // x, y, currentCity, color 
      Globals.state = s.Split(','); 

      MainWindow NewWindow = new MainWindow(); 
      NewWindow.Top = double.Parse(Globals.state[0]); 
      NewWindow.Left = double.Parse(Globals.state[1]); 
      Globals.currentCity = Globals.state[2]; 
      Globals.color = Globals.state[3]; 
      NewWindow.Show(); 
     } 
    } 
} 
<Window x:Class="TZClock.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:p="clr-namespace:TZClock.Properties" 
      WindowStartupLocation="Manual" Loaded="Window_Loaded" 
      Height="155" Width="271" 
      Left="{Binding Source={x:Static p:Settings.Default}, Path=Left, Mode=TwoWay}" 
      Top="{Binding Source={x:Static p:Settings.Default}, Path=Top, Mode=TwoWay}"> 

    <Grid Height="120" Width="258" Margin="0,0,0,0" > 
     <Rectangle Opacity=".75" Fill="{Binding Source={x:Static p:Settings.Default}, Path=Color, Mode=TwoWay}" RadiusX="8" RadiusY="8" 
        Stroke="#FF94B494" StrokeThickness="5" Margin="0,0,12,12"></Rectangle> 
     <ComboBox Name="DropDown" Width="139" Height="23" HorizontalAlignment="Left" VerticalAlignment="Top" 
        Margin="51,13,0,0" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" 
        SelectionChanged="DropDown_SelectionChanged" /> 
    </Grid> 
</Window> 
public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     this.Uid = Globals.num.ToString(); 
     InitializeComponent(); 
     LoadCityDictionary(); 
     displayTimeZoneInfo(); 
     scheduleTimer(); 
    } 

    private void scheduleTimer() 
    { 
     // http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatchertimer.aspx 
     // schedule a new, timed thread to refresh time (will not block the UI Thread) 
     DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer(); 
     dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick); 
     dispatcherTimer.Interval = new TimeSpan(0, 0, 1); 
     dispatcherTimer.Start(); 
    } 

    private void dispatcherTimer_Tick(object sender, EventArgs e) 
    { 
     // http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatchertimer.aspx 
     // refresh time label by current timezone 
     Time.Content = (TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.Now, currentZone)).ToLongTimeString(); 
     Date.Content = (TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.Now, currentZone)).ToShortDateString(); 

     // Forcing the CommandManager to raise the RequerySuggested event 
     CommandManager.InvalidateRequerySuggested(); 
    } 

    private void Window_Loaded(object sender, RoutedEventArgs e) 
    { 
     this.DropDown.SelectedValue = Globals.currentCity; 
    } 

    private void DropDown_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     Globals.currentCity = DropDown.SelectedItem.ToString(); 
     TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById(d[Globals.currentCity]); 

     Time.Content = TimeZoneInfo.ConvertTime(DateTime.Now, tz).ToLongTimeString(); 
     Date.Content = TimeZoneInfo.ConvertTime(DateTime.Now, tz).ToShortDateString(); 
     Location.Content = (tz.IsDaylightSavingTime(TimeZoneInfo.ConvertTime(DateTime.Now, tz)) ? tz.DaylightName : tz.Id); 

     currentZone = tz.Id; 

     Properties.Settings.Default.CurrentCity = Globals.currentCity; 
     Properties.Settings.Default.Save(); 
    } 
} 
+0

後一些代碼的值。 – Jon 2011-04-03 17:38:03

+0

謝謝克里斯!對不起,我是這個新手。 – nicky 2011-04-03 17:58:57

+0

@Jon好吧,我現在正在編輯。 – nicky 2011-04-03 18:39:45

回答

4

我懷疑窗口是存在的,但屏幕的界限之外...查看分配給TopLeft

+0

好主意,但似乎並非如此。 – nicky 2011-04-03 19:21:18

+0

@nicky,如果你刪除了應用程序設置的綁定(對於Left和Top),它是否工作? – 2011-04-03 21:18:19

+0

你說這只是在屏幕之外是正確的。我硬編碼Properties.Settings.Default.Top(和左)我想要的,窗口顯示。現在我只需要明白爲什麼這些數字會被破壞。在調試器中,它表示它們具有相同的值,必須在綁定過程中更改它們。 – nicky 2011-04-04 04:03:07

相關問題