我的应用程序构建并运行良好。我在任务栏中看到一个图标,显示该窗口存在,但它从未显示过。我从app.xaml调用StartupUri =“MainWindow.Xaml”,mainwindow只包含一些函数和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();
}
}
答案 0 :(得分:4)
我怀疑窗口在那里,但在屏幕范围之外...检查您分配给Top
和Left
的值