将Gtk#Dialog置于Gtk窗口的中心位置

时间:2015-08-03 06:56:27

标签: c# .net mono gtk gtk#

我试图将GTK#对话框放在窗口的中心,我也希望在出现此对话框时使父窗口不可编辑。 我尝试设置parent属性和对话框并将其位置设置为center on parent。(父窗口位置始终居中,父窗口居中但其中一部分位于任务栏下方)

我做错了什么?

更新:

我尝试设置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();
        }
    }
}

1 个答案:

答案 0 :(得分:2)

这里有问题报道:

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

您的 mywin 类不是 Gtk.Dialog ,但可能是 Gtk.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;
        }
    }
}

您只能创建作为顶级窗口子项的对话框;从来没有一个窗口可以成为另一个窗口的孩子。

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

希望这有帮助。

相关问题