Приложение xamarin forms зависает при привязке к datetime, допускающему значение NULL - PullRequest
0 голосов
/ 01 августа 2020

При попытке отобразить данные поверх формы в приложении Xamarin Forms свойство модели, имеющее значение NULL DateTime, останавливает приложение. Я знаю, что это оскорбительное свойство, потому что, если я удалю Xaml для этой привязки, все будет работать нормально.

При пошаговом выполнении кода свойство LoginTime из источника (получено из db) и getter из свойства User ViewModel переходят в бесконечное l oop. Я не могу себе представить, почему.

Модель:

        public class User : PersistentEntity
    {
        private string _userId;
        public string UserId
        {
            get => _userId;
            set => SetProperty(ref _userId, value);
        }
    
        private string _userName;
        public string UserName
        {
            get => _userName;
            set => SetProperty(ref _userName, value);
        }
    
        private string _email;
        public string Email
        {
            get => _email;
            set => SetProperty(ref _email, value);
        }
    
        private DateTime? _loginTime;
        public DateTime? LoginTime
        {
            get => _loginTime;
            set => SetProperty(ref _loginTime, value);
        }
    }

ViewModel:

public class SettingsViewModel : ViewModelBase
    {
        private readonly SettingsSession _settingsSession;

        private User _user;
        public User User
        {
            get => _user;
            set => SetProperty(ref _user, value);
        }


        public SettingsViewModel(INavigationService navigationService, SettingsSession settingsSession)
            : base(navigationService)
        {
            Title = "Settings";

            _settingsSession = settingsSession;
        }

        private DelegateCommand _fetchUserCommand;
        public DelegateCommand FetchUserCommand =>
            _fetchUserCommand ?? (_fetchUserCommand = new DelegateCommand(ExecuteFetchUserCommand, CanExecuteFetchUserCommand));

        async void ExecuteFetchUserCommand()
        {
           User = await _settingsSession.FetchUser();
        }

        bool CanExecuteFetchUserCommand()
        {
            return true;
        }

    }

View:

    <TableView Intent="Settings">
        <TableRoot>
            <TableSection Title="User">

                <ViewCell>
                    <StackLayout Orientation="Horizontal"
                                 Padding="2">
                        <Label Text="User Id"
                               WidthRequest="150" 
                               Margin="5,0,0,0"
                               VerticalOptions="CenterAndExpand"/>
                        <Entry WidthRequest="250" 
                               HorizontalTextAlignment="Start"
                               VerticalTextAlignment="Center"
                               Placeholder="UserId" 
                               Text="{Binding User.UserId}"/>
                    </StackLayout>
                </ViewCell>

                <ViewCell>
                    <StackLayout Orientation="Horizontal"
                                 Padding="2">
                        <Label Text="User Name"
                               WidthRequest="150" 
                               Margin="5,0,0,0"
                               VerticalOptions="CenterAndExpand"/>
                        <Entry WidthRequest="250" 
                               HorizontalTextAlignment="Start"
                               VerticalTextAlignment="Center"
                               Placeholder="User Name" 
                               Text="{Binding User.UserName}"/>
                    </StackLayout>
                </ViewCell>

                <ViewCell>
                    <StackLayout Orientation="Horizontal"
                                 Padding="2">
                        <Label Text="Login Time"
                               WidthRequest="150" 
                               Margin="5,0,0,0"
                               VerticalOptions="CenterAndExpand"/>
                        <Entry WidthRequest="250" 
                               HorizontalTextAlignment="Start"
                               VerticalTextAlignment="Center"
                               Placeholder="Login Time" 
                               Text="{Binding User.LoginTime, StringFormat='{0:dd-MM-yyyy HH:mm}'}"/>
                    </StackLayout>
                </ViewCell>

            </TableSection>
        </TableRoot>
    </TableView>

Любая помощь оценен.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...