开发者

wpf application not showing mainwindow anymore

开发者 https://www.devze.com 2023-02-21 23:20 出处:网络
My application builds and runs fine. I get an icon in the taskbar showing that the window exists, but it never shows. I call StartupUri=\"MainWindow.Xaml\" from app.xaml and mainwindow just contains s

My application builds and runs fine. I get an icon in the taskbar showing that the window exists, but it never shows. I call StartupUri="MainWindow.Xaml" from app.xaml and mainwindow just contains some functions and InitializeComponent(). When debugging, it runs until the window is supposed to open, then stops, and I cannot step through anything anymore. I can't even tell where the debugger is because the yellow highlight is gone and the code doesn't change. I am supposing it is waiting on user interaction at this point because it thinks the window opened successfully. Any Ideas?

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.ConvertTimeByS开发者_如何学JAVAystemTimeZoneId(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();
    }
}


I suspect the window is there, but outside the bounds of the screen... check the values you assign to Top and Left

0

精彩评论

暂无评论...
验证码 换一张
取 消