Drift bei der Wiederherstellung Fenster Position und Größe in WPF
Frage
Ich verwende den Code zu speichern und die Fensterposition und Größe beim Neustart wiederherstellen.
Ich beobachte eine Aufwärtsbewegung von 28 Pixeln jedes Mal ausführen ich diesen Code !
Lese ich die falschen Werte, oder bin ich sie falsch Wiederherstellung? Wo ist die Nummer 28 (Größe des Chrom?), Die von (und wie würde ich es erklären programmatisch, anstatt eine feste Zahl im Code)?
Hier ist mein Code:
public partial class MainStudioWindowControl : RibbonWindow
{
public MainStudioWindowControl()
{
App.MainWindowOwner = this;
this.Loaded += new System.Windows.RoutedEventHandler(MainStudioWindowControl_Loaded);
}
void MainStudioWindowControl_Loaded(object sender, System.Windows.RoutedEventArgs e)
{
System.Windows.Window mainWindow = System.Windows.Application.Current.MainWindow;
mainWindow.WindowStartupLocation = System.Windows.WindowStartupLocation.Manual;
if (Studio.Properties.Settings.Default.Width > 0)
{
mainWindow.Left = Studio.Properties.Settings.Default.Left;
mainWindow.Top = Studio.Properties.Settings.Default.Top;
mainWindow.Width = Studio.Properties.Settings.Default.Width;
mainWindow.Height = Studio.Properties.Settings.Default.Height;
}
Debug.WriteLine(string.Format("Loading: Top = {0}", this.Top));
}
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
base.OnClosing(e);
System.Windows.Window mainWindow = System.Windows.Application.Current.MainWindow;
Studio.Properties.Settings.Default.Left = mainWindow.Left;
Studio.Properties.Settings.Default.Top = mainWindow.Top;
Studio.Properties.Settings.Default.Width = mainWindow.Width;
Studio.Properties.Settings.Default.Height = mainWindow.Height;
Studio.Properties.Settings.Default.Save();
Debug.WriteLine(string.Format("Saving: Settings.Top = {0}", Studio.Properties.Settings.Default.Top));
}
}
Lösung
Versuchen Sie folgendes:
1) Leiten Sie Ihre Klasse von den normalen Fenstern, nicht die RibbonWindow - wenn es das behebt, ist es eine RibbonWindow Ausgabe
.2) Verwenden Sie hartcodierte Werte die measurments in den Loaded-Handler gesetzt -. Wenn es das behebt, bekam das Problem etwas mit den Einstellungen zu tun
Mit diesen beiden Änderungen, es funktionierte gut für mich. Das Fenster erschien, wo es sollte jedes Mal.