C# 如何使用gtk更新gtk中的图像。按钮#

C# 如何使用gtk更新gtk中的图像。按钮#,c#,button,icons,gtk#,stock,C#,Button,Icons,Gtk#,Stock,我当时正在开发一个简单的应用程序,用于在mono中启动/停止和重新启动我的LAMP(只是为了更多地了解mono的GUI开发),因此为了减少按钮,我决定使用一个按钮来启动和停止服务器。在GUI设计器中,我添加了一个带有图标的开始按钮,问题是更新按钮标签很容易,但是将图像更改为Stock.MediaStop有点问题。那么,如何在点击事件的按钮中更改股票图像(它不取决于它实际上是什么类型的事件)。下面是一些代码: 按钮的GUI XML: <widget class="Gtk.Button" id

我当时正在开发一个简单的应用程序,用于在mono中启动/停止和重新启动我的LAMP(只是为了更多地了解mono的GUI开发),因此为了减少按钮,我决定使用一个按钮来启动和停止服务器。在GUI设计器中,我添加了一个带有图标的开始按钮,问题是更新按钮标签很容易,但是将图像更改为Stock.MediaStop有点问题。那么,如何在点击事件的按钮中更改股票图像(它不取决于它实际上是什么类型的事件)。下面是一些代码: 按钮的GUI XML:

<widget class="Gtk.Button" id="button1">
       <property name="MemberName" />
       <property name="CanFocus">True</property>
       <property name="Type">TextAndIcon</property>
       <property name="Icon">stock:gtk-media-play Menu</property>
       <property name="Label" translatable="yes">Start</property>
       <property name="UseUnderline">True</property>
       <signal name="Clicked" handler="OnMysqlServerStartStop" />
</widget>

我明白你为什么在这里这么麻烦。我以为你可以只设置按钮的图像和文本属性,但似乎你可以显示标签或图像,但不能同时显示两者。标签会覆盖图像。我认为只有在主题设置要求时,图像才会显示。这同样适用于标签。您可以同时拥有两者,仅一个标签或仅一个图像。所有设置均由您使用的主题设置

您在这里所处的轨道是正确的,此示例代码应该回答您的查询

using System;
using Gtk;

namespace TogglePlay
{
    public class MainClass
    {
        private bool stop = false;
        private Image image;
        private Label label;

        public MainClass ()
        {
            Button button = new Button();
            VBox box = new VBox();
            image = new Image(Stock.MediaPlay, IconSize.Button);
            box.PackStart(image, true, true, 0);
            label = new Label("Start");
            box.PackStart(label, true, true, 0);
            button.Add(box);
            button.Clicked += OnButtonClicked;
            Window window = new Window("LAMP light");
            window.Add(button);
            window.DeleteEvent += DeleteWindow;
            window.ShowAll();
        }


        private void DeleteWindow(object obj, DeleteEventArgs args)
        {
            Gtk.Application.Quit();
        }

        public void OnButtonClicked(object widget, EventArgs args)
        {
            if (!stop) {
                stop = true;
                image.Stock = Stock.MediaStop;
                label.Text = "Stop";
            } else {
                stop = false;
                image.Stock = Stock.MediaPlay;
                label.Text = "Start";
            }
        }


        public static void Main(String[] args)
        {
            Gtk.Application.Init ();
            MainClass mainClass = new MainClass();
            Gtk.Application.Run ();
        }
    }
}
using System;
using Gtk;

namespace TogglePlay
{
    public class MainClass
    {
        private bool stop = false;
        private Image image;
        private Label label;

        public MainClass ()
        {
            Button button = new Button();
            VBox box = new VBox();
            image = new Image(Stock.MediaPlay, IconSize.Button);
            box.PackStart(image, true, true, 0);
            label = new Label("Start");
            box.PackStart(label, true, true, 0);
            button.Add(box);
            button.Clicked += OnButtonClicked;
            Window window = new Window("LAMP light");
            window.Add(button);
            window.DeleteEvent += DeleteWindow;
            window.ShowAll();
        }


        private void DeleteWindow(object obj, DeleteEventArgs args)
        {
            Gtk.Application.Quit();
        }

        public void OnButtonClicked(object widget, EventArgs args)
        {
            if (!stop) {
                stop = true;
                image.Stock = Stock.MediaStop;
                label.Text = "Stop";
            } else {
                stop = false;
                image.Stock = Stock.MediaPlay;
                label.Text = "Start";
            }
        }


        public static void Main(String[] args)
        {
            Gtk.Application.Init ();
            MainClass mainClass = new MainClass();
            Gtk.Application.Run ();
        }
    }
}