Xamarin Forms - Error CS1061

1

How to resolve the CS1061 error 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:///";
        }
    }
}

Displayed Error

  

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 \ MapMVC \ AppFrete \ AppFrete \ View \ Map \ MapViewDetail.xaml.cs 28 Active

    
asked by anonymous 03.05.2018 / 00:51

1 answer

1

TL; DR;

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

  'MapViewDetail.IBaseUrl' does not contain a definition for 'BaseUrl' and in the extension method 'BaseUrl' accepting the 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 extension method named BaseUrl accepting as first argument a MapViewDetail.IBaseUrl was found

You are trying to get the object of type IBaseUrl to consume a method that does not exist ( BaseUrl ).

Complement

I do not know exactly what your intention with the code that was written, it does not make much sense. Let me tell you the good old jack style:

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

public partial class MapViewDetail : ContentPage
2 In it you have created a Interface IBaseUrl that has a Get method that should return a string:
public interface IBaseUrl   { string Get(); }

3 - Returning to the class counter MapViewDetail is that the error 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();

In theory, DependencyService.Get<IBaseUrl>() already returns you an instance that implements the interface that you have declared, which should not give error (but I highly doubt it works) would be DependencyService.Get<IBaseUrl>().Get(); , which is a call to the method you declared in the interface.

However, given your previous questions and the amount of conceptual issues that this small part of the code shows, I'm assuming that you still do not know the language and the .Net development environment very well. If that's the case, it's starting out a pretty complicated path, because that bit of code (like Xamarin itself) already uses features like Interfaces , #, Inheritance and Partial classes that requires you to know a few #.

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 to start with simpler implementations, such as the references suggested in tag wiki .

I hope I have helped.

    
03.05.2018 / 03:44