locked
Xamarin.Forms.Platform.iOS not found? RRS feed

  • Question

  • User372619 posted

    Hey guys,

    I'm trying to work with native controls in Xamarin.Forms, but I get the following exception when trying to reference Xamarin.Forms.Platform.iOS:

    The type or namespace name 'iOS' does not exist in the namespace 'Xamarin.Forms.Platform' (are you missing an assembly reference?)

    Because of this I'm not able to use the .ToView() extension method.

    This is my code:

    using System;
    using UIKit;
    using Xamarin.Forms.Platform.iOS;
    using Xamarin.Forms;
    
    namespace AppName
    {
        public class MyPage : ContentPage
        {
            public MyPage()
            {
                View view = null;
                UILabel label = new UILabel
                {
                    Text = "Hello iOS Native!",
                    Font = UIFont.FromName("Papyrus", 32f),
                };
                view = label.ToView();
                Content = view;
            }
        }
    }
    

    Xamarin.Forms version: 4.5.0.495

    These are the docs I'm working from: https://docs.microsoft.com/en-us/archive/msdn-magazine/2016/connect/mobile-embedding-native-views-in-your-xamarin-forms-apps

    Do any of you know what's going on? I would really appreciate if you could help me out! Thanks in advance!

    Saturday, March 28, 2020 8:09 PM

Answers

  • User369978 posted

    We can't do this in a PCL (.NetStandard) project ,this feature only supports shared project which has been removed by Xamarin Team now .

    Check https://forums.xamarin.com/discussion/comment/157839/#Comment_157839 .

    It is also mentioned in documentation : https://docs.microsoft.com/en-us/archive/msdn-magazine/2016/connect/mobile-embedding-native-views-in-your-xamarin-forms-apps .

    Normally a Xamarin.Forms solution contains tiny stub application projects for each platform and a common Portable Class Library (PCL) that contains the bulk of your Xamarin.Forms application. However, when using native views in code, you can’t use a PCL. Instead, you’ll need to put your Xamarin.Forms code in a shared project, which at Xamarin is often called a Shared Asset Project or SAP. In the New Project dialog of Visual Studio select Blank App (Xamarin.Forms Shared) rather than the usual Blank App (Xamarin.Forms Portable). (In Xamarin Studio you select between Portable Class Library or Shared Library with radio buttons.) The code in this shared project is effectively an extension of each application, which means you can use C# conditional compilation directives (#if, #elif and #endif) to delimit the platform-specific code.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, March 30, 2020 7:54 AM

All replies

  • User74 posted

    You haven’t included the ifdef for iOS.

    Saturday, March 28, 2020 8:42 PM
  • User372619 posted

    @JohnH you mean like this?:

    using System;
    using Xamarin.Forms;
    #if __IOS__
    using Xamarin.Forms.Platform.iOS;
    using UIKit;
    #endif
    
    namespace AppName
    {
        public class MyPage : ContentPage
        {
            public MyPage()
            {
                View view = null;
    #if __IOS__
                UILabel label = new UILabel
                {
                    Text = "Hello iOS Native!",
                    Font = UIFont.FromName("Papyrus", 32f),
                };
                view = label.ToView();
    #endif
                Content = view;
            }
        }
    }
    

    I did try that out at first and it does make the code compile, but I just get a blank page. The code inside the ifs is also greyed out for some reason.

    Saturday, March 28, 2020 8:57 PM
  • User74 posted

    Being able to compile is a good starting point at least. ;) As to why you can’t see the label, maybe try to put it in the page centre of the page? Or if you have some existing iOS code you can paste etc.

    Saturday, March 28, 2020 9:05 PM
  • User372619 posted

    @JohnH doesn't seem to make a difference sadly.

    I also don't have any other code. It's a clean project.

    Saturday, March 28, 2020 9:25 PM
  • User74 posted

    Ok, so why do you need to show iOS views within a Xamarin Forms page?

    Saturday, March 28, 2020 9:31 PM
  • User74 posted

    I’ve just reread your last post, if the code is greyed out it means it isn’t compiling it. That means the config you are using isn’t including the definition that the ifdef requires. What is your startup project? Are you actually compiling for Android instead of iOS?

    Sunday, March 29, 2020 5:45 PM
  • User369978 posted

    We can't do this in a PCL (.NetStandard) project ,this feature only supports shared project which has been removed by Xamarin Team now .

    Check https://forums.xamarin.com/discussion/comment/157839/#Comment_157839 .

    It is also mentioned in documentation : https://docs.microsoft.com/en-us/archive/msdn-magazine/2016/connect/mobile-embedding-native-views-in-your-xamarin-forms-apps .

    Normally a Xamarin.Forms solution contains tiny stub application projects for each platform and a common Portable Class Library (PCL) that contains the bulk of your Xamarin.Forms application. However, when using native views in code, you can’t use a PCL. Instead, you’ll need to put your Xamarin.Forms code in a shared project, which at Xamarin is often called a Shared Asset Project or SAP. In the New Project dialog of Visual Studio select Blank App (Xamarin.Forms Shared) rather than the usual Blank App (Xamarin.Forms Portable). (In Xamarin Studio you select between Portable Class Library or Shared Library with radio buttons.) The code in this shared project is effectively an extension of each application, which means you can use C# conditional compilation directives (#if, #elif and #endif) to delimit the platform-specific code.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, March 30, 2020 7:54 AM
  • User74 posted

    @colex Are you saying there is now no official method for embedding native controls in Xamarin Forms?

    Monday, March 30, 2020 9:04 AM
  • User369978 posted

    @JohnH I mean it only works for Shared Project not .NET Standard library solutions .

    Check https://docs.microsoft.com/en-us/xamarin/xamarin-forms/platform/native-views/code#overview

    However, note that this functionality requires the use of #if defines in Xamarin.Forms Shared Project solutions, and isn't available from Xamarin.Forms .NET Standard library solutions.

    Monday, March 30, 2020 9:30 AM
  • User74 posted

    @colex Sure, but you also just said Shared projects have been deprecated. So how do you do this when you can no longer create a shared project?

    Monday, March 30, 2020 9:37 AM
  • User369978 posted

    I test on vs for windows and vs for mac , the shared project can be created on mac only , you can try it on mac .

    Monday, March 30, 2020 9:40 AM
  • User74 posted

    @colex I think the question is, why was Shared projects removed from VS for Windows?

    Monday, March 30, 2020 9:52 AM
  • User372619 posted

    @ColeX @JohnH The problem did turn out to be because I was using PCL instead of Shared Project. I was able to create a Shared Project in VS for Mac and got it working after playing around with it, but It's really unfortunate that you can't do this in PCL.

    Supposedly you can still embed native controls in PCL in XAML and use bindings to interact with the data, but I was having a few problems doing this. The native controls would not show up on initial load and would only appear after reloading the page using Hot Reload. I also couldn't figure out how to get the bindings to work for my use case. IntelliSense doesn't seem to be supported this way either, which made it much harder considering I haven't worked with Xamarin.iOS before.

    I really appreciate the help though. Thanks guys!

    Tuesday, March 31, 2020 11:07 AM