pat*_*vax 6 c# mono cross-platform gtk# .net-core
我即将开始开发一个软件项目,如果可能的话,该项目应该在 Linux 和 Windows 上运行。由于我已经有一些 C# 经验,所以我渴望将它用于这个项目。我认为对于 .NET Core 3 和 GTK# 3.22 这应该不是问题,因为 .NET Core 应用程序应该是开箱即用的跨平台。GTK# - 根据我的理解 - 应该可以在任何地方使用相同版本的 GTK+。
为什么是 C#?嗯,我只是喜欢这种语言,而且我想使用一个适用于 c# 的 ECS 框架。
到目前为止,我已经在 Visual Studio 中设置了一个针对 .NET Core 3 的测试控制台应用程序项目,并添加了GTK# 特定的 NuGet 包。
我写了一个简单的Hello World程序来测试环境。
using System;
using Gtk;
namespace GTKTest
{
class Program
{
static void Main(string[] args)
{
Application.Init();
Window win = new Window("Hello GTK");
Label lbl = new Label("This is a test GTK App written with C# for GNU/Linux and Windows");
win.DeleteEvent += Win_DeleteEvent;
win.Add(lbl);
win.ShowAll();
Application.Run();
win.Dispose();
Console.Write("Press any key...");
Console.ReadKey();
}
private static void Win_DeleteEvent(object o, DeleteEventArgs args)
{
Application.Quit();
args.RetVal = true;
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我从 Visual Studio 2019 运行此代码时,我得到
System.TypeInitializationException
HResult=0x80131534
Message=The type initializer for 'Gtk.Application' threw an exception.
Source=GtkSharp
StackTrace:
at Gtk.Application.Init()
at GTKTest.Program.Main(String[] args) in D:\Workspace\VSRepos\C#\GTKTest\Program.cs:line 10
Inner Exception 1:
DllNotFoundException: Gtk
Run Code Online (Sandbox Code Playgroud)
在寻找解决方案时,我从此页面安装了适用于 Windows 的 mono 和 GTK#。我认为,如果我坚持使用 .NET Core,那么单声道部分就没有必要了。
我缺少什么?我究竟做错了什么?我想要实现的目标是否像我想象的那样可能?我还对如何使用 C# 编写跨平台 GUI 软件的一些替代方案感兴趣。我偶然发现了 Electron.js,但我听说它有一些很大的内存开销,而且我不太喜欢 JavaScript。AvaloniaUI 听起来很有趣,但我认为上述方法会更好。
编辑:按照步骤 3 中的建议添加 msys 路径后,我在上面的异常之前收到以下错误。该错误表明在 dll 中找不到过程入口点。
我知道这是一个老问题,但谷歌喜欢它。我也遇到过类似的问题。有很多指南很容易通过谷歌搜索,但很难使其发挥作用。这是截至 2022 年 4 月对我有帮助的指南:
第 1 步:安装 MSYS2 下载 MSYS2 安装程序并按照安装说明进行操作。
步骤 2:安装 GTK+3打开 MSYS2 shell,然后运行:pacman -S mingw-w64-x86_64-gtk3
步骤3:修改PATH变量以便可以找到该库
Open up Control Panel
Go to System and Security > System
Click on the Advanced system settings link
Click on Environment Variables... button
Under System variables find the Path variable and select it
Click the Edit button
Add either ;C:\msys64\mingw64\bin or ;C:\msys32\mingw32\bin to the end of the variable, depending on your system architecture
Click OK, and you are done
Restart your system for the changes to apply
Run Code Online (Sandbox Code Playgroud)
要运行 GtkSharp Samples 项目,您还需要:
pacman -S mingw-w64-x86_64-gtksourceview4
否则它会抛出...System.DllNotFoundException: GtkSource: libgtksourceview-4-0.dll...
然后您可以从此处或 nuget 安装 GtkSharp。
| 归档时间: |
|
| 查看次数: |
8346 次 |
| 最近记录: |