So I have a settings panel in my app that uses a TableView and Cells to show various options to users. There are EntryCell and SwitchCell items in the TableView. These Cells are data bound to a ViewModel.
Decided to add a "Reset to Defaults" button on the settings page. Pressing it, as you might expect, programmatically changes all the settings values in the app to their original, unedited states. When I try this, however, the values displayed do not appear to change at all. EntryCells retain their edited text, and SwitchCells remain in their previous states. If I close the settings page, go back to the main app, then recreate the settings page, of course, the values all reflect their defaults. I tried utilizing ForceLayout on the settings page, but it doesn't appear to change the behavior at all.
This is on Android by the way, actual devices or simulators. I haven't tried in iOS yet...
Is there a means to get the TableView to update itself? I even tried resetting its BindingContext to null, perform the data reset, then set the BindingContext back, but even that doesn't make it update itself...
Xaml looks like this:
<StackLayout>
<TableView Intent="Settings" x:Name="tblSet" >
<TableView.Root>
<TableRoot>
<TableSection Title="Server Settings">
<EntryCell x:Name="ecServer" Label="URL:" Placeholder ="Required Server Address" />
...other entrycells and switchcells here...
</TableSection>
</TableRoot>
</TableView.Root>
</TableView>
<Button Text="Reset" x:Name="btnReset" VerticalOptions="End" HorizontalOptions="Center" />
</StackLayout>
The bound data is a class with Strings and booleans, nothing fancy. For example:
public class UserSettings : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Update any bindings that a given value has changed.
/// </summary>
/// <param name="propertyName">The property that changed, by name.</param>
private void OnPropertyChanged( [CallerMemberName] String propertyName = "" )
{
var handler = PropertyChanged;
if( handler != null )
{
handler( this, new PropertyChangedEventArgs( propertyName ) );
}
}
private static readonly UserSettings instance = new UserSettings();
public static UserSettings Instance
{
get { return instance; }
}
private String _serverUri;
public String ServerURI
{
get { return _serverUri; }
set
{
if( _serverUri != value )
{
_serverUri = value;
OnPropertyChanged();
}
}
}
public void ResetAll()
{
ServerURI = String.Empty;
...
}
}
For the settings page:
public class SettingsPage : ContentPage
{
UserSettings _us;
public SettingsPage()
{
...initialization...
_us = UserSettings.Instance;
tblSet.BindingContext = _us;
ecServer.SetBinding( EntryCell.TextProperty, "ServerURI" );
btnReset.Clicked += btnReset_Clicked;
... other setup....
}
public async void btnReset_Clicked( object sender, EventArgs e )
{
bool bAnswer = await DisplayAlert( "", "Resetting will clear all user settings and data. Do you wish to perform the reset?", "Yes", "No" );
if( bAnswer == true )
{
_us.ResetAll();
ForceLayout();
}
}
}