I'm trying to create and use de Portable library for a cross platform Xamarin.Forms. But the preprocessor for the platform doesn't seem set when I break in the code.
#if WINDOWS_PHONE || __ANDROID__
Debug.WriteLine("Passed");
#endif
Using step by step I can see all the code between the preprocessor condition be skipped (tried on Android and Windows Phone). But when I try the same thing in the start project it works.
In Visual Studio the code should be highlighted when active but it's not in the PCL.
The preprocessors defined in the startup project should be shared with the library, no ?
PCL doesn't work this way.
Try this:
using Xamarin.Forms;
...
if(Device.OS == TargetPlatform.Android)
Debug.WriteLine("I'm on Android");
else if(Device.OS == TargetPlatform.iOS)
Debug.WriteLine("This is iOS");
Here (https://docs.xamarin.com/guides/cross-platform/application_fundamentals/building_cross_platform_applications/sharing_code_options/)
you can find the appropriate Xamarin article. What you are trying to do is
Shared Assets approach and not PCL approach.
Related
I'm trying to set up a basic UWP project in Unity and use parts of the Windows namespace.*
However, no matter which part of the Windows namespace I try to import in my script, I always get
CS0246: The type or namespace name 'Windows' could not be found (are you missing a using directive or an assembly reference?)
Build support both for Windows and UWP is set up in Visual Studio and Unity and Windows 10 SDK (10.0.19041.0) is installed. Any other ideas how to fix this?
Based on https://forum.unity.com/threads/windows-storage-is-missing-when-building-for-uwp-hololens.541415/ I would have assumed that Windows is available in Unity for UWP as they were able to fix it there?
Thanks
*In the long run, I am trying to use 'Windows.Devices.Input.Preview' to access Gaze Input in a project that also uses Vuforia and thus needs to be UWP. Other tips on how to combine Gaze Tracking and Marker recognition in Unity are also welcome.
The Windows namespace in Unity is made for Windows PC build only. It won't work for other systems. On the top of the script try to add a
#if UNITY_STANDALONE_WIN
You should add the whole declaration like
#if UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN
Use SpeechRecognitionEngine class
#endif
If you are building for android or ios you will need also to add:
#if UNITY_ANDROID
Use SpeechRecognizer class
#endif
#if UNITY_IOS
Use SFSpeechRecognizer class
#endif
I am using a multi-targeting library in my Xamarin forms application. The library has come code written with __ANDROID__ conditional compiler symbol. When i run the application on Android, it does not execute the code written with the symbol.
The multi-targeting library project is using MSBuild.Sdk.Extras.
<Project Sdk="MSBuild.Sdk.Extras" ToolsVersion="16.0">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;MonoAndroid90</TargetFrameworks>
</PropertyGroup>
I also tried it by defining conditional symbol in Xamarin.Android Project Properties --> Build --> General --> Conditional compilation symbols. But it still does not run that particular code.
#if __ANDROID__
// do some andoid specific work
#endif
It should run this code block when running the application on Android.
Seems like you have defined __ANDROID__ symbol in the wrong project. If you define it in e.g. Project.Android it's only defined in this project, not the other ones.
There is a possibility to define solution level conditional symbols and you can read about it here
Probably you forgot to add inside the 'conditional compilation symbols' field inside the build section of the Android project that symbol: ANDROID
I don't know why on iOS when the project is created, the symbol is automatically inserted, while in the Android project it is not inserted.
Here the symbols are not preset.
Here the symbols are preset.
You can find more info here under Conditional Compilation section:
microsoft xamarin docs
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.
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
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
}