2011-04-03 147 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