Max von der Mühle
Max von der Mühle

Reputation: 127

Type or namespace "Android" could not be found

I am creating an Android app with Xamarin.Forms 4.8 and need to add the namespace "Android.Media" to be able to use the MediaPlayer class.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.PlatformConfiguration.TizenSpecific;
using Android.Media;

namespace Breakapp
{
    public partial class MainPage : ContentPage
    {
        int seconds = 0;
        int minutes = 0;

        public MainPage()
        {
            InitializeComponent();
        }

        protected override void OnAppearing()
        {
            base.OnAppearing();
            minutes = Convert.ToInt32(Minutes.Text);
            seconds = Convert.ToInt32(Seconds.Text);

            Device.StartTimer(TimeSpan.FromSeconds(1), () =>
            {
                .....

However, as soon as I try to add it by using the using directive, I get the following error:

CS0246 The type or namespace name 'Android' could not be found (are you missing a using directive or an assembly reference?)

As I looked up the error message I found out that others fixed this error by adding the "Mono.Android" assembly as a reference to their project. But this assembly is already added. I already tried removing the assembly and readding it but it didn't work at all.

Upvotes: 2

Views: 6573

Answers (1)

Ivan I
Ivan I

Reputation: 9990

Your project needs to reference Xamarin.Android so that you are able to use Android.Media.

By default your Xamarin standard project (that you refer to) doesn't have this reference and generally shouldn't have this reference (as it won't be able to compile on iOS, UWP or any other supported platform).

There is a native project in your solution that already has this reference and from which you can call this code using dependency injection.

Upvotes: 2

Related Questions