【问题标题】:wpf application not showing mainwindow anymorewpf应用程序不再显示主窗口
【发布时间】:2011-04-03 17:26:48
【问题描述】:

我的应用程序构建并运行良好。我在任务栏中得到一个图标,显示该窗口存在,但它从未显示。我从 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();
    }
}

【问题讨论】:

  • 谢谢克里斯!抱歉,我是新手。
  • @Jon 好的,我现在已经完成了编辑。
  • 自上次运行版本以来,我添加了 App_Startup 函数和 Properties.Settings 字符串集合 Clocks。 App_Startup函数中的foreach循环完成,然后一切都停止了,在调试器中使用F11没有任何响应。
  • 还有一件事。我在 Settings.settings 页面中单击了同步。这弹出一个窗口,说没有文件。
  • 在运行了以前的版本之后,似乎发生了什么破坏了我使用“设置”页面的所有版本。上一个工作版本没有对“设置”页面进行任何编辑,所有损坏的都有。

标签: wpf mainwindow


【解决方案1】:

我怀疑窗口在那里,但在屏幕边界之外...检查您分配给 TopLeft 的值

【讨论】:

  • @nicky,如果您删除应用程序设置(左侧和顶部)的绑定,它会起作用吗?
  • 你说它只是在屏幕外是正确的。我将 Properties.Settings.Default.Top(和 Left)硬编码为我想要的,然后显示窗口。现在我只需要看看为什么数字被破坏了。在调试器中,它说它们具有相同的值,一定是在绑定过程中改变了它们。
  • 似乎在创建多个主窗口实例时使用内置的 Properties.Settings 会导致问题。太多意想不到的结果。为了解决我的问题,我遍历当前窗口并使用每个窗口的值创建字符串,将它们放在集合中并将其序列化到文件中。像魅力一样工作。
猜你喜欢
  • 2019-02-07
  • 1970-01-01
  • 2017-09-09
  • 1970-01-01
  • 1970-01-01
  • 2011-04-05
  • 2011-07-04
  • 1970-01-01
相关资源
最近更新 更多