繁体   English   中英

将Gtk#对话框放置在Gtk窗口的中心

[英]Position Gtk# Dialog in the center of a Gtk window

我试图将GTK#对话框放置在窗口的中心,我也想使该对话框出现时不可编辑父窗口。 我尝试设置parent属性和对话框的位置,并将其位置设置为以父窗口为center on parent 。(父窗口位置始终居中,父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类不是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的设计器。 如果您使用设计器,然后调用HideAll()之前勾住ShowAll(),如果你需要调用SETPOSITION后使用的小工具,或移动电话建设()()。

希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM