Native Control上的双向绑定会导致Xamarin Forms跨平台严重崩溃

时间:2018-04-25 20:27:36

标签: binding xamarin.forms crash

应用程序关闭时没有显示任何错误,当我尝试输入一些条目框时,会发生这种情况,我使用SQLite并使用模型中的INotifyPropertyChanged接口将ViewModels与Views绑定。 我不知道为什么会发生这种情况,只要您不尝试输入某个值,应用程序就不会关闭。

这是ViewModel:

class PersonViewModel : PersonModel
{
    public ICommand Save { get; private set; }
    public ICommand Update { get; private set; }
    public ICommand Delete { get; private set; }
    public ICommand New { get; private set; }

    public PersonViewModel()
    {
        Save = new Command(() =>
        {
            PersonModel model = new PersonModel()
            {
                Name = Name,
                Lastname = Lastname,
                Country = Country,
                Email = Email,
                Date = Date,
                City = City
            };

            using (var context = new DataContext()) // Cierra la conexion (Dispose) cuando sale el scope 
            {
                context.Insert(model);
            }
        });

        Update = new Command(() =>
        {
            PersonModel model = new PersonModel()
            {
                Name = Name,
                Lastname = Lastname,
                Country = Country,
                Email = Email,
                Date = Date,
                City = City,
                IdPerson = IdPerson
            };

            using (var context = new DataContext())
            {
                context.Update(model);
            }
        });

        Delete = new Command(() =>
        {
            PersonModel model = new PersonModel()
            {
                Name = Name,
                Lastname = Lastname,
                Country = Country,
                Email = Email,
                Date = Date,
                City = City,
                IdPerson = IdPerson
            };

            using (var context = new DataContext())
            {
                context.Delete(model);
            }
        });

        New = new Command(() =>
        {
            PersonModel model = new PersonModel()
            {
                Name = string.Empty,
                Lastname = string.Empty,
                Country = string.Empty,
                Email = string.Empty,
                Date = DateTime.Now,
                City = string.Empty
            };
        });
    }
}

这是视图:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="ProbandoSqlite.Views.PersonsView">
    <ScrollView>
        <StackLayout>
            <Label Text="Persons" Font="25" TextColor="Green" HorizontalOptions="Center"></Label>
            <Entry Placeholder="Name" Text="{Binding Name}"></Entry>
            <Entry Placeholder="Lastname" Text="{Binding Lastname}"></Entry>
            <Entry Placeholder="Email" Text="{Binding Email}"></Entry>
            <Entry Placeholder="Country" Text="{Binding Country}"></Entry>
            <Entry Placeholder="City" Text="{Binding City}"></Entry>
            <DatePicker Date="{Binding Date}"></DatePicker>

            <Button Text="Save" Command="{Binding Save}"></Button>
            <Button Text="New"  Command="{Binding New}"></Button>
            <Button x:Name="btnShow" Text="Show" Clicked="BtnShow_Clicked"></Button>
        </StackLayout>
    </ScrollView>  
</ContentPage>

这是视图背后的代码:

public partial class PersonsView : ContentPage
{
    public PersonsView ()
    {
        InitializeComponent ();
        this.BindingContext = new PersonViewModel();
    }

    private void BtnShow_Clicked(object sender, EventArgs e)
    {
        ((NavigationPage)Parent).PushAsync(new ShowView());
    }
}

0 个答案:

没有答案