Xamarin Android Layout Resources in library project not included in Application - xamarin

I have a Xamarin Android library project which is referenced by an Android application project.
The library project has layout files which need to be used by a library project component. The Resource.designer.cs file seems to be generated properly. The layouts are marked as Android Resources. Yet at runtime the resources are not there. Trying to access them just returns 0.
After reading all the SO questions on the topic, I am convinced this should work, but so far it doesn't. Ideas?

Create the Android Class Library.
Create a layout folder in this class library and create a layout in this folder.
Add the code in your xamarin android app.
var btn_Load = FindViewById<Button>(Resource.Id.btn_LoadLibraryLayout);
btn_Load.Click += delegate
{
SetContentView(ClassLibrary1.Resource.Layout.library_layout);
};
I create a button to open the layout in class library.
Result:
You could download from the ClassLibrary_layout folder of GitHub for reference.
https://github.com/WendyZang/Test.git
Updated:
If you want to get the resource identifier, you need the package name of this class library. First, we could not get the package name in AndroidManifest.xml file or properties, because the class library does not have it.
You could get it from the steps below.
Create a activity in class library. And get the package name with the code.
var PackageNname = Application.Context.PackageName;
You could also get the resource identifier in this activity.
var resId2 = Resources.GetIdentifier("library_layout", "layout", PackageNname);
Result:
The paskage name is same to the app1 I provided.
Package Name: com.companyname.app1
You could also use the code directly in app1 activity.
int resId2 = Resources.GetIdentifier("library_layout", "layout", "com.companyname.app1");

Related

NativeScript: use custom objective-c imported class into Xcode project in NativeScript

basically I have some Objective-c class already existing and I want to use them in my NativeScript projet. Currently I have added those files to the Xcode project target and I want to be able to call my objective-c code from nativeScript js. I've read the doc but I don't understand it. It seems so complicated. basically currently all I want to do is be able to present my custom view controller by calling probably something along
const vc = MyCustomViewController.alloc.initWithNibName("xib file")
page.frame.ios.controller.present(vc,true, nil)
Am I obligated to create a plugin for that? Am I obligated to use my objective-c class to build a framework in Xcode and then import the framework?
So I found out.
Actually what you need to do is first to compile your native iOS code into a framework. As per the documentation all your classes must inherit NSObject and all your function must be marked with #objc to be exposed to the objective-c Nativescript side if you write in swift. You will notice as well that in a framework your bundle is not the main bundle. In this example you can see how you can retrieve the bundle from the framework and load a xib from it.
Then you need to add your framework file to a Nativescript plugin. For that, you want to add the framework to the plugin's iOS folder yourPlugin/platform/iOS/yourFramework.framework.
then, you need to add your plugin to your app. You can add your local plugin by using the next command line. Notice the path end with the /src folder.
tns plugin add /path/to/yourplugin/src
Now, you can then call your native functions and classes without even importing them. Of course this works only on iOS. If you run your app on android, calling those methods will crash.
To show this viewController on your Nativescript side you will need to call the following code. By the way You can find documentation elsewhere to get a reference to the current page or frame object.
const controller = page.frame.ios.controller
const vc = IOMediaViewController.create()
vc.modalPresentationStyle = UIModalPresentationFullScreen
controller.presentViewControllerAnimatedCompletion(vc,true,null)

How to use System.Security.Cryptography in Xamarin.Forms PCL

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.

Nativescript - platform specific code

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
}

Appcelerator Hyperloop - using 3rd party swift library

I'm trying to use Hyperloop in order to use a third-party open source library. The library I want to use is Sweet Alert iOS.
The library has a swift file. I have put this file inside an src directory inside my project's main folder (does it have to be inside src folder? can I use other folder in my project? what about sub-directories?).
According to the Titanium documentation:
Any *.swift files found in your src directories will automatically be compiled
In my app when I do something like this:
var UIView = require('UIKit/UIView');
Ti.API.info('UIView => ' + UIView);
It works. But if I try to require any of the classes in the swift file I get an error that it can't find architecture x86_64 module.
var sweetAlert = require('SweetAlert');
var cancelAnimatedView = require('CancelAnimatedView');
When looking at the SweetAlert source code, I also notice that SweetAlert class is open while other classes are just regular classes:
open class SweetAlert
I'm not a swift developer so I don't really know what it means.
What am I missing here?
EDIT
Some progress... So I found that I need to have an appc.js file that's defining the src frame work and a name for MyFramework. And that the require I do should match the filename of the swift file. Now when I compile I see some more interesting output about the swift compilation, but getting an error:
[INFO] Generating metabase for swift MyFramework /Users/ophir/Documents/Appcelerator_Studio_Workspace/HyperloopTest1/src/SweetAlert.swift
2017-06-29T14:43:44.061Z | ERROR | An uncaught exception was thrown!
Cannot read property '1' of null
2017-06-29T14:43:44.064Z | ERROR | Cannot read property '1' of null
EDIT 2
I've cleared all the script in the swift file, and narrowed it down to the following failing script:
import Foundation
import UIKit
import QuartzCore
public enum AlertStyle {
case success,error,warning,none
case customImag(imageFile:String)
}
class SweetAlert: UIViewController {
}
class AnimatableView: UIView {
func animate(){
}
}
If I remove the func animate() { } it will not fail. Even if this function is inside that one SweetAlert class then it will fail. And from what I've read this is a pretty standard swift code.
So this makes me wonder - is Hyperloop 2.0.0(!) ready for production apps?
I receive a similar error when creating a simple Swift class and including it in a Titanium/Appcelerator project with the appc.js file set up just how they show in the sparse documentation. The project will compile with no issues and run in the Xcode Simulator, as long as I don't reference the class from the Swift file. If I reference that class in the project, the error arises. Based on the compile log, Swift files do compile into the project, but the classes don't seem to properly link in the end for "require"-ing into the JavaScript code.
When I search online for solutions, this same issue shows up all over, and the Titanium/Appcelerator team never presents a solution. I've tried Titanium SDK 6.0.1, 6.1.2, and 6.2.0, with all the same results; I've tried adding Swift code to existing projects and to new ones; I've also uninstalled and reinstalled Titanium/Appcelerator. (I've even tried running their sample app with no luck!)
Hyperloop does work when I use native frameworks, like UIKit, but it doesn't seem ready for production apps for 3rd party frameworks and classes.
I hope my discoveries help you and others to debug projects, but, unfortunately, I don't think I will be able to offer much help outside that.

Xamarin Android Binding Library Custom Namespace Not Recognized

I am creating a Xamarin Android binding library for an existing JAR that contains a single class and following the Xamarin binding library documentation, I am able to successfully rename the namespace using:
<attr path="/api/package[#name='com.company.blah']" name="managedName">Company.Blah</attr>
I also confirmed the namespace is changed in the generated class in the 'obj/Debug' folder:
namespace Company.Blah {
// Metadata.xml XPath class reference: path="/api/package[#name='com.company.blah']/class[#name='NativeClass']"
[global::Android.Runtime.Register ("com/sprylab/android/widget/TextureVideoView", DoNotGenerateAcw=true)]
public partial class NativeClass
{
...
}
}
I face two problems:
I am unable to reference NativeClass from a sample Android project. It's like to doesn't see the namespace at all. The binding project built successfully without errors.
If I remove the package namespace rename setting, it also builds successfully and I am then able to reference it in my sample project but it requires that I fully qualify the class name anywhere it is used:
private com.company.blah.NativeClass nativeClass;
I'm hoping if I can fix #1, then #2 will not show up again. If so, I'm also curious how you prevent fully qualified class names from showing up?
Going into the project settings of the binding library project and clearing the box to the right of the assembly name seemed to do the trick. Looking inside the csproj directly, it is the root namespace.

Resources