I have a UWP WinRt App.
I use MVVM Light.
I have a ListView on the MainPage. When I press on a Item with A x:Bind RelayCommand i go to a DetailsView of the Item. If I debug on a Lumia 635 I got an Error after pressing on a Item three times.
=> I press on a Item => go to DetailsView and then i press Back and repeat this, but third time pressing it the error occurs.
I never execute the command. After three Times I got this Error. Its in the constructor of the DetailsViewModel.
If I comment this out, another RelayCommand raise this exception.. so I think i forgot something to implement and I dont get it..
Detail:
My Service in the Portable Project looks like this
public interface ISystemcontactsService
{
bool CanAdd { get; }
Task AddContactToSystemcontacts(Contact contact);
}
My implementation in the UWP Project:
public class UwpSystemcontactsService : ISystemcontactsService
{
public bool CanAdd => false;
public async Task AddContactToSystemcontacts(BL.Model.Contact contact)
{
}
}
My ViewModelLocator
public class UwpViewModelLocator : ViewModelLocator
{
static UwpViewModelLocator()
{
//if (!GalaSoft.MvvmLight.ViewModelBase.IsInDesignModeStatic)
{
SimpleIoc.Default.Register<INavigationService>(CreateUwpNavigationService);
SimpleIoc.Default.Register<ICallerService, UwpCallerService>();
SimpleIoc.Default.Register<IEmailerService, UwpEmailerService>();
SimpleIoc.Default.Register<IConnectivityCheckerService, ConnectivityCheckerService>();
SimpleIoc.Default.Register<ILocationService, UwpLocationService>();
SimpleIoc.Default.Register<ISystemcontactsService, UwpSystemcontactsService>();
SimpleIoc.Default.Register<IAddressResolverService, UwpAddressResolverService>();
}
}
static INavigationService CreateUwpNavigationService()
{
var navigationService = new UwpNavigationService();
navigationService.Configure(Navigation.Details, typeof(DetailPage));
navigationService.Configure(Navigation.Main, typeof(MainPage));
return navigationService;
}
}
The implementation in the DetailsViewModel
public class DetailsViewModel : ViewModelBase
{
private readonly ISystemcontactsService _contactService;
public DetailsViewModel(ISystemcontactsService contactService)
{
PropertyChanged += (sender, args) =>
{
if (args.PropertyName == nameof(CanAddContactToSystemContacts)) ((RelayCommand)AddContactToSystemContactsCommand).RaiseCanExecuteChanged();
};
AddContactToSystemContactsCommand = new RelayCommand(AddContactToSystemContacts, () => CanAddContactToSystemContacts);
}
public bool CanAddContactToSystemContacts => _contactService.CanAdd && Contact != null;
public ICommand AddContactToSystemContactsCommand { get; private set; }
private void AddContactToSystemContacts()
{
_contactService.AddContactToSystemcontacts(Contact);
}
XAML - DetailPage
<AppBarButton Icon="SaveLocal"
Label="Lokal Speichern"
Command="{x:Bind ViewModel.AddContactToSystemContactsCommand}" />
WinRT, C#, UWP, MVVM-Light, Fody, Win10