Xamarin:方法开始没有被要求......?

时间:2017-03-24 20:06:33

标签: xamarin

所以,我有这些代码:

    static readonly string TAG = "X:" + typeof (Activity1).Name;
    Location _currentLocation;
    LocationManager _locationManager;

    string _locationProvider;
    TextView _locationText;

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        SetContentView(Resource.Layout.Main);

        _locationText = FindViewById<TextView>(Resource.Id.GpsTest);

        InitializeLocationManager();

        Add700ToCoordinates();
    }

    public void OnLocationChanged(Location location)
    {

        _currentLocation = location;
        if (_currentLocation == null)
        {
            _locationText.Text = "Unable to determine your location. Try again in a short while.";
        }
        else
        {
            _locationText.Text = string.Format("{0:f6},{1:f6}", _currentLocation.Latitude, _currentLocation.Longitude); 
            //das her wird ausgegeben bei button.click 
        }

    } // ausgabe der koordinaten 

    public void OnProviderDisabled(string provider) {}

    public void OnProviderEnabled(string provider) {}

    public void OnStatusChanged(string provider, Availability status, Bundle extras)
    {
        Log.Debug(TAG, "{0}, {1}", provider, status);
    }

    void InitializeLocationManager()
    {
        _locationManager = (LocationManager) GetSystemService(LocationService);
        Criteria criteriaForLocationService = new Criteria
                                              {
                                                  Accuracy = Accuracy.Fine
                                              };
        IList<string> acceptableLocationProviders = _locationManager.GetProviders(criteriaForLocationService, true);

        if (acceptableLocationProviders.Any())
        {
            _locationProvider = acceptableLocationProviders.First();
        }
        else
        {
            _locationProvider = string.Empty;
        }
        Log.Debug(TAG, "Using " + _locationProvider + ".");
    }

    protected override void OnResume()
    {
        base.OnResume();
        _locationManager.RequestLocationUpdates(_locationProvider, 0, 0, this);
        Log.Debug(TAG, "Listening for location updates using " + _locationProvider + ".");
    }

    protected override void OnPause()
    {
        base.OnPause();
        _locationManager.RemoveUpdates(this);
        Log.Debug(TAG, "No longer listening for location updates.");
    }

    public string StringWithLocation(Location location)
    {
        string StringWithCoordinates = "Null";

        _currentLocation = location;
        if (_currentLocation == null)
        {
            StringWithCoordinates = "Unable to determine your location. Try again in a short while.";
        }
        else
        {
            StringWithCoordinates = string.Format("{0:f6},{1:f6}", _currentLocation.Latitude, _currentLocation.Longitude);
        }

        return StringWithCoordinates;

    }

    public void Add700ToCoordinates()
    {
       // StringWithLocation(); 
    }

}

}

有四件我无法找到的东西:

1)我的方法在哪里&#34; onLocationChanged(位置位置)&#34;叫什么?我无法在任何地方找到它!

2)我在我的应用程序中编辑了一个按钮的id-name,该应用程序仍可正常使用新按钮。我无法看到按钮初始化的位置(我可以找到TextView)。我真的不知道为什么它自己初始化......我至少应该得到一个错误...

3)通常,据我所知,我需要一个button.click事件,以便从按钮获得结果。在这段代码中没有这样的东西,但它的工作正常......有谁知道是什么原因引起的?

4)按下我的一个按钮后给出结果的方法,它没有出现在代码中的任何地方......,类型为void。所以我不能从它返回一个字符串。然而,当我使它成为字符串而不是void时,IlocationListener会给出一个错误,声明该方法需要为void。我如何让它工作?

非常感谢!我无法与你们一起进步!

0 个答案:

没有答案