如何将入口参数传递给 Xamarin MVVM 视图模型 [英] How do I pass entry parameters to Xamarin MVVM viewmodel

查看:14
本文介绍了如何将入口参数传递给 Xamarin MVVM 视图模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I was looking for the best way to pass entry parameters (username, password) to Xamarin MVVM viewmodel by clicking a button(in the view) with command parameters.

推荐答案

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

This is an example of passing Username and Password in Xamarin MVVM pattern. It works fine:

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

1)Create a LoginViewModel which will contain your commands. Make sure the ViewModel implements 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 分配一个名称,以便您可以从列表项中访问 LoginCommand

2) In your LoginView's markup, assign your ListView a name so that you can access the LoginCommand from theList Item

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

3)Bind the Button's Command property to the LoginCommand in your LoginView. Make sure to set its Command Source to the BindingContext used by the ListView which will point to the LoginViewModel. And bind the Button's CommandParameter to the current list item:

[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>

这篇关于如何将入口参数传递给 Xamarin MVVM 视图模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆