WPFでウィンドウの場所とサイズを復元する際のドリフト
質問
以下のコードを使用して、再起動時にウィンドウの位置とサイズを保存および復元しています。
このコードを実行するたびに28ピクセルの上方へのドリフトが発生しています!
間違った値を読んでいますか、それとも間違って復元していますか?番号28(クロームのサイズ?)はどこから来ていますか(そして、コード内の固定数ではなく、プログラムでどのようにそれを説明しますか)。
ここに私のコードがあります:
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));
}
}
解決
これを試してください:
1)RibbonWindowではなく、通常のWindowからクラスを派生します-これが修正される場合、RibbonWindowの問題です。
2)ハードコードされた値を使用して、Loadedハンドラーで測定値を設定します-それが修正された場合、問題は設定と関係があります。
これら2つの変更により、私にとってはうまくいきました。ウィンドウは毎回必要な場所に表示されました。
所属していません StackOverflow