Приложение wpf больше не отображает главное окно - PullRequest
2 голосов
/ 03 апреля 2011

Мое приложение собирается и работает нормально.Я получаю значок на панели задач, показывающий, что окно существует, но оно никогда не отображается.Я вызываю StartupUri = "MainWindow.Xaml" из app.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();
    }
}

1 Ответ

4 голосов
/ 03 апреля 2011

Я подозреваю, что окно есть, но за пределами экрана ... проверьте значения, которые вы назначаете Top и Left

...