简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM