Forum Cross Platform with Xamarin

PCL working with shared code (cross platform library)

BiancaBianca USMember
edited October 2014 in Cross Platform with Xamarin

I'm new with Xamarin and have to create a cross platform library (iOS, Android, Win Phone). i have so much difficult to create a pcl who works for those 3 platforms. For example, this pcl needs establish a socket connection with a printer. In pcl i can't use System.Net.Sockets so my idea is create a method and tried call his code with shared project . when i execute the code, is stepped over the code inside > #if ANDROID .

PCL

public class Class1
{
public void socket()
{
Conecta.Class oi = new Conecta.Class();
oi.conecta();
}
}

SHARED PROJECT

namespace Conecta
{
class Class
{
public void conecta()
{
#if ANDROID

    System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();
    clientSocket.Connect("192.168.210.171", 4002);
    NetworkStream serverStream = clientSocket.GetStream();
    byte[] outStream = System.Text.Encoding.ASCII.GetBytes("teste \n");
    serverStream.Write(outStream, 0, outStream.Length);
    serverStream.Flush();

#endif
}

}

}

It's just a test, the code isn't sophisticated Someone can help me? Any other idea?

Posts

  • I feel your pain. PCL so limits the selection of available libraries that it seems almost impossible to get any work done.

  • BiancaBianca USMember
    edited October 2014

    I tried everything !! The best solution was create a pcl interface and implements his method from Shared project called by Xamarin.Android (for example). But in my case i can't show the code for everybody.. if i use him, i will have to expose my code (am i right??)and the idea is generate a single dll.

  • SKallSKall USMember ✭✭✭✭

    You don't want to reference the implementation class directly. Instead you want to "resolve" it through a service locator / dependency container. The quickest way would be to use the built-in DependencyService class from Xamarin.Forms.

    Quick sample, from PCL first the interface + a client code using the interface:

    public interface IConnection
    {
        void Connect();
    }
    
    public class ConnectionClient
    {
        public void Connect()
        {
            var connection = Xamarin.Forms.DependencyService.Get<IConnection>();
    
            connection.Connect();
        }
    }
    

    Implementation either on shared project or platform specific project. The key is to tell the DependencyService that there is a class implementing interfaces, which is the first line.

    [assembly: Xamarin.Forms.Dependency(typeof(DependencySample.SharedConnection))]
    
    namespace DependencySample
    {
        public class SharedConnection : IConnection
        {
            #region IConnection implementation
    
            public void Connect()
            {
                #if __DROID__
    
                #else
    
                throw new NotImplementedException();
    
                #endif
            }
    
            #endregion
        }
    }
    
Sign In or Register to comment.