如何将条目参数传递给Xamarin MVVM视图模型

时间:2017-10-03 20:27:38

标签: xamarin mvvm xamarin.forms commandparameter

我一直在寻找通过命令参数单击按钮(在视图中)将条目参数(用户名,密码)传递给Xamarin MVVM视图模型的最佳方法。

2 个答案:

答案 0 :(得分:2)

这是在Xamarin MVVM模式中传递用户名和密码的示例。它工作正常:

1)创建一个包含命令的LoginViewModel。确保ViewModel实现了INotifyPropertyChanged:

public class LoginViewModel:INotifyPropertyChanged
{
    private ObservableCollection<CredentialsModel> _listOfItems=new 
    ObservableCollection<CredentialsModel>();
    public ObservableCollection<CredentialsModel> ListOfItems
    {
        get { return _listOfItems; }
        set
        {
            if (_listOfItems != value)
            {
                _listOfItems = value;
                RaisePropertyChanged();
            }
        }
    }
    private string username = null;
    private string password = null;
    private string loginMessage = null;
    public string Username { get { return username; } set { username = 
    value; } }
    public string Password { get { return password; } set { password = 
    value; } }
    public string LoginMessage { get { return loginMessage; } set { 
    loginMessage = value; RaisePropertyChanged(); } }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChanged([CallerMemberName]string caller="")
    {
        if(PropertyChanged!=null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(caller));
        }
    }

    public ICommand LoginCommand
    {
        get;
        private set;
    }

    public LoginViewModel()
    {
        ListOfItems.Add(new CredentialsModel());
        LoginCommand = new Command((e) =>
        {
            var item = (e as CredentialsModel);
            //TODO: LOGIN TO YOUR SYSTEM
            loginMessage = string.Concat("Login successful for user: ", 
            item.Username);
        });
    }
 }

2)在LoginView的标记中,为ListView分配一个名称,以便您可以从List Item访问LoginCommand

3)将Button的Command属性绑定到LoginView中的LoginCommand。确保将其Command Source设置为ListView使用的BindingContext,它将指向LoginViewModel。并将Button的CommandParameter绑定到当前列表项:

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class LoginView : ContentPage
{
    public LoginView()
    {
        InitializeComponent();
        BindingContext = new LoginViewModel();
    }

}


<?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="MyApp.View.LoginView">
    <ContentPage.Content>
    <StackLayout>
        <control:MenuView />
        <ListView x:Name="LoginList" ItemsSource="{Binding ListOfItems, 
           Mode=TwoWay}" SeparatorVisibility="None">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout Orientation="Horizontal">
                            <Entry Placeholder="Enter user name" Text="
                              {Binding Username, Mode=TwoWay}" 
                              x:Name="Username"/>
                            <Entry Placeholder="Enter password" Text="
                              {Binding Password, Mode=TwoWay}"  
                               x:Name="Password" />
                            <Button Text="Login" 
                                    Command="{Binding 
                                    Path=BindingContext.LoginCommand, 
                                    Source={x:Reference LoginList}}"
                                    CommandParameter="{Binding}">
                            </Button>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
        <Label Text="{Binding LoginMessage}"></Label>
     </StackLayout>
     </ContentPage.Content>
   </ContentPage>

答案 1 :(得分:2)

此回购可能对您有所帮助

https://github.com/deanilvincent/Xamarin-Forms-Simple-MVVM-Login

如果您是xamarin表单中的MVVM新手,此链接也可能对您有所帮助。

Basic Understanding How Data Binding and MVVM works in Xamarin Forms