Xamarin Forms - Error CS1061

Asked

Viewed 325 times

1

How to resolve the error CS1061 in the code below: Code to open a local webview on each Xamarin.Forms platform

public partial class MapViewDetail : ContentPage
{
    public interface IBaseUrl   { string Get(); }

    public MapViewDetail()
    {
        InitializeComponent();


        var urlSource = new UrlWebViewSource();

        string url = DependencyService.Get<IBaseUrl>().BaseUrl(); 

        string TempUrl = Path.Combine(url, "mapas.html");
        urlSource.Url = TempUrl;
        Browser.Source = urlSource;
    }
}


[assembly: Xamarin.Forms.Dependency(typeof(BaseUrl))]
namespace AppFrete.UWP
{
    public class BaseUrl : IBaseUrl
    {
        public string Get()
        {
            return "ms-appx-web:///";
        }
    }
}

Error Presented

Severity Code Description Project File Line Suppression State Error CS1061 'Mapviewdetail.Ibaseurl' does not contain a Definition for 'Baseurl' and no Extension method 'Baseurl' Accepting a first argument of type 'Mapviewdetail.Ibaseurl' could be found (are you Missing a using Directive or an Assembly Reference?) AppFrete C:\Users\vinic\source\repos\AppFrete\MapaMVC\AppFrete\AppFrete\View\Map\MapViewDetail.xaml.cs 28 Active

inserir a descrição da imagem aqui

1 answer

1


TL;DR;

This build error already shows you where and what the problem is.

'Mapviewdetail.Ibaseurl' does not contain a Definition for 'Baseurl' and no Extension method 'Baseurl' Accepting a first argument of type 'Mapviewdetail.Ibaseurl' could be found

(Translation and highlights of mine)

'Mapviewdetail.Ibaseurl' does not have a definition for 'BaseUrl' and no method of extension called BaseUrl accepting as first argument a MapViewDetail.IBaseUrl was found

You’re trying on the object of type IBaseUrl consume a method that does not exist (BaseUrl).

Complement

I can’t say exactly what your intention is with the code that was written, it doesn’t make much sense. Let me comment it to good old jack style:

1 - You have created a class MapViewDetail who inherits from ContentPage:

public partial class MapViewDetail : ContentPage

2 - Within of her you created a Interface IBaseUrl which has a method Get that must return a string:

public interface IBaseUrl   { string Get(); }

3 - Returning to the constructor of the class MapViewDetail is that the mistake happens:

// Intancia um objeto qualquer
var urlSource = new UrlWebViewSource(); 

// Tenta recuperar uma instância que implementa IBaseUrl (em outras palavras, 
// a única coisa que você sabe sobre essa instância é que será possível invocar o 
// método Get que te retornaria uma string)
string url = DependencyService.Get<IBaseUrl>().BaseUrl();

Theoretically, DependencyService.Get<IBaseUrl>() already returns an instance that implements the interface you declared, which shouldn’t go wrong (but I doubt very much that it works) would be DependencyService.Get<IBaseUrl>().Get();, which is a call to the method you stated in the interface.

However, given your previous questions and the amount of conceptual problems that this small part of the code shows, I’m assuming that you still don’t know very well the language and the development environment .Net. If that’s the case, you’re starting out on a very complicated path, because this little bit of code (as well as the Xamarin) already uses resources such as Interfaces, Dependency injection, Inheritance and Partial classes that requires you to meet some pillars of object orientation.

Only then can we help by giving objective answers. Until then, I suggest you continue consuming the content on the subject right here in Stackoverflow in Portuguese and start from simpler implementations, as the references suggested in the tag wiki .

I hope I’ve helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.