1
votes

I'm trying to set up a brand new Xamarin Android project where I can use Xamarin.Forms (with .NET Standard 2.0) for the UI and can use Xamarin Live Player to preview XAML/run it. I've been getting a strange error that I don't know how to resolve.

I create a New Project > Cross-Platform App (Xamarin) leaving the default name of "App1". I select a Blank App, with UI Technology set to Xamarin.Forms and Code Sharing Strategy to Portable Class Library (PCL). The moment the solution is generated, I proceed to delete the iOS and UWP projects. I build the project and preview in Xamarin Live Player just fine.

Next, I create Add > New Project to the solution. I select a .NET Standard > Class Library (.NET Standard). I leave the default name as "ClassLibrary1". I click OK. I right-click the ClassLibrary1 project and Manage NuGet Packages..., then browse and install Xamarin.Forms (which is the same version as the PCL, which is 2.4.0.280).

Next, I copy and paste App.xaml and MainPage.xaml from the PCL (named App1 (Portable) into the ClassLibrary1 project.

Next, I delete the App1 (Portable) PCL project. This causes an error in the MainActivity.cs of App1.Android because the reference to App is not found. I right-click and add the ClassLibrary1 reference in App1.Android. Additionally, in MainActivity.cs, I add using ClassLibrary1;

This is what my App1.Android now looks like:

using System;
using ClassLibrary1;
using Android.App;
using Android.Content.PM;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

namespace App1.Droid
{
    [Activity(Label = "App1", Icon = "@drawable/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        protected override void OnCreate(Bundle bundle)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;

            base.OnCreate(bundle);

            global::Xamarin.Forms.Forms.Init(this, bundle);
            LoadApplication(new App());
        }
    }
}

The project builds just fine. But when I go to preview it in Xamarin Live Player, I get this weird error message:

The following errors were encountered when building and running your app:

• MainActivity.cs: The type or namespace name 'ClassLibrary1' could not be found (are you missing a using directive or an assembly reference?) • MainActivity.cs: The type or namespace name 'App' could not be found (are you missing a using directive or an assembly reference?)

enter image description here

Can anyone tell me anything I can do to fix this error and get Xamarin Live Player to work?

I've additionally, tried doing a NuGet update on everything in the solution (nothing pre-release though) with no success.

I'm running Visual Studio 15.4.2. I've tried going to 15.5 (preview) and have had no luck.

2
Please add code, errors and data as text (using code formatting), not images. Images: A) don't allow us to copy-&-paste the code/errors/data for testing; B) don't permit searching based on the code/error/data contents; and many more reasons. In general, code/errors/data in text format >>>> code/errors/data as an image >> nothing. Images should only be used, in addition to text in code format, if having the image adds something significant that is not conveyed by just the text code/error/data.Makyen♦
Everything you've done is just fine. But could you please register in beta test program of xamarin live player app and install beta version on your phone?Yaser Moradi
@Makyen - Gotcha. At first I thought the dialog didn't seem to allow me to copy and paste the text out of it... but found that clicking the dialog and then doing a Ctrl+C puts the entire error dialog in the clipboard for use. Not that apparent.Cory Kroll
@YaserMoradi I've installed the beta player (1.0.507) on my Android tablet (Samsung Tab E) and still get the same issue.Cory Kroll

2 Answers

2
votes

Unfortunately, The Live player does not yet work on .Net Standard. Ref: https://forums.xamarin.com/discussion/comment/303636

-1
votes

I think you need to add your newly created .netstandard project as a reference each of the platform specific project. Follow these two steps

Step 1:

Step 1

Step 2:

Step 2

17a: Right click Project(eg. Android project) -> 17b: Add -> 17c: Reference -> 18a: Tick the newly created project and click OK (this will basically tell your project that, it needs to add the .NETStandard project as a reference) and 18b: click OK to add as reference.

Repeat the same for Android, UWP & iOS.

At the same time, you have to ensure all projects installed with same version of Xamarin.Forms & .NET Standard 2.0.

You can refer to my written post on "Create Cross Platform Mobile App with .NET Standard 2.0" for steps that will guide you converting Xamarin.Forms from PCL to .NET Standard.