Creating a UWP app with mvvm.
I am trying to set a refresh mechanism to call a webservice that will update the ItemSource data with the latest collection from the DB. Starting with a button trigger but eventually would like to add a timer.
When items are added to the ItemSource it is not just locally but through a webservice to an online DB so when a refresh is triggered a whole new ItemSource data needs to be loaded from the webservice.
Problems:
The get for the property syncfusion:SfDataGrid "ItemsSource="{Binding OrderDetails, Mode=TwoWay}" " only gets called once on initial load.
The OrderDetails set, gets triggerd with the refresh page button command which changes the property but not the get, it never gets called again.
Have attempted switching from ObservableCollection, IEnumerable with no trigger of the get on the property of OrderDetails.
My View:
<Grid x:Name="Grid" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" DataContext="{StaticResource OrderViewModel}">
<Grid.RowDefinitions>
<RowDefinition Height=".08*" MinHeight="50"/>
<RowDefinition Height="*" MinHeight="50"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" VerticalAlignment="Center" Margin="10,0,10,0" Orientation="Horizontal">
<Button Content="New License Order" Width="300" FontSize="20" Click="NewOrder" Margin="10,0,10,0"/>
<Button Content="Refresh" Width="300" FontSize="20" Command="{Binding RefreshOrderCommand}" Margin="50,0,10,0"/>
</StackPanel>
<Grid Grid.Row="1" x:Name="ContentArea" Margin="{StaticResource MediumLeftRightMargin}">
<Grid Background="{ThemeResource SystemControlPageBackgroundChromeLowBrush}" >
<ScrollViewer VerticalScrollMode="Auto" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
<syncfusion:SfDataGrid x:Name="grid" AutoGenerateColumns="True" ItemsSource="{Binding OrderDetails, Mode=TwoWay}" AllowSorting="True" AllowFiltering="True" ColumnSizer="Auto" />
</ScrollViewer>
</Grid>
</Grid>
</Grid>
My ViewModel (Ugly but explicit):
private RelayCommand _refreshOrderCommand;
public ICommand RefreshOrderCommand => _refreshOrderCommand ?? (_refreshOrderCommand = new RelayCommand(RefreshPage));
private IEnumerable<OrderDetails> _OrderDetails;
public IEnumerable<OrderDetails> OrderDetails
{
set => _OrderDetails = value;
get
{
if (_OrderDetails != null && !_OrderDetails.Any())
return _OrderDetails;
var lList = GetOrdersAsync();
_OrderDetails = lList.Result;
return _OrderDetails;
}
}
public void RefreshPage()
{
//OrderDetails = new List<OrderDetails>();
OrderDetails = GetOrdersAsync().Result;
NotifyPropertyChanged("OrderDetails");
}
public async Task<IEnumerable<OrderDetails>> GetOrdersAsync()
{
try
{
var client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AccessToken);
var response = await client.GetStringAsync(new Uri("http://localhost:####/api/GetOrders/")).ConfigureAwait(false);
var orders = JsonConvert.DeserializeObject<IEnumerable<OrderDetails>>(response);
return orders;
}
catch (Exception exception)
{
Debug.WriteLine($"We got a problem {exception.Message}");
return new List<OrderDetails>();
}
}