Forum Visual Studio

How to add existing XAML files from Windows VS Solution to a Visual Studio for Mac solution.

coolrjmcoolrjm INMember ✭✭✭
edited September 2017 in Visual Studio


I have created a PCL project targetting Android on Visual Studio 2105 on Windows.

The project has been compiled for Android and it works without any issue.

Now I want to use the same PCL project for iOS and so created a new project in Visual Studio for Mac on a Mac desktop.

Now when I try to add existing XAML files in the PCL project, it does not include the xaml.cs code behind file.

I also observed that the type of file is None for a XAML file whereas it should be Content or Embedded Resource. Verified that by adding a new "Forms Content Page XAML"

Is there any particular way to importing existing XAML files along with their corresponding code behind files.




  • JGoldbergerJGoldberger USMember, Forum Administrator, Xamarin Team, University Xamurai
    edited September 2017

    Why can't you just add the PCL project to your solution and reference it from the iOS app project? Better yet, why can't you just add an iOS Project to your existing solution? I must be missing something about how you set up your PCL, not sure why you feel you ned to copy the XAML files to a new PCL project.

    Xamarin.Android, Xamarin.iOS, and Xamarin.Mac (Mobile) all use the same subset of .Net, so if you target one you are really targeting all three. In fact if you open the Project properties for a PCL library, and click the "Change" button on the Library settings page. Screenshot:

    Note at the bottom where it says that Xamarin.iOS, etc., will also be targeted as they have the same set of portable APIs.

    IOW your PCL that targets Xamairn.Android already targets Xamarin.iOS so you should just be able to reference it from an iOS project. You will have to make other changes in the iOS project itself to load the Xamarin Forms app. You can get rid of the template ViewController and Main.storyboard, then update the AppDelegate.cs file so that the AppDelegate inherits from global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate, and you call Xamarin.Forms.Init() and the LoadApplication() methods from the AppDelegate.FinishedLaunching override. e.g.:

    public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
         public override bool FinishedLaunching(UIApplication app, NSDictionary options)
            LoadApplication(new App());
            return base.FinishedLaunching(app, options);

    You will also have to open your Info.plist file in the iOS project and make sure that nothing is set for the Main Interface field. By default this would be set to Main.storyboard in a new template iOS project.

    If you create a new Forms solution, you can look over the iOS project to make sure you have everything set correctly to use a Xamarin.Forms PCL.

  • coolrjmcoolrjm INMember ✭✭✭

    Hi Jon,

    The reason for this mess switching between Xamarin on Windows and Xamarin on Mac is explained below.

    • We worked on PCL targetting Android since the last 7 months and completed the project and it worked without any issue. We developed this project on Windows Platform.

    • When we started targetting iOS platform using the same PCL, we ran into this issue with the debugger. Step through debugging was not working in Windows, even after specifying the debugger to handle null reference exceptions. I read about changing the version of Mono to 4.8 but could not find where to change it.

    • So we experimented with a small project on Visual Studio for Mac and we were able to step through debugging and view the values and identify the issues.

    • But we noticed that while adding existing files in Visual Studio for Mac, the XAML page and the code behind page were not linked.

    Hence we started with creating a new solution from scratch, added new pages and copying the inline code for both XAML and code behind. We were able to use the debugger to identify the bugs and fix them.

    Regarding your hints to simplify the process , I have the following doubts.

    • How do you add the existing PCL project after creating a new solution based on PCL and targetting Android and iOS in Visual Studio for Mac.



  • JGoldbergerJGoldberger USMember, Forum Administrator, Xamarin Team, University Xamurai

    @coolrjm said:

    • How do you add the existing PCL project after creating a new solution based on PCL and targetting Android and iOS in Visual Studio for Mac.
    1. Copy or move the folder that your old PCL project is in to your new solution's folder.
    2. Open your new solution in Visual Studio for Mac
    3. Right click on the solution in the solution explorer and select Add->Add Existing Project
    4. Navigate to the PCL folder that you copied/moved into your new solution folder in step 1
    5. Select the .csproj file for your copied/moved PCL project.
    6. Click Open
    7. Delete the PCL project that was included with the template and any references to it from your platform projects. (Right-click on the project in the solution explorer and select Delete. This should also remove the reference from the platform projects.
    8. Add a reference to the newly added PCL project to all of your platform projects

    Note: You can add or remove project references in the platform projects by right-clicking on the References folder in the platform projects and selecting Edit References, then selecting the Projects tab in the Edit References dialog and placing a checkmark next to the PCL project you want to reference, then clicking OK.

  • coolrjmcoolrjm INMember ✭✭✭


    Thanks for the help.



Sign In or Register to comment.