搜索位置时,Windows电话进度条

时间:2014-08-06 15:52:01

标签: c# windows-phone-8

我尝试激活进度条,同时应用正在搜索位置(按下按钮后) 我怎样才能以最好的方式解决它?

最好以某种方式在那里得到一个if else,我从地理定位器得到(rigth)数据并检查。

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
        }
    }
}

1 个答案:

答案 0 :(得分:1)

假设您使用ProgressIndicator中的SystemTry,请将以下内容添加到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;
        }                
    }
}

希望这会有所帮助..