Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Xamarin.Forms.Platform.iOS not found?

AmigomanAmigoman Member ✭✭

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!

Best Answer

Answers

  • JohnHJohnH GBMember ✭✭✭✭✭
    You haven’t included the ifdef for iOS.
  • AmigomanAmigoman Member ✭✭

    @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.

  • JohnHJohnH GBMember ✭✭✭✭✭
    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.
  • AmigomanAmigoman Member ✭✭

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

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

  • JohnHJohnH GBMember ✭✭✭✭✭
    Ok, so why do you need to show iOS views within a Xamarin Forms page?
  • JohnHJohnH GBMember ✭✭✭✭✭
    edited March 29
    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?
  • JohnHJohnH GBMember ✭✭✭✭✭

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

  • ColeXColeX Member, Xamarin Team Xamurai

    @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.

  • JohnHJohnH GBMember ✭✭✭✭✭

    @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?

  • ColeXColeX Member, Xamarin Team Xamurai

    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 .

  • JohnHJohnH GBMember ✭✭✭✭✭

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

  • AmigomanAmigoman Member ✭✭

    @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!

Sign In or Register to comment.