将 Gtk# Dialog 置于 Gtk 的中心 window

Position Gtk# Dialog in the center of a Gtk window

我正在尝试将一个 GTK# 对话框放置在 window 的中心,我还想在该对话框出现时使父 window 不可编辑。 我尝试设置 parent 属性 和对话框并将其位置设置为 center on parent。(父 windows 位置始终居中,父 appers 居中但一部分它在任务栏下方)

我做错了什么?

更新:

我尝试设置 transcientfor 属性。

        mywin d = new mywin();
        d.Parent = this;
        d.WindowPosition = WindowPosition.CenterOnParent; 
        d.TransientFor = this; 
        d.WidthRequest = 360;
        d.HeightRequest =260;
        d.Resizable = false; 
        d.Show (); 

我在执行此操作时也会在应用程序输出中收到此错误

(myapp:5844): Gtk-WARNING **: Can't set a parent on a toplevel widget

(myapp:5844): Gdk-CRITICAL **: inner_clipboard_window_procedure: assertion 'success' failed

更新: 从父

调用对话框
protected void OnButton7Clicked (object sender, EventArgs e)
    {
            var mydiag = new mydiag( this );

            if ( ( (Gtk.ResponseType) mydiag.Run() ) == Gtk.ResponseType.Ok ) {
                // do whatever here...
            }
     }

对话框代码

using System;

namespace myproj
{
    public partial class mydiag : Gtk.Dialog
    {
        public mydiag (Gtk.Window parent)
        {
            this.Build ();
            this.Title = parent.Title + " more info";
            this.Icon = parent.Icon;
            this.Parent = parent;
            this.TransientFor = parent;
            this.SetPosition( Gtk.WindowPosition.CenterOnParent );
            this.ShowAll();
        }
    }
}

这里报告的问题:

(myapp:5844): Gtk-WARNING **: Can't set a parent on a toplevel widget

你的 mywin class 不是 Gtk.Dialog,但可能是 Gtk.Window。你应该这样创建你的主window:

namespace Whatever {
    public class MainWindow: Gtk.Window {
        public MainWindow()
            : base( Gtk.WindowType.Toplevel )
        {
            this.Title = "Gtk# App";
            this.Build();
        }

        private void Build() {
            // create your widgets
        }

        // more things...
}

假设现在您需要为应用程序中的某些功能打开一个对话框。

namespace Whatever {
    public class MainWindow: Gtk.Window {
        // ...more things
        private void OnWhatever() {
            var dlg = new DlgMoreInfo( this );

            if ( ( (Gtk.ResponseType) dlg.Run() ) == Gtk.ResponseType.Ok ) {
                // do whatever here...
            }

            dlg.Destroy();
        }
    }
}

最后,您需要创建对话框 DlgMoreInfo 居中等

namespace Whatever {
    public class DlgMoreInfo : Gtk.Dialog {
        public DlgMoreInfo(Gtk.Window parent) {
            this.Build();

            this.Title = parent.Title + " more info";
            this.Icon = parent.Icon;
            this.TransientFor = parent;
            this.SetPosition( Gtk.WindowPosition.CenterOnParent );
            this.ShowAll();
        }

        private void Build() {
            // Create widgets here...
            // Buttons
            this.AddButton( Gtk.Stock.Cancel, Gtk.ResponseType.Cancel );
            this.AddButton( Gtk.Stock.Ok, Gtk.ResponseType.Ok );
            this.DefaultResponse = Gtk.ResponseType.Ok;
        }
    }
}

您只能创建 child 顶级 window 的对话框; window 永远不可能是另一个的 child。

请注意,此代码未使用 Xamarin Studio 的设计器。如果您使用设计器,则在 ShowAll() 之前调用 HideAll(),如果您需要使用小部件,或将调用移至Build() 在调用 SetPosition().

之后

希望对您有所帮助。