I´m trying to create a multi-project-solution-template with VSIX for my Xamarin solutions but I just can´t get the template to work when I add a PCL to it.
I´m using this Git project (see How to create a multi project template) as a boiler plate and I can easily add ordinary projects (e.g class libraries) to it and get them to work just fine.
I´m guessing it is not working because I probably need to add some Dependencies in the vsixmanifest file to e.g ASP.NET Core 1.0 or more of the stuff the PCL is referring to.
But I have not been able to find out what to add to get it to work.. and of course this could be something totally different...
I just tried to change the PCL to the new .net Standard by using this guide ".NET Standard Library with Xamarin Forms" and it works like a charm. But with Xamarin.Forms 2.3.1.114 I needed to go for netstandard 1.4.
So my project.json file looks like this
{
"supports": {},
"dependencies": {
"Xamarin.Forms": "2.3.1.114",
"NETStandard.Library": "1.6.0",
"Microsoft.NETCore.Portable.Compatibility": "1.0.1"
},
"frameworks": {
"netstandard1.4": {
"imports": [
"portable-net45+win8+wpa81+wp8"
]
}
}
}
This ".NET Standard with Xamarin Forms Gotchas" might also help you as it did me.
Related
The official documentation explains how to migrate an existing Angular Web project to a code-sharing structure.
But I could not find documentation on how to do the other way around. That is, how to migrate an existing NativeScript Mobile project to a code-sharing structure.
Any thoughts on how to convert an existing mobile app project to a web app project?
I have answered this before as well, here are the steps that I followed to do the same.
It is actually very time-saving to use same code base for both Web and mobile. Here are the steps I would suggest based on my experience.
You should be using #angular/cli#6.1.0 or newer. npm i -g #angular/cli
Installl nativescript-schematics. npm i -g #nativescript/schematics
Create a new Project. ng new --collection=#nativescript/schematics my-mobile-app (I did it in this way and then copied over src/app folder here from Mobile app).
Copy the app/src folder from existing project. (You may want to look for source folder in nsconfig.json "appPath": "app")
Find the .ts file where you are using mobile specific components and create a wrapper class for the same. E.g. I was using Fancy Alerts for mobile apps, so I created a wrapper helper class like helper.tns.ts and helper.ts
in helper.ts
public show() {
alert('Javascript+HTML alert') .
}
in helper.tns.ts
public show() {
TNSFancyAlert.showWarning('Warning!', 'Message', `Ok`, 0, 300).then(() => {
resolve('Success');
});
}
Rename all .html to .tns.html and create web specific html files.
Build a web app
ng serve
Build a Mobile app
tns run android --bundle
tns run ios --bundle
P.S. --bundle is the key here to compile mobile specific files only. The HTML code that defines the View of the component should be different between a web and a mobile app.
By reference to this link https://developer.xamarin.com/api/namespace/System.Security.Cryptography/
May I know how do I include this in my Xamarim.Forms PCL project? When I include in, Visual Studio is giving error as the picture below
May I know if anybody has any idea how to solve this? Thanks.
The namespace is available both in Xamarin.iOS and Xamarin.Android. You could make platform specific implementations for both platforms and then resolve them with the DependencyService
You would have your interface for whatever you need in your PCL
public interface ICryptoService // or whatever
{
string Cipher(string stringToCipher);
string Decipher(string stringToDecipher);
}
and then implement these in your platform specific projects
using System.Security.Cryptography;
namespace MyApp.Droid
{
public class CryptoService : ICryptoService
{
// implement interface
}
}
To make the implementation visible to DependencyService you have to use the DependencyAttribute
[assembly: Xamarin.Forms.Dependency(typeof(MyApp.Droid.CryptoService)]
You can now obtain an instance in your PCL with
var cryptoService = DependencyService.Get<ICryptoService>();
and then use it. The steps for iOS are basically the same. For UWP you have to register the implementation manually, see here.
Edit:
Since it's likely that the implementation will be the same for all platforms, you could introduce a shared project and put the implementation there. All you have to do now is referencing the shared project from your iOS and Android projects.
Edit 2:
Adding a shared project to an existing Xamarin.Forms solution is quite easy. Just right-click your solution in VS, choose Add -> New Project... (I only have a german localized VS at hand at the moment, but it should be something in the lines of that). Now select Shared Project, give it a name and click OK, there will be a new shared project in your solution.
Now right-click your platform specific project and choose Add -> Reference.... The window to add a reference should open and on the left you can choose the source of the reference (Assemblys, Projects, Shared Projects, COM and Browse). Select Shared Project and then the project you just created. Any code file in your shared project will now be compiled with your platform specific project as if the code file was in the platform specific project (watch for namespaces!). Repeat for the other platform specific projects.
I've got an iOS specific code (class definition) and would like to include in a separate ios specific file.
So I created two files
utils2.ios.ts <-- added my code here
utils2.android.ts
And then from another file, I did:
import utils2 = require("./utils2");
But I'm getting an error message that the module is not found.
I had to go this route because I've got a class definition in
the code and if I have just one file, I get a run-time error on Android.
Does typescript support platform specific files?
Or another option - is something like this possible in ts file
if ios
do this
end
Similar to C preprocesser
You can get by with just a utils.ts file and inside it you can do platform specific logic that you mention. Your issue about the module not found is likely just a path issue to that module, not that the module isn't actually there. Also note that Typescript doesn't really "support" platform specific files. Nativescript takes those .android.ts and .ios.ts files and at runtime loads the file you need on the platform running. So just FYI that's not really TS related :)
Nativescript provides a platform module in the tns-core-modules. Import the { isAndroid } or { isIOS } from the platform module in your code and use it as the
if (isAndroid) {
// do android only here
} else {
// ios
}
I created a Xamarin Forms app.
And inside a new page with a label named "MyLabel".
In the code behind for my page I have
private void SetUpUI()
{
#if __IOS__
this.MyLabel.BackgroundColor = Color.Navy;
#endif
}
In my iOS project options I can see symbol __IOS__ in the "Compiler" tab. (please see screenshot)
When I run in iOS it doesn't make the label blue:
But if I remove #if __IOS__ block it makes the label blue:
So it seems conditional compilation is not working.
I'm on a Mac. So couldn't test on Visual Studio.
Stuck with it for a long time but cannot figure out what I missed.
The answer of SushiHangover is correct: your PCL project won't have the compiler definitions for the platforms.
However, the solution he provides has become obsolete since Xamarin Forms 2.3.4 was released. Device.OnPlatform has been redesigned as discussed in this discussion and implemented in this Pull Request.
The correct way to do this in Xamarin Forms 2.3.4 and onwards is by using Device.RuntimePlatform. Use a switch or conditional to suit your needs like so:
if(Device.RuntimePlatform == Device.iOS)
{
// iOS
}
else if(Device.RuntimePlatform == Device.Android)
{
// Android
}
It would be possible to do it like you asked, if you were to use a shared project instead of a PCL. Because when you use a shared project, you have access to the compiler directives of your platform projects.
You are using the conditionals in your PCL project which would not contain those compiler defines, thus why your conditional code is greyed out.
In your PCL project you can use Device.OnPlatform to perform platform based processing:
Device.OnPlatform (iOS: () => this.MyLabel.BackgroundColor = Color.Navy; );
re: https://developer.xamarin.com/api/member/Xamarin.Forms.Device.OnPlatform/
I am writing a Xamarin Forms PCL application (both iOS and Android)
I have to pick an image from phone gallery.
I have read some documentation about this plugin:
Labs.Platform.Services.Media.MediaPicker
In my Common project, i have to put this code:
var device = Resolver.Resolve<IDevice>();
picker = DependencyService.Get<IMediaPicker>() ?? device.MediaPicker;
But I have an error on the first line: IDevice and Resolver Objects are not know. I think I am missing a reference or a using clause.
Thanks
You need to install the following Nuget package in all of your projects in the solution (i.e. PCL core project, iOS project, and the Android Project, and UWP project if using it):
https://www.nuget.org/packages/XLabs.Forms/
And you will need to add the following using statements:
using XLabs.Ioc;
using XLabs.Platform.Device;
using XLabs.Platform.Services.Media;