GTK UI从捆绑运行和从Xamarin Studio运行时看起来有所不同 [英] GTK UI Looks different when running from bundle and when running from Xamarin Studio

查看:253
本文介绍了GTK UI从捆绑运行和从Xamarin Studio运行时看起来有所不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有使用Mono框架的C#应用​​程序和用于UI的GTK。当我从Xamarin Studio运行它时,它看起来像这样


$ b



哪一个看起来更适合OSX平台?如果它是第一个,那么我怎样才能在捆绑的应用程序中获得相同的外观和感觉。

解决方案

GTK 2主题选项: ul>

  • 使用默认系统主题

  • 通过〜/ .gtkrc

  • 每个应用程序通过GTK API进行主题化

  • 每个应用程序通过环境变量var


    GTK 2主题



    主题通常以名为'gtkrc'的主文件通过资源文件在GTK 2中处理。这些资源文件可以是独立的,包括其他资源文件和引用外部图像(图标)等....基于 gtkrc 的内部文件超出了范围这里,但是网上有很多关于GTK 2.0资源/主题的参考资料。

    Mono w / GTK 2:



    Mono在OS-X上安装默认的GTK2主题( gtkrc )和几个替代主题:

    位置: / Library / Frameworks / Mono.framework / Versions / 4.2.1

      find。 -namegtkrc
    ./etc/gtk-2.0/gtkrc
    ./share/themes/bubble/gtk-2.0/gtkrc
    ./share/themes/Clearlooks/gtk-2.0 / gtkrc
    ./share/themes/Mac/gtk-2.0-key/gtkrc
    ./share/themes/Quartz/gtk-2.0/gtkrc
    ./share/themes/Xamarin/ gtk-2.0 / gtkrc

    位于< Mono / Version> ; /etc/gtk-2.0 是默认的(Tango风格),而其中一个替代方案是'Xamarin',并且是用于风格化Xamarin Studio的内部方案(内部命名为Xamarian的Mac主题 )。

    至于控制哪个主题应用于您的应用程序,您可以允许用户通过$ HOME目录控制它,该目录基于 .gtkrc 文件(恕我直言:有多少用户真的知道如何做到这一点,甚至是'专家'最终用户调整用户界面的做法和支持噩梦,我更愿意将UIX经验锁定为统一的用户,但是,这只是我的选择)。



    你应该能够设置环境var GTK2_RC_FILES 在你的应用启动脚本中,但是我从来没有碰到过这种情况。



    注意:env 。变种。 GTK_THEME 可以在GTK3中工作(但Mono是GTK2绑定的)


    • Ref:Very与此相关的好的堆栈交换@ https:/ /unix.stackexchange.com/questions/14129/gtk-enable-set-dark-theme-on-a-per-application-basis

    • Ref:另一个好Q /答: GTK +应用程序特定的皮肤可能?



    • 通过GTK API应用主题:

      通过应用程序,您可以将GTK分配给使用Mono安装的主题之一或应用程序提供的主题。



      BEFORE Gtk.Application.Init )您可以执行以下操作来分配gtkrc文件:

        Gtk.Rc.AddDefaultFile({ gtkrcFile}); 
      Gtk.Rc.Parse({gtkrcFile});

      如果您有自己的主题,请将文件(gtkrc,其他包含gtkrc文件,图标,图片,等等......)在你的 .app 中。通常这些将被捆绑在 Contents \Resource目录中,并使用上面的代码来分配捆绑包中的 gtkrc`。



      对于独立的exe文件,我也使用了一个嵌入的gtkrc文件,并将它解压缩到一个tmp文件的运行时:

       使用系统; 
      使用System.Reflection;
      使用System.IO;
      使用Gtk;

      命名空间主题
      {
      class MainClass
      {
      public static void Main(string [] args)
      {
      InitGlobalResourceFile ();
      Application.Init();
      MainWindow win = new MainWindow();
      win.Show();
      Application.Run();
      }

      static void InitGlobalResourceFile()
      {
      String gtkResouceFile;

      汇编myAssembly = Assembly.GetExecutingAssembly();
      string [] names = myAssembly.GetManifestResourceNames();
      foreach(名称中的字符串名称){
      Console.WriteLine(name);
      if(name.Contains(GtkThemeResource.rc)){
      gtkResouceFile = name;
      休息;
      }
      Console.WriteLine(在清单中未找到Gtk主题资源);
      }

      var tmpFile = Path.GetTempFileName();
      var stream = myAssembly.GetManifestResourceStream(gtkResouceFile);
      字符串gtkrc;
      using(StreamReader reader = new StreamReader(stream)){
      gtkrc = reader.ReadToEnd();

      using(StreamWriter outputFile = new StreamWriter(tmpFile)){
      outputFile.WriteLine(gtkrc);
      }
      Rc.AddDefaultFile(tmpFile);
      Rc.Parse(tmpFile);
      }

      }
      }




      I have C# App using Mono framework and GTK for UI.When i run it from Xamarin Studio it looks like this

      I have bundled Mono and GTK into a standalone bundle.When running from it it looks different.

      Which one looks more native to the OSX Platform? If its the first one,then how can i get the same look and feel in the bundled App.

      解决方案

      GTK 2 Theming Options:

      • Use the default system theme
      • Per user theming via ~/.gtkrc
      • Per application theming via GTK api
      • Per application theming via environment var

      GTK 2 Theming:

      Theming is handled in GTK 2 via resource files with the master normally named 'gtkrc'. These resources files can be standalone, include other resources files and reference external images (icons), etc.... The internals of gtkrc-based files are out-of-scope here, but there are plenty of references on the web for GTK 2.0 resources/theming.

      Mono w/ GTK 2:

      Mono on OS-X installs a default GTK2 theme (gtkrc) and few alternative themes:

      Location : /Library/Frameworks/Mono.framework/Versions/4.2.1

      find . -name "gtkrc"
      ./etc/gtk-2.0/gtkrc
      ./share/themes/bubble/gtk-2.0/gtkrc
      ./share/themes/Clearlooks/gtk-2.0/gtkrc
      ./share/themes/Mac/gtk-2.0-key/gtkrc
      ./share/themes/Quartz/gtk-2.0/gtkrc
      ./share/themes/Xamarin/gtk-2.0/gtkrc
      

      The one located in the <Mono/Version>/etc/gtk-2.0 is the default (Tango style) while one of the alternatives is 'Xamarin' and is the one that is used to stylize Xamarin Studio (internally named "Mac Theme for Xamarian").

      As for controlling which theme is applied to your application, you can allow the user to control it via $HOME directory based .gtkrc files (IMHO: how many users really know how to do that or would even what to and the support nightmares of 'expert' end-users tweaking the UI, I prefer locking the UIX experience to something uniform to all users, but, again, that is just my opition).

      You are supposed to be able to set environment var GTK2_RC_FILES in your app startup script, but I never had any luck with this.

      Note: The env. var. GTK_THEME does work in GTK3 (but Mono is GTK2 bound)

      Application theming via GTK api:

      With your application you can assign GTK to use one of the Mono installed themes or one that your application provides.

      BEFORE the Gtk.Application.Init() you can do the following to assign the gtkrc file:

      Gtk.Rc.AddDefaultFile ({gtkrcFile}); 
      Gtk.Rc.Parse ({gtkrcFile}); 
      

      If you have your own theme, bundle the files (gtkrc, other included gtkrc files, icons, images, etc...) inside your .app. Normally these would be bundled inside the Contents\Resource directory and use the above code to assign thegtkrc` that is in your bundle.

      For standalone exe's I have also have used an embedded gtkrc file and extract it to a tmp file a runtime:

      using System;
      using System.Reflection;
      using System.IO;
      using Gtk;
      
      namespace themes
      {
          class MainClass
          {
              public static void Main (string[] args)
              {
                  InitGlobalResourceFile ();
                  Application.Init ();
                  MainWindow win = new MainWindow ();
                  win.Show ();
                  Application.Run ();
              }
      
              static void InitGlobalResourceFile ()
              { 
                  String gtkResouceFile;
      
                  Assembly myAssembly = Assembly.GetExecutingAssembly ();
                  string[] names = myAssembly.GetManifestResourceNames ();
                  foreach (string name in names) {
                      Console.WriteLine (name);
                      if (name.Contains ("GtkThemeResource.rc")) {
                          gtkResouceFile = name;
                          break;
                      }
                      Console.WriteLine ("Gtk Theme Resource not found in manifest");
                  }
      
                  var tmpFile = Path.GetTempFileName ();
                  var stream = myAssembly.GetManifestResourceStream (gtkResouceFile);
                  string gtkrc;
                  using (StreamReader reader = new StreamReader (stream)) {
                      gtkrc = reader.ReadToEnd ();
                  }
                  using (StreamWriter outputFile = new StreamWriter (tmpFile)) {
                      outputFile.WriteLine (gtkrc);
                  }
                  Rc.AddDefaultFile (tmpFile); 
                  Rc.Parse (tmpFile); 
              }
      
          }
      }
      

      这篇关于GTK UI从捆绑运行和从Xamarin Studio运行时看起来有所不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆