How to update a progress bar based on a form in WinRT?

343 views Asked by At

I have a form containing few TextBox elements and a progress bar. I want the progress bar to be updated when the TextBox have some values assigned.

I noticed that in WPF this can be achieved using BackgroundWorker, but this doesn't exist in WinRT

XAML:

<StackPanel>
    <ProgressBar x:Name="progressBar1" 
                 Value="{Binding ProgressPercent}"  
                 HorizontalAlignment="Left" 
                 IsIndeterminate="False" 
                 Maximum="100" />

    <TextBlock Text="Name" Grid.Column="0"/>
    <TextBox x:Name="NameTextBox" Text="{Binding Name, Mode=TwoWay}"/>

    <TextBlock Text="Data" Grid.Row="1" Grid.Column="0"/>
    <DatePicker Date="{Binding Data, Mode=TwoWay}" 
                HorizontalAlignment="Stretch" 
                VerticalAlignment="Center" 
                Grid.Row="1" 
                Grid.Column="1" 
                CalendarIdentifier="GregorianCalendar" 
                Margin="0,8" />

    <TextBlock Text="Address" Grid.Row="2" Grid.Column="0"/>
    <TextBox x:Name="AddressTextBox" 
             Text="{Binding Address, Mode=TwoWay}" 
             Grid.Row="2" 
             Grid.Column="1" 
             Margin="0,5" />

    <TextBlock Text="Email" Grid.Row="3" Grid.Column="0"/>
    <TextBox x:Name="EmailTextBox" 
             Text="{Binding Email, Mode=TwoWay}" 
             Grid.Row="3" 
             Grid.Column="1" 
             Margin="0,5" />
</StackPanel>

ViewModel:

#region fields

private string progressPercent {get;set;}

#endregion

#region proprietes
 public int ProgressPercent
    {
        get
        {
            return this.progressPercent;
        }
        set
        {
            this.progressPercent = value;
            this.RaisePropertyChanged(() => this.ProgressPercent);
        }
    }
#endregion

How can this be achieved in WinRT? Most of the examples are for Wpf unfortunately

2

There are 2 answers

0
SamTh3D3v On BEST ANSWER

Each time one of the form's fields got updated a percent value needs to be added to the ProgressPercent property, i don't see the need for an async code, but if you insisted on that here how to do it

 private string _name = "";
    public string Name
    {
        get
        {
            return _name;
        }

        set
        {
            if (_name == value)
            {
                return;
            }
            _name = value;
            OnPropertyChanged();
            Window.Current.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
            {
                ProgressPercent += 10; //handle the fact that this needs to be added once using a bool or something 
            });

        }
    }
0
Thomas Bouman On

You're missing INotifyPropertyChanged. Without this, the progress bar won't get the updated values from your ViewModel, and won't show any progress.

You can read more INotifyPropertyChanged here.