【问题标题】:How do I pass entry parameters to Xamarin MVVM viewmodel如何将条目参数传递给 Xamarin MVVM 视图模型
【发布时间】:2018-03-15 03:22:32
【问题描述】:

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

【问题讨论】:

    标签: xamarin mvvm xamarin.forms commandparameter


    【解决方案1】:

    这是在 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 项访问 LoginCommand

    3) 将Button 的Command 属性绑定到LoginView 中的LoginCommand。确保将其命令源设置为 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>
    

    【讨论】:

      【解决方案2】:

      这个 repo 可能对你有帮助

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

      如果您不熟悉 xamarin 形式的 MVVM,此链接也可能对您有所帮助。

      Basic Understanding How Data Binding and MVVM works in Xamarin Forms

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-22
        • 2019-07-22
        • 1970-01-01
        相关资源
        最近更新 更多