Windows手机的进度条,同时搜索位置

本文关键字:搜索 位置 手机 Windows | 更新日期: 2023-09-27 18:16:52

我尝试激活一个进度条,而应用程序正在搜索位置(按下按钮后)我怎样才能用最好的方法解决它?

最好的方式是在那里得到一个if else,无论我是否从地理定位器获得(正确的)数据并检查它。

private async void Ellipse_Tap (object sender, System.Windows.Input.GestureEventArgs e)
{
    Geolocator geolocator = new Geolocator();
    //Set his accuracy in Meters 
    geolocator.DesiredAccuracyInMeters = 50;
    try
    {
        //The await guarantee the calls  to be returned on the thread from which they were called
        //Since it is call directly from the UI thread, the code is able to access and modify the UI directly when the call returns.
        Geoposition geoposition = await geolocator.GetGeopositionAsync(
            maximumAge: TimeSpan.FromMinutes(5),
            timeout: TimeSpan.FromSeconds(10)
            );
        //Relativer Nullpunkt
        delta_y = geoposition.Coordinate.Latitude - y;
        delta_x = geoposition.Coordinate.Longitude - x;
        Path.Visibility = Visibility.Visible;
    }
    //If an error is catch 2 are the main causes: the first is that you forgot to includ ID_CAP_LOCATION in your app manifest. 
    //The second is that the user doesn't turned on the Location Services
    catch (Exception ex)
    {
        if ((uint)ex.HResult == 0x80004004)
        {
            MessageBox.Show("Location is disabled in phone settings.");
            return;
            //Application.Current.Terminate();
        }
        //else
        {
            // something else happened during the acquisition of the location
        }
    }
}

Windows手机的进度条,同时搜索位置

假设您在SystemTry中使用ProgressIndicator,将以下内容添加到OnNavigatedTo方法

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);
    SystemTray.ProgressIndicator = new ProgressIndicator();
}

然后创建此方法来设置ProgressIndicator

private void DisplayProgressIndicator(bool isvisible, string message = "")
{
    SystemTray.ProgressIndicator.Text = message;
    SystemTray.ProgressIndicator.IsIndeterminate = isvisible;
    SystemTray.ProgressIndicator.IsVisible = isvisible;
}

然后使用Eclips_Tap方法中创建的方法。

private async void Ellipse_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
    Geolocator geolocator = new Geolocator();
    geolocator.DesiredAccuracyInMeters = 50;
    try
    {
        DisplayProgressIndicator(true, "Finding current location..."); // < SET THE PROGRESS INDICATOR
        Geoposition geoposition = await geolocator.GetGeopositionAsync(
            maximumAge: TimeSpan.FromMinutes(5),
            timeout: TimeSpan.FromSeconds(10)
            );
        delta_y = geoposition.Coordinate.Latitude - y;
        delta_x = geoposition.Coordinate.Longitude - x;
        Path.Visibility = Visibility.Visible;
        DisplayProgressIndicator(false); // << UNSET PROGRESS INDICATOR
    }
    catch (Exception ex)
    {
        if ((uint)ex.HResult == 0x80004004)
        {
            MessageBox.Show("Location is disabled in phone settings.");
            return;
        }                
    }
}