I have an existing application written in C# for .NET 6 that renders OpenGL with the lightweight OpenTK (v4) OpenGL bindings, which support .NET 5+, Open GL 3+, and OpenGL ES 2+.
According to the OpenTK FAQ:
OpenTK runs on Windows, Linux, and MacOS X.
(Older versions of OpenTK supported Android and iOS as well; current OpenTK might work on these, but it’s untested.)
I am excited by .NET 6's promise of one unified platform, but I have only seen cross-platform mobile projects that use Microsoft's new .NET Multi-platform App UI (.NET MAUI) framework. MAUI appears to be a web view shell that contains a Blazor Web App. I found the code for this shell in the src/Core/AndroidNative folder of the repository, but it seems very bloated for my purposes. I want to be able to access Android-specific functions without the bloat of Blazor and a web view. According to this blog post, Xamarin.Android might be the answer, but I am not sure how to introduce it into my existing project.
All I need is a simple shell that:
Creates an Activity with a GLSurfaceView
Obtains an OpenGL ES 2 context
Runs my current C# code with that context
I would prefer the shell to be in C#, since introducing Java code would complicate my build process and fragment my code base.
How can I introduce the needed Android-specific code?
How will this change my build process?
I know that I need to write OpenGL ES-compatible shaders and that I probably need to change how I save files. What other changes will I need to make to my current code to ensure Android compatibility?
Related
I've been 2 years with Xamarin Environment and mostly I used Xamarin.Forms. But yet, I have no idea what Xamarin Cross Platform is? Can anyone provide some structural differences?
When we talk about Xamarin there are two approaches in development of native apps:
Traditional Xamarin approach
Xamarin.Forms
There is a good quote from Xamarin web site
Share code everywhere. Use the same language, APIs and data structures
to share an average of 75% of app code across all mobile development
platforms. Build user interfaces with Xamarin.Forms and share nearly
100%.
Important note is that these numbers can vary from project to project so this is some assumption for general usage of Xamarin.
And as you can see from the image below about "differences" between these two approaches. Traditional Xamarin approach (Xamarin.iOS and Xamarin.Android) they’re both built on top of Mono, an open-source version of the .NET Framework. To develop those apps you use C# and there is an opportunity to share up to 75% of the codebase as you can see on the image bellow and from a quote from Xamarin.
Using Xamarin Traditional approach you can use a C# as a programming language to write your models, call web services, etc. So you are writing that code logic once and using/share it across the Xamarin.Android and Xamarin.iOS projects, and in those separate projects you are writing code that is specific for that platform and you have access to their APIs.
Also, there is a quote from Xamarin about the traditional approach:
Anything you can do in Objective-C, Swift, or Java can be done in C#
with Xamarin using Visual Studio.
And project structure with Xamarin traditional looks like this:
Shared code project: Codebase that is common and reusable for your other projects.
Xamarin.Android: Project for your Android app where you are writing code using Android-specific APIs, writing your UI code, etc.
Xamarin.iOS: Project for your iOS app where you are writing code
using iOS specific APIs, writing your UI code, etc.
More info about Xamarin traditional here.
Now about Xamarin.Forms, the best definition is quoted from the Xamarin website:
Xamarin.Forms is a framework that allows developers to rapidly create
cross-platform user interfaces. It provides it's own abstraction for
the user interface that will be rendered using native controls on iOS,
Android, Windows, or Windows Phone. This means that applications can
share a large portion of their user interface code and still retain
the native look and feel of the target platform.
As you can conclude from the text above, Xamarin.Forms is a framework that allows you to write your UI using C# or with XAML with the opportunity to use MVVM... With Xamarin.Forms you can write your codebase and your UI which will be shared across the platforms.
Note: this is a very simple project structure, of course, you can add more project for different layers
Shared code project: Holds your common code base and also because you are using Xamarin.Forms you can write code for your UI
Xamarin.iOS and Xamarin.Android: Code for that specific platform, and some advanced topics such as Custom renderers and Dependency services.
Of course, there is some of the limitations if you are using Xamarin.Forms for example out of the box you can only use UI controls that are common across all platforms. You can read my blog post about Xamarin.Forms pros and cons here.
More info about Xamarin.Forms here.
I hope that this answer is not confusing for you and as a conclusion in one or two sentences... Using Xamarin Traditional you can share your code logic between platform-specific projects, and using Xamarin.Forms you can share code logic and also code for UI across your projects.
Note: For shared project and code sharing strategy you can use "Shared Project" and "PCL" this is a topic for another question... so in this answer, I was simplifying this and use shared project term for that type of project in Xamarin app solution.
UPDATE, Summer of 2020:
.NET Maui is announced in May of 2020, the question about the difference between MAUI and Xamarin.Forms could be found in the StackOverflow thread here: What is MAUI? and what are differences between MAUI and Xamarin
If you're talking about the Xamarin.iOS and Xamarin.Android (Xamarin Native), I'd say the main difference is that in Xamarin Forms, you share NOT ONLY the business logic, but also the UI. In Xamarin forms, you'll use basic components on all platforms (Button, Label, etc). That's the reason why it's called Xamarin.Forms, because it's suitable for basic forms.
But for Xamarin Native, you'll create a UI per platform (One for iOS, one for Android). You can do much more with Xamarin Native, because you'll use native components.
Hope it helps!
Xamarin has two ways of building mobile applications, Xamarin.Forms and Xamarin.Native.
Both ways let you write code cross platform.
Cross platform is just a concept. It means the code you write once can run in multiple platforms. Xamarin is cross platform (mainly Xamarin.Forms) because it allows you to write code that runs on multiple (across) platforms.
I think Xamarin Cross Platform refers to the technology that compiles shared code (your PCL or Shared Project) such that it can be used by the different platforms such as Xamarin.Android or Xamarin.IOS before finally being compiled to the native platforms.
So I'd say in a way Xamarin.Forms is one of the tools you use to build cross platform compatible code that can then be put together using the Xamarin Cross Platform Technology, makes sense?
To be clear, I follow the MVVM pattern, and I want to structure my project such that I can share my model code between a UWP app and a standard WPF app. The code I want to share has no UI. I don't relish the thought of finding new tools to replace the ones that I've been using for years that take care of certain tasks like logging, connecting to a document oriented database, etc.
I attempted to start writing a UWP wrapper around some code I already have and reference the model project directly. Visual Studio refused to let that happen, showing me an error message that says "Unable to add a reference to project 'ACK.Model'". The same thing happened when I attempted to put the model in a Universal Library and reference it from a WPF app. I'm not trying to share WPF code. Just the model layer that has no reference to UI libraries.
This is a scary proposition, because it means that if I want to do anything substantial I have to choose to either jump 100% to UWP or stay 100% WPF. NewtonSoft.JSON might have a universal distribution (ASP.NET MVC), but what about ElasticSearch.NET, and other tools needed to make important apps?
I found where the "Portable Class Library" project type was hiding. PCLs will allow me to share my code across WPF and Universal apps as that was one of the options. This solves the simple case of the Model part of my code, but I (still) can't use some of the libraries I want. There are still a large number of libraries that I need that do not have PCL available.
About a year later, with the advent of Visual Studio 2017 there is a more complete solution. If you target your libraries to .Net Standard then the library is compatible with both .Net Core apps and the monolithic .Net targeted app. The support for standard .Net libraries and APIs is fairly complete, as is the support for modern C# language features.
The general advice now is this:
Target .Net Standard for all libraries
Target the appropriate platform for your actual application. (UWP or WPF).
NOTE: if your library has to interact with C libraries or applications, you have to take extra care to make sure you load the correct version.
It appears that there is a solution, but it has to be adopted by the whole tool chain you want to use. When Microsoft introduced Windows Store apps in Windows 8, they also introduced a Portable Class Library (PCL). The purpose of the PCL is to share code between different parts of your application.
When you create a PCL in Visual Studio 2015, you can specify the types of APIs you want it to be accessible from:
Universal Apps
Mono
.Net Core 5
.Net 4.6
This of course, limits the APIs available to you but most of the ones you want to use are OK as long as it's not UI related. There are other limitations as well:
Your project can only be edited in Visual Studio 2015 or greater
You don't have access to special directories from the Environment variable (i.e. user Documents directory, etc.)
You can't link to a library designed for only one of your target platforms (i.e. libgit2sharp, etc.)
There's no way to browse the API for this subset--MSDN needs to get on the stick. MSDN has updated much of the API documentation, but it's still difficult to figure out what applies to your PCL
However, you can link any library designed for a single target platform to your PCL. It's not ideal, but it's better than nothing.
The ASP.NET MVC stack has been ported to using PCLs, so you can use NewtonSoft.JSON directly as well as any other of those libraries used by that application. However, there are several libraries that have not been ported.
This arrangement forces you to think about how you want to integrate better. The .Net Core 5 seems to be stable, but support is in it's infancy. The current generation of Universal Apps as of VS 2015 update 1 uses .Net Core 5 directly.
There are several features from Nuget that are not currently supported even though work is under way:
MS Build extensions (major changes to MSBuild and the project.json structure)
Install/uninstall scripts (related to the removal of the concept of install)
Content (related to install/uninstall, but work is in progress on this)
Content transforms (related to lack of install/uninstall)
I wish I had a more complete answer. But this is as far as I got once I discovered the PCL and how it evolved for the current infrastructure.
I'm in the process of creating a game creation toolkit that incorporates version control right off the bat. I want to be able to deploy a game as a Windows 10 app, or as a standard WPF app, but due to the libraries I'm using to integrate version control I need to create the editor as a standard WPF app. I had to be a bit creative in building the shared code and importing the correct libraries.
First, my project hierarchy:
Project.Model (Portable Class Library)
Project.Model.Versioning (standard C# library)
Mvvm.Toolkit (Portable Class Library)
Editor (Standard WPF application)
I want the core PCL to be able to load a project and deserialize the JSON encoded objects. The PCL did have access to System.IO, but surprisingly it is not the same as the one defined in the standard C# library. Here's how I had to fix things:
After adding the package reference to NewtonSoft.JSON, I had to change the target framework in the packages.config file:
<package id="Newtonsoft.Json" version="8.0.2" targetFramework="portable-net452+win81" />
All projects dependent on my Project.Model class had to install the `system.io.filesystem' package from nuget so that the System.IO.FileInfo etc. objects were the same.
While this is definitely not a panacea, it's also not a dead end. I'm sure there are more gotchas, but this will at least help with some of the problems.
a .NET Standard library could be used to share a Model-View-ViewModel architecture between a WPF project and a UWP project.
https://www.syncfusion.com/ebooks/xamarin-forms-succinctly/sharing-code-among-platforms
https://devblogs.microsoft.com/dotnet/announcing-uwp-support-for-net-standard-2-0/
We're developing very similar applications for Windows and Android using Xamarin platform and C#. Both will incorporate some on-screen and off-screen 3D rendering, such as 3D mesh visualization.
We'd like to implement this as cross-platform as possible. How is this usually done in Xamarin? Is it better to do rendering in C++ code, or is there a C# wrapper with abstractions over platform-specific context creation etc.?
After some time I can answer my own question.
At least in my case it turned out to be more convenient to use OpenGL from C++. We just built a shared library from C++ code and link it to .NET dll's using DllImport.
Our tool for building C++ libraries is CMake, however you can use other build systems, such as new features of VS2015.
One particular benefit of this solution is that you can reuse this code anywhere else: in iOS application, desktop application for Windows, Mac, Linux, etc.
why can't I use regular class libraries in XBox360 games?
I have application logic which I want to keep independent from XNA and use in both WPF and XNA applications.
Does anyone know good practice to share code between XBox/Phone7 applications and "regular" windows applications?
Have a look at Portable Class Libraries: http://msdn.microsoft.com/en-us/library/gg597391.aspx
Using the Portable Class Library
project, you can build portable
assemblies that work without
modification on the .NET Framework,
Silverlight, Windows Phone 7, or XNA
(Xbox) platforms. Without the Portable
Class Library project, you must target
a single platform and then manually
rework the class library for other
platforms. The Portable Class Library
project supports a subset of
assemblies from these platforms, and
provides a Visual Studio template that
makes it possible to build assemblies
that run without modification on these
platforms.
Edit: Check out JoDG's answer for a nicer solution.
You could try linked files...
In your Xbox project, right-click -> Add Existing File, then after selecting the file(s), click the little drop-down arrow on the "Add" button and click "Add As Link", as shown here:
This might not work if you need to share more than just files, but it's an easy way to share code.
JoDG's answer is probably the most practically useful. But just to provide additional information:
why can't I use regular class libraries in XBox360 games?
Because the different XNA platforms target different versions of the .NET framework.
XNA Game Studio itself provides a mechanism (documented here and more info here) for automatically mirroring the source files of a project between two projects. This mechanism can be used for creating copies of your library projects for each platform, as well as for your game projects.
For XNA-related work, this is the preferred method for creating cross-platform libraries. When you have to also make your library work on WPF you have to take additional steps:
On Windows, an XNA game is just like any other .NET application, and a XNA library for Windows is just like any other .NET library. Except for the fact that they reference XNA assemblies. So your WPF application can reference a Windows version of your XNA library. But if you want your application to work on systems without XNA installed, you need to remove the XNA assembly references from the Windows project for that XNA library.
XNA Game Studio will still mirror changes between the Windows library and the Xbox 360 library for you.
I heard about Mono project but only in regards to winforms. Will it work with WPF?
WPF under Mono does not exist.
At this point, no group in the Mono project has plans to implement Windows Presentation Foundation APIs as part of the project.
The mono team propose using Silverlight/Moonlight instead:
Silverlight implement a subset of the WPF APIs and is available on Windows, MacOS X and through our own open source effort Moonlight it is available on Linux and other Unix systems.
There is a library called Silverform SDK that aims to provide cross-platform WPF and Silverlight implementation.
The library is implemented in managed code and currently works with OpenTK and Unity3D as render backends. Major functionality, such as binding, layout, main controls and primitives, has already been implemented (check Unity web player demos here). Initially it has been focused on Unity3d render, while support for standalone Mono applications will be added as a separate build in the future.
Disclaimer: I am one of the developers of the library.