Running code based on application platform - c#

Trying to make a cross-platform application library that will be able to work on UWP, Winforms, Wpf, Android and iOS.
Using NetStandard2.0
I've been using Xamarin Forms to test the library because of the cross-platform option but something I want to do is to get hardware information and emulate the devices such as Audio output.
I can't find a way that works across all platforms/devices only ones that target specific. And I can't find a way to run specific code or use a specific library based on the platform that is running.
Targeting multiple frameworks don't work because they cause errors in all the other files.
Tried creating separate libraries that are specifically targeting a platform like UWP, Android and iOS separately but when referencing them, I get a not supported errors:
Project ****.UWP is not compatible with netstandard2.0 (.NETStandard,Version=v2.0).
Project ****.UWP supports: uap10.0.17763 (UAP,Version=v10.0.17763)
stuff like that and for each project (UWP, Android and iOS)
What can I do?

Targeting multiple frameworks don't work because they cause errors in all the other files. Tried creating separate libraries that are specifically targeting a platform like UWP,
The platform specific code can't be used into share library directly. for calling native api for each platforms. the better way is using DependencyService to approach. And here is official document that you could refer to.
And the other way is using Conditional Compilation
For example
public static string DatabaseFilePath
{
get
{
var filename = "TodoDatabase.db3";
#if SILVERLIGHT
// Windows Phone 8
var path = filename;
#else
#if __ANDROID__
string libraryPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
#else
#if __IOS__
// we need to put in /Library/ on iOS5.1 to meet Apple's iCloud terms
// (they don't want non-user-generated data in Documents)
string documentsPath = Environment.GetFolderPath (Environment.SpecialFolder.Personal); // Documents folder
string libraryPath = Path.Combine (documentsPath, "..", "Library");
#else
// UWP
string libraryPath = Windows.Storage.ApplicationData.Current.LocalFolder.Path;
#endif
#endif
var path = Path.Combine(libraryPath, filename);
#endif
return path;
}
}

Related

Preventing mobile projects from using certain .cs files in a Shared Project

I am making a mobile app. In my Solution I have a shared project. Some of the files I do not want to be used in my iOS or Android builds.
My code looks something like this:
#if __MOBILE__
#else
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using Azure.Storage.Queues;
using Azure.Storage.Queues.Models;
using System;
namespace Blah.Utils
{
public static class AzureBlobService
{
public static async Task<string> CreateCloudQueue(string connectionString, string queueName)
{
// blah
}
}
}
#endif
This doesn't seem to be removing this file for my mobile apps. It still wants me to get those
nuget Azure.Storage packages. How can I get that #if to work the way I want it to?
Well after struggling with this for a long time and thinking it must be some obscure setting somewhere...it was because my shared library was being referenced by a project in between my mobile app project.
So in other words the projects were referencing each other like this:
Sharedproject <--- CoreLib proj <--- Mobile app proj
And I wasn't putting MOBILE in the 'Conditional Compilation Symbol' in the CoreLib proj properties.
ugh

How to add conditional compilation symbols regarding OS platforms in .Net Standard projects

I have created Xamarin.Android and Xamarin.iOS apps that have a few shared projects.
In the shared project I use the following code:
IDataStorage dataStorage = null;
#if __ANDROID__
dataStorage = new DataStorage(Size.SmallData, 10);
#endif
#if __IOS__
dataStorage = new DataStorage(Size.BigData, 20);
#endif
if (dataStorage == null)
throw new InvalidOperationException("Data storage is not available.");
Whenever I run this code, I get an exception (obviously).
So I tried to add those compilation symbols in the Conditional compilation symbols part of the project properties, but I realized that if I add both symbols, both section will be compiled.
Unfortunately, the Platform list contains only one item: Active (Any CPU).
So I tried to separate the platforms.
If I try to create a new project platform, I can only choose x86 or x64.
If I create a custom platform, I can't set it as project platform as it is not supported by any project.
I can't set it as well, as it does not appear in the list.
So, what am I doing wrong?
How can I use conditional symbols in a .Net Standard project regarding to OS platforms?
A .NET Standard library doesn't know at compile time that it is going to be used by Android, iOS, or anything else. That's why you can't use conditional compilation symbols.
If you are using Xamarin Forms you can use the Device class to make runtime decisions based on the platform that is actually running.

Getting an embedded resource from a shared project

I'm working on a Xamarin Forms app which has two projects, an Android App and an iOS app. All the other code is stored in shared projects.
Solution looks like this:
MyApp.Android (Android project), references MyApp.Base
MyApp.iOS (iOS project), references MyApp.Base
MyApp.Base (Shared project)
I am using the following code to read an SVG image from the shared project:
using (var stream = GetType().Assembly.GetManifestResourceStream("MyApp.Base.image.svg"))
{
// do work here...
}
This works perfectly when the image is in the Android or iOS project, but I want the image to be shared so I put it in the shared project.
GetType().Assembly return MyApp.Android, thus it cannot find the image. I suppose I'm overlooking something but I haven't been able to find a solution.
Can anyone point me in the right direction? Thanks!
You are using the Shared Project NOT the PCL or .Net Standard library. So, all of your Shared Project contents will get merged into the Platform specific project when you Compile them. That means - even if you Embed your resource in your MyApp.Base that will get merged into .iOS/.Droid project.
I suggest you to learn more about Shared Project vs PCL or .Net Standard library.
Below code; I did't tested but this is the direction that you should follow:
#if __IOS__
var resourcePrefix = "MyApp.iOS.";
#endif
#if __ANDROID__
var resourcePrefix = "MyApp.Android.";
#endif
var assembly = IntrospectionExtensions.GetTypeInfo(typeof(SharedPage)).Assembly;
Stream stream = assembly.GetManifestResourceStream
(resourcePrefix + "image.svg");
For more information please look into this page : https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/files?tabs=macos#embedding-in-shared-projects

How to add Text to Speech to Unity project?

I am looking for System.speech to work in unity? Is there any way how to include this DLL in unity and MonoDevelop?
Because I'm trying to make a sound text to speech without spend the money from asset store. If System.Speech Library DLL could handle this why not. Just how to make it work with unity 5.3.5 ?
Also I have already try speechLib.dll. It is work while in editor but when Build to APK it is error and can't build.
Dlls files don't work on Android or iOS unless it is an unmanaged dll file without Windows specified API. If it is a Windows API or a managed dll then it won't work on Android or iOS.
You have two options: Buy a plugin or make your own. If you are only targeting Android and iOS then go for this Easy TTS which cost $5.
If you want to make one yourself then the process is very similar to my Speech to Text solution. The only difference are the classes used. Making one your self is easy. The only downside is that it is time consuming to make one for each platform.
Android:
TextToSpeech class.
iOS:
AVSpeechSynthesizer class
MacOS:
NSSpeechSynthesizer class
Windows:
ISpVoice class
There are tons of examples of how to use these on the internet. You have to make plugin for the Android class using Java, Objective-C for the iOS and MacOs classes. C++ for the Windows class.
For putting them together, you should use Unity's directive to do that.
class TextToSpeech
{
#if UNITY_ANDROID
Use TextToSpeech class
#endif
#if UNITY_IOS
Use AVSpeechSynthesizer class
#endif
#if UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX
Use NSSpeechSynthesizer class
#endif
#if UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN
Use ISpVoice class
#endif
}

SQLite in Xamarin Forms Android

When I try to create a SQLite Database in the Xamarin Android Project, I get the following Exception.
System.Exception: Something went wrong in the build configuration.
This is the bait assembly, which is for referencing by portable
libraries, and should never end up part of the app. Reference the
appropriate platform assembly instead.
One thing I found already out is that I need SQLite.Net.SQLitePlatform for Android. I dont have the SQLite.Net Namespace.
I used the SQLite.net PCL NuGet package
https://www.nuget.org/packages/sqlite-net-pcl/
I am creating the DB Path in the MainActivity.cs of the Droid Project
var sqliteFilename = "timesheet.db3";
string libraryPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
var path = Path.Combine(libraryPath, sqliteFilename);
In the Forms app I am trying to create the Database
database = new SQLiteConnection(databasePath);
database.CreateTable<TimeRecordEntity>();

Categories

Resources