View Progressring while loading data

Asked

Viewed 30 times

0

I have a ProgressRingwhich must be displayed while a list of accounts is loaded into a DataGrid:

<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();
    }

When running the application, the data is loaded but Progressring is not displayed. What I am doing wrong?

  • Joao, I went to see an example of use and found one that arrow the visibility of Progressring: CarregamentoDeContas.Visibility = Visibility.Visible;

  • @Rodrigotognin, thanks for the comment. I put the snippet inside the try and it brought the CS0176 error The member "Visibility.Visible" cannot be accessed with an instance reference; qualify it with a type name. I set the property in control, which removes the error, but still does not display the ProgressRing.

  • Joao, I saw the example on this site: https://www.tutorialspoint.com/xaml/xaml_progressring.htm - Suddenly you can take a look and see if there is something missing.

  • I found something else here, João. Add this library to your project: using Windows.UI.Xaml;

  • The library is already included, @Rodrigotognin. And the example is the same as in Windows Community Toolkit Sample App. I believe the problem is in try; I believe that if I put it in an asynchronous method I will succeed.

1 answer

0

Instead of loading the data in the page builder, try to load the event Onnavigatedto(), which is fired every time it enters the page.

protected override async void OnNavigatedTo(NavigationEventArgs e)
{
    try
    {
        CarregamentoDeContas.IsActive = true;
        // Idealmente é que o carregamento seja assíncrono para a UI não travar
        DataGridDeContas.ItemsSource = await ListaDeContas.ObterContasAsync();
    }
    finally
    {
        CarregamentoDeContas.IsActive = false;
    }
}

Page.Onnavigatedto

Browser other questions tagged

You are not signed in. Login or sign up in order to post.