When I try updating in Xamarin Forms I get an System.NullReferenceException HResult=0x80004003 Message=Object reference not set to an instance of an object.
Here is the code.
namespace QiXApp.Services.Data
{
public class SxCaseDataService : BaseService, ISxCaseDataService
{
private readonly IGenericRepository _genericRepository;
public SxCaseDataService(IGenericRepository genericRepository,
IBlobCache cache = null) : base(cache)
{
_genericRepository = genericRepository;
}
public async Task<IEnumerable<SxCase>> GetAllSxCasesAsync()
{
List<SxCase> sxCasesFromCache =
await GetFromCache<List<SxCase>>(CacheNameConstants.AllSxCases);
if (sxCasesFromCache != null)//loaded from cache
{
return sxCasesFromCache;
}
else
{
UriBuilder builder = new UriBuilder(ApiConstants.BaseApiUrl)
{
Path = ApiConstants.SxCaseEndpoint
};
var sxCases = await _genericRepository.GetAsync<List<SxCase>>(builder.ToString());
await Cache.InsertObject(CacheNameConstants.AllSxCases, sxCases, DateTimeOffset.Now.AddSeconds(20));
return sxCases;
}
}
public async Task<SxCase> UpdateSxCase(SxCase selectedSxCase)
{
UriBuilder builder = new UriBuilder(ApiConstants.BaseApiUrl)
{
Path = ApiConstants.SxCaseEndpoint
};
var result = await _genericRepository.PatchAsync(builder.ToString(), selectedSxCase);
return result;
}
}
}
namespace QiXApp.Contracts.Services.Data
{ public interface ISxCaseDataService { Task> GetAllSxCasesAsync();
Task<SxCase> UpdateSxCase(SxCase selectedSxCase);
}
}
namespace QiXApp.ViewModels
{ public class SxCaseDetailViewModel : ViewModelBase { private SxCase _selectedSxCase; private readonly ISxCaseDataService _sxCaseDataService;
public SxCaseDetailViewModel(IConnectionService connectionService,
INavigationService navigationService, IDialogService dialogService)
: base(connectionService, navigationService, dialogService)
{ }
public ICommand UpdateSxCaseCommand => new Command(OnUpdateSxCase);
public ICommand ReadDescriptionCommand => new Command(OnReadDescription);
public SxCase SelectedSxCase
{
get => _selectedSxCase;
set
{
_selectedSxCase = value;
OnPropertyChanged();
}
}
public override async Task InitializeAsync(object sxCase)
{
SelectedSxCase = (SxCase)sxCase;
}
private async void OnUpdateSxCase()
{
await _sxCaseDataService.UpdateSxCase(SelectedSxCase);
MessagingCenter.Send(this, MessagingConstants.UpdateSxCase, SelectedSxCase);
await _dialogService.ShowDialog("Case details updated.", "Success", "OK");
}
private void OnReadDescription()
{
DependencyService.Get<ITextToSpeech>().ReadText(SelectedSxCase.Facesheet);
}
}
}
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:utility="clr-namespace:QiXApp.Utility;assembly=QiXApp"
xmlns:dataform="clr-namespace:Syncfusion.XForms.DataForm;assembly=Syncfusion.SfDataForm.XForms"
utility:ViewModelLocator.AutoWireViewModel="True"
mc:Ignorable="d"
x:Class="QiXApp.Views.SxCaseDetailView">
<ContentPage.Content>
<StackLayout>
<dataform:SfDataForm x:Name="dataForm" DataObject="{Binding SelectedSxCase}"
AutoGenerateItems="false"
NotifyPropertyChanges="True">
<dataform:SfDataForm.Items>
<dataform:DataFormTextItem Name="SxCaseId" Editor="Text"/>
<dataform:DataFormDateItem Name="Dos" Editor="Date"/>
<dataform:DataFormTextItem Name="Facesheet" Editor="Text"/>
<dataform:DataFormTextItem Name="Record" Editor="Text"/>
<dataform:DataFormSegmentItem Name="Complication" Editor="Switch"/>
<dataform:DataFormSegmentItem Name="ReturnOr" Editor="Switch"/>
</dataform:SfDataForm.Items>
</dataform:SfDataForm>
<Button Text="Update Case" Style="{StaticResource RegularButton}" Command="{Binding UpdateSxCaseCommand}" Grid.Row="5" Grid.Column="0" Grid.ColumnSpan="2" HorizontalOptions="Center"></Button>
</StackLayout>
</ContentPage.Content>
User contributions licensed under CC BY-SA 3.0