我的应用程序构建并运行良好。我在任务栏中得到一个图标,显示该窗口存在,但它从未显示。我从 app.xaml 调用 StartupUri="MainWindow.Xaml",而主窗口只包含一些函数和 InitializeComponent()。调试时,它会一直运行,直到窗口应该打开,然后停止,我无法再单步执行任何操作。我什至不知道调试器在哪里,因为黄色高亮消失了,代码也没有改变。我假设它此时正在等待用户交互,因为它认为窗口已成功打开。有任何想法吗?
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();
}
}