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!
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.
Answers
@JohnH you mean like this?:
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.
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.
@JohnH doesn't seem to make a difference sadly.
I also don't have any other code. It's a clean project.
What is your startup project? Are you actually compiling for Android instead of iOS?
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 .
@colex Are you saying there is now no official method for embedding native controls in Xamarin Forms?
@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
@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?
I test on
vs for windows
andvs for mac
, the shared project can be created on mac only , you can try it on mac .@colex I think the question is, why was Shared projects removed from VS for Windows?
@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!