Xamarin.Android 不断停止

Xamarin.Android Keeps Stopping

我是 MVVM 模式和 Xamarin 平台的新手。 我试图通过使用视图模型创建一个可绑定的界面。 我从一个非常简单的任务开始:获取用户输入的 Entry,并在单击按钮后将其发送到标签文本。

ViewModel ---> 命令 ---> EntryCommand.cs

namespace HelloWorld.ViewModel.Commands
{
    public class EntryCommand : ICommand
    {
        public ViewModel VM { get; set; }
        public EntryCommand( ViewModel vm)
        {
            VM = vm;
        }

        public event EventHandler CanExecuteChanged;

        public bool CanExecute(object parameter)
        {
            return true;
        }
        
        public void Execute(object parameter)
        {
            VM.ChangeLabelText();
        }
    }
}

ViewModel.cs

namespace HelloWorld.ViewModel
{
    public class ViewModel: INotifyPropertyChanged
    {  
        public EntryCommand EntryCommand { get; set; }
        public event PropertyChangedEventHandler PropertyChanged;

        public ViewModel()
        {
            EntryCommand = new EntryCommand(this);
        }

        private void OnPropertyChanged(string PropertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(PropertyName));
        }

        public string LabelText { get; set; }
        public string Name 
        {
            get { return Name; }
            set 
            {
                Name = value;
                OnPropertyChanged("Name");
            }
        }
        public void ChangeLabelText()
        {
            if(Name!=null)
            {
                LabelText = Name;
            }
        }

    }
}

可绑定Xaml文件:

MainPage.xaml

<?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="HelloWorld.MainPage"
             xmlns:ViewModel="clr-namespace:HelloWorld.ViewModel" x:DataType="ViewModel:ViewModel">

    <ContentPage.Resources>
        <ViewModel:ViewModel x:Key="vm"/>
    </ContentPage.Resources>

    <StackLayout BindingContext="{StaticResource vm}" BackgroundColor="AliceBlue"
                 Margin="15">

        <Label Text="{Binding Name, Mode=TwoWay}"/>
        <Entry Placeholder="Enter Your Name" Text="{Binding Name, Mode=TwoWay}"/>
        <Button Text="Enter" Command="{Binding EntryCommand}"/>

    </StackLayout>

</ContentPage> 

现在,我没有收到任何错误,但模拟器在构建 UI:

之前一直停止

enter image description here

我认为您使用的 ViewModel 不正确。

根据自己的需要,不需要创建EntryCommand: ICommand

你可以这样做:

public ICommand EntryCommand => new Command(DoSomething);

public void DoSomething() { 
    
        // do some thing here
    }

此外,Name的使用也不正确,需要另外创建一个变量(如private string _name;

    private string _name;
    public string Name
    {
        set { SetProperty(ref _name, value); }
        get { return _name; }
    }

您可以在这里参考完整的示例代码:

   public class MyViewModel: INotifyPropertyChanged
    {
        public ICommand EntryCommand => new Command(DoSomething);

        public MyViewModel() {
            Name = "abc123";

        }
      

        public void DoSomething() { 
        
            // do some thing here
        }



        public string LabelText { get; set; }


        private string _name;
        public string Name
        {
            set { SetProperty(ref _name, value); }
            get { return _name; }
        }



        public void ChangeLabelText()
        {
            if (Name != null)
            {
                LabelText = Name;
            }
        }


        bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
        {
            if (Object.Equals(storage, value))
                return false;

            storage = value;
            OnPropertyChanged(propertyName);
            return true;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string PropertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(PropertyName));
        }

    }

在xaml中使用时,可以设置BindingContext如下:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:testapp0211="clr-namespace:TestApp0211"
             x:Class="TestApp0211.MainPage">


    <ContentPage.BindingContext>
        <testapp0211:MyViewModel />
    </ContentPage.BindingContext>

    <StackLayout  BackgroundColor="AliceBlue"
                 Margin="15">

        <Label Text="{Binding Name, Mode=TwoWay}"/>
        <Entry Placeholder="Enter Your Name" Text="{Binding Name, Mode=TwoWay}"/>
        <Button Text="Enter" Command="{Binding EntryCommand}"/>

    </StackLayout>
</ContentPage>

注:

建议您重命名 class ViewModel(例如 MyViewModel)以更好地区分不同的视图模型。