简体   繁体   English

Xamarin Forms 在绑定到 Enrty 的文本属性时冻结

[英]Xamarin Forms Freezes on Binding to text property of Enrty

Not sure how this is happening but two of my Entry elements will freeze the entire application when their Text is bound.不知道这是怎么发生的,但是我的两个 Entry 元素会在它们的 Text 被绑定时冻结整个应用程序。 They look essentially the same as everywhere else Entries are used within the app, the only difference being, when included the application freezes.它们看起来与在应用程序中使用的任何其他条目基本相同,唯一的区别是,当包含应用程序时,应用程序会冻结。 It just doesn't make sense why these break but the others, which are setup exactly the same, don't.为什么这些会中断是没有意义的,但其他设置完全相同的却不会。 Any help, advice, or suggestions are appreciated!任何帮助,建议或建议表示赞赏!

Offending entries:违规条目:

 <Label Margin="3">Professor Phone</Label>
 <Entry Text="{Binding ProfessorPhone}" Placeholder="Professor Email" Margin="3" />
 <Label Margin="3">Professor Email</Label>
 <Entry Text="{Binding ProfessorEmail}" Placeholder="Professor Phone" />

View:看法:

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="TermManager.Views.CourseDetailView">
<ContentPage.ToolbarItems>
    <ToolbarItem Text="Update" />
</ContentPage.ToolbarItems>
<ContentPage.Content>
    <ScrollView>
        <StackLayout>
            <Label Text="Course Details" VerticalOptions="Center" HorizontalTextAlignment="Center" IsVisible="true" FontSize="Large" FontAttributes="Bold" TextColor="DarkBlue" Margin="10" />
            <Label Margin="3">Course Title</Label>
            <Entry Text="{Binding CourseTitle}" Placeholder="Course Title" Margin="3" />
            <Label Margin="3">Start Date</Label>
            <DatePicker Date="{Binding StartCourseDate}" Margin="3" />
            <Label Margin="3">End Date</Label>
            <DatePicker Date="{Binding EndCourseDate}" Margin="3" />
            <Picker SelectedItem="{Binding CourseStatus}" Title="Course Status" Margin="3" TitleColor="DarkBlue">
                <Picker.ItemsSource>
                    <x:Array Type="{x:Type x:String}">
                        <x:String>in progress</x:String>
                        <x:String>completed</x:String>
                        <x:String>dropped</x:String>
                        <x:String>plan to take</x:String>
                    </x:Array>
                </Picker.ItemsSource>
            </Picker>
            <Label Margin="3">Professor Name</Label>
            <Entry Text="{Binding ProfessorName}" Placeholder="Professor's Name" Margin="3" />
            <Label Margin="3">Professor Email</Label>
            <Entry Text="{Binding ProfessorPhone}" Placeholder="Professor Email" Margin="3" />
            <Label Margin="3">Professor Phone</Label>
            <Entry Text="{Binding ProfessorEmail}" Placeholder="Professor Phone" />
        </StackLayout>
    </ScrollView>
</ContentPage.Content>

Code Behind:代码背后:

using System;
using System.Collections.Generic;
using TermManager.Models;
using TermManager.ViewModels;
using Xamarin.Forms;

namespace TermManager.Views
{
    public partial class CourseDetailView : ContentPage
    {
        protected Course Course = new Course();

        public CourseDetailView(Course course)
        {
            InitializeComponent();
            BindingContext = new CourseDetailViewModel(Navigation, course);
        }
    }
}

ViewModel:视图模型:

using System;
using Xamarin.Forms;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using TermManager.Models;

namespace TermManager.ViewModels
{
    public class CourseDetailViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public Command UpdateCourse { get; private set; }
        protected readonly INavigation Nav;

        protected int _courseId;
        protected int _termId;
        protected string _courseTitle;
        protected DateTime _startCourseDate;
        protected DateTime _endCourseDate;
        protected string _courseStatus;
        protected string _name;
        protected string _email;
        protected string _phone;

        public int CourseId
        {
            get => _courseId;
            set
            {
                _courseId = value;
                OnPropertyChanged();
            }
        }
        public int TermId
        {
            get => _termId;
            set
            {
                _termId = value;
                OnPropertyChanged();
            }
        }
        public string CourseTitle
        {
            get => _courseTitle;
            set
            {
                _courseTitle = value;
                OnPropertyChanged();
            }
        }
        public DateTime StartCourseDate
        {
            get => _startCourseDate;
            set
            {
                _startCourseDate = value;
                OnPropertyChanged();
            }
        }
        public DateTime EndCourseDate
        {
            get => _endCourseDate;
            set
            {
                _endCourseDate = value;
                OnPropertyChanged();
            }
        }
        public string CourseStatus
        {
             get => _courseStatus;
            set
            {
                _courseStatus = value;
                OnPropertyChanged();
            }
        }
        public string ProfessorName
        {
            get => _name;
            set
            {
                _name = value;
                OnPropertyChanged();
            }
        }
        public string ProfessorEmail
        {   
            get => ProfessorEmail;
            set
            {
                _email = value;
                OnPropertyChanged();
            }
        }
        public string ProfessorPhone
        {
            get => ProfessorPhone;
            set
            {
                _phone = value;
                OnPropertyChanged();
            }
        }

        public CourseDetailViewModel(INavigation nav, Course course = null)
        {
            Nav = nav;

            if (course != null)
            {
                _courseId = course.CourseId;
                _termId = course.TermId;
                _courseTitle = course.CourseTitle;
                _courseStatus = course.CourseStatus;
                _startCourseDate = course.StartCourseDate;
                _endCourseDate = course.EndCourseDate;
                _name = course.ProfessorName;
                _email = course.ProfessorEmail;
                _phone = course.ProfessorPhone;
            }
        }

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

The problem is in your getters for the ProfessorPhone and ProfessorEmail properties, there you have to return the protected fields, but instead you are returning the property itself, which causes an infinite loop!问题出在ProfessorPhoneProfessorEmail属性的getter 中,您必须返回受保护的字段,而是返回属性本身,这会导致无限循环!

Change your code as follows:更改您的代码如下:

public string ProfessorEmail
{   
    get => _email;
    set
    {
        _email = value;
        OnPropertyChanged();
    }
}
public string ProfessorPhone
{
    get => _phone;
    set
    {
        _phone = value;
        OnPropertyChanged();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM