1

I have a ProgressRing that I need to show while a list is loaded into a DataGrid. When I run the app, the data is loaded, but ProgressRingisn't showed. What am I doing wrong?

XAML:

<Grid>
    <ProgressRing x:Name="CarregamentoDeContas" />

    <controls:DataGrid
        x:Name="DataGridDeContas"
        AutoGenerateColumns="True"
        ItemsSource="{x:Bind Contas}" />
</Grid>

code-behind:

    private List<Conta> Contas;

    private void ObterListaDeContas()
    {
        try
        {
            CarregamentoDeContas.IsActive = true;
            Contas = ListaDeContas.ObterContas();
        }
        finally
        {
            CarregamentoDeContas.IsActive = false;
        }
    }

    public ContasPage()
    {
        this.InitializeComponent();

        ObterListaDeContas();
    }
  • hi @joao please try just your progress ring by commenting controls:DataGrid and finnaly block may be you datagrid is superpossed on your progress ring – sayah imad Apr 13 '19 at 14:26

2 Answers2

0

ProgressRing is not showed during data loading

Please check ObterListaDeContas method, it has not contain async calling, it means that IsActive property of CarregamentoDeContas will be set as false directly. if you want show progress ring, you could set task delay in ObterListaDeContas method or make ObterContas() as asynchronous method and call it with await method.

private async void ObterListaDeContas()
{
    try
    {
        CarregamentoDeContas.IsActive = true;

        //The progress will last for two seconds
        await Task.Delay(2000);

    }
    finally
    {
        CarregamentoDeContas.IsActive = false;
    }
}
Nico Zhu
  • 32,367
  • 2
  • 15
  • 36
0

You should avoid using "async void", maybe it would be better:

    //create Task<bool>
    private async Task<bool> ObterListaDeContas()
    {
        try
        {
            //ProgressRing activation
            CarregamentoDeContas.IsActive = true;

            //DoSomethingBig() or await Task.Delay(3000)-only for learning
            return true;
        }
        catch
        {
            //catch your exceptions
            return false;
        }
    }

    private void DeactiveProgressBar(bool isDone)
    {
        //ProgressRing deactivation when the task is over
        CarregamentoDeContas.IsActive = false;

        //optional
        if (isDone)
        {
            Debug.WriteLine("Data loaded");
            //Unblock Button, DoSomething() etc...
        }
        else
        {
            Debug.WriteLine("Data NOT Loaded");
            //give a warning message to the user
        }
    }

Using this method:

DeactiveProgressBar(await ObterListaDeContas());

It works as intended with the UWP application.

Kamil
  • 11
  • 1
  • 2
  • 3