微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从Xamarin的“查看”页面中的条目获取用户数据

如何解决从Xamarin的“查看”页面中的条目获取用户数据

在Xamarin应用程序中,我能够通过 Entry 用户那里获取数据,并将其绑定到 viewmodel.cs 中。

但是,现在我想从 ViewPage 中的Entry中获取用户数据。

下面是.xml页面中Entry的代码,也是我正在使用viewmodel的数据出价的代码(它工作得很好)。

我怎么也可以通过 PageView.xml.cs 中的Entry获取用户数据。

PageView.xml

<Entry
    Text="{Binding Email}"
    Keyboard="Email"
    ReturnType="Done"
    FontSize="15"
    Placeholder="Enter Email" >
</Entry>

<Entry
    Text="{Binding Password}"
    Keyboard="Numberic"
    ReturnType="Done"
    FontSize="15"
    IsPassword="true"
    MaxLenght="8"
    Placeholder="Enter Password" >
</Entry>

<Button
    x:Name="SubmitButton"
    Text="Submit" />

PageView.xml.cs

FormDataButton.Clicked += async (sender,args) =>
{
    if (Email != null && Password != null && Password.Lenght == 8)
    {
        // sample code
        var email = Email;
        var password = Password;
    }
    else{
        Console.WriteLine("Error");
    }
}
我在viewmodel.cs中使用的

数据绑定

private string _Email;
public string Email
{
    get => _Email;
    set => this.RaiseAndSetIfChanged(ref _Email,value);
}

private string _Password;
public string Password
{
    get => _Password;
    set => this.RaiseAndSetIfChanged(ref _Password,value);
}

解决方法

如果您的意思是隐藏代码,则只需为控件分配一个名称

<Entry x:Name="MyEntry"
    Text="{Binding Email}"
    Keyboard="Email"
    ReturnType="Done"
    FontSize="15"
    Placeholder="Enter Email" >
</Entry>

然后在后面的代码中

if (myEntry.Text == "blah") ...

但是,如果您已经在使用数据绑定,那么您可以通过VM来访问值

if (VM.Email == "blah") ...

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。