我如何在relaycommand中解决COMException?我错过了什么吗?

时间:2016-01-21 12:44:36

标签: c# debugging mvvm-light uwp relaycommand

我有一个UWP WinRt应用程序。

我使用MVVM Light。

我在MainPage上有一个ListView。当我按下带有A x:Bind RelayCommand的项目时,我会转到项目的详细信息视图。如果我在Lumia 635上进行调试,则在按下一个项目三次后出现错误。

=>我按下一个Item =>转到DetailsView,然后我按Back并重复此操作,但第三次按下它会发生错误

我从不执行命令。 三次之后我得到了这个错误。它在DetailsViewModel的构造函数中。

如果我发表评论,另一个RelayCommand会引发这个异常..所以我想我忘记了要实现的东西,我不明白......

Error

详情:

Detail

便携式项目中的我的服务看起来像这样

 public interface ISystemcontactsService
{
    bool CanAdd { get; }
    Task AddContactToSystemcontacts(Contact contact);
}

我在UWP项目中的实施:

public class UwpSystemcontactsService : ISystemcontactsService
{
public bool CanAdd => false;

    public async Task AddContactToSystemcontacts(BL.Model.Contact contact)
    {

    }
}

我的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;
    }
}

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

0 个答案:

没有答案