如何在Xamarin中使用Entry进行文本显示和编辑?

问题描述 投票:0回答:2

我有账户信息页面。当用户点击编辑时,我想让他们编辑他们的信息。

但我不希望用户转到另一个页面,因为它是完全相同的页面,但只有可编辑的字段。我可以重用现有页面以某种方式进行编辑吗?我尝试将字段类型更改为 Entry 但它只提供输入字段并且不显示信息。或者我应该创建新页面进行编辑吗?

当我使用标签时它正常显示,就像上面的截图一样:

 <Label x:Name ="patientPhone"/>

但是我无法编辑标签。我必须使用 Entry?

我尝试将 Entry 与 isenabled false 一起使用,但它只显示一位数字(完整电话号码之外):

带入口的代码:

<Entry x:Name ="patientPhone" VerticalOptions="CenterAndExpand"  IsEnabled="false" HorizontalOptions="EndAndExpand"/>
c# forms xamarin crud edit
2个回答
1
投票

根据您的查询,我给出了如下所示的解决方案。

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="TestDemoStack.MainPage">

    <StackLayout Orientation="Vertical">

        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="60"  />
                <RowDefinition Height="60" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="100" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>

            <Label  Text="Address:"  Grid.Column="0" Grid.Row="0" VerticalOptions="Center"  />

            <Label x:Name="patientAddressLabel" Text="Your Address"  Grid.Column="1" Grid.Row="0" VerticalOptions="Center"  />

            <Entry x:Name ="patientAddressEntry"  Text="Your Address" Grid.Column="1" Grid.Row="0" BackgroundColor="Yellow"   IsVisible="false" />

            <Label  Text="Phone:" Grid.Row="1" Grid.Column="0" VerticalOptions="Center" />

            <Label x:Name="patientPhoneLabel" Text="123456789" Grid.Row="1" Grid.Column="1" VerticalOptions="Center" />

            <Entry x:Name ="patientPhoneEntry" Text="123456789"  Grid.Row="1" Grid.Column="1"    BackgroundColor="Yellow"  IsVisible="false" />
        </Grid>

        <Button x:Name="btn_click" Text="Edit" Clicked="Button_Clicked"/>

    </StackLayout>

</ContentPage>

请在 Xaml.cs 类文件中添加此代码,以便管理按钮单击均匀。

Xaml.cs类文件代码:-

namespace TestDemoStack
{
    public partial class MainPage : ContentPage
    {
        bool isViewEditable = true;
        public MainPage()
        {
            InitializeComponent();
        }

        private void Button_Clicked(object sender, EventArgs e)
    {
        if(isViewEditable == true)
        {
            isViewEditable = false;
            patientAddressEntry.Text = patientAddressLabel.Text;
            patientPhoneEntry.Text = patientPhoneLabel.Text;

            patientAddressEntry.IsVisible = true;
            patientPhoneEntry.IsVisible = true;

            patientAddressLabel.IsVisible = false;
            patientPhoneLabel.IsVisible = false;

            btn_click.Text = "Save";
        }
        else
        {
            isViewEditable = true;

            patientAddressEntry.IsVisible = false;
            patientPhoneEntry.IsVisible = false;

            patientAddressLabel.Text = patientAddressEntry.Text;
            patientPhoneLabel.Text = patientPhoneEntry.Text;

            patientAddressLabel.IsVisible = true;
            patientPhoneLabel.IsVisible = true;

            btn_click.Text = "Edit";
        }
        
    }
    }
}

我认为这个解决方案对你有帮助。

谢谢。


1
投票

是的,您可以使用

Entry
来实现这一点并为其设置
IsEnabled
。而对于你的布局,建议你使用
Grid
来做。

请参考以下代码:

<StackLayout Orientation="Vertical"> 

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="60"  />
            <RowDefinition Height="60" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="100" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>

        <Label  Text="Address:"  Grid.Column="0" Grid.Row="0" VerticalOptions="Center"  />

        <Entry x:Name ="patientAddress"  Text="address 1" Grid.Column="1" Grid.Row="0" BackgroundColor="Yellow"   IsEnabled="false" />

        <Label  Text="Phone:" Grid.Row="1" Grid.Column="0" VerticalOptions="Center" />

        <Entry x:Name ="patientPhone" Text="address 2"  Grid.Row="1" Grid.Column="1"    BackgroundColor="Yellow"  IsEnabled="false" />
    </Grid>

    <Button Text="Edit" Clicked="Button_Clicked"/>

</StackLayout>

MainPage.xaml.cs

public partial class MainPage : ContentPage 
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void Button_Clicked(object sender, EventArgs e)
        {
            patientAddress.IsEnabled = true; 
            patientPhone.IsEnabled = true;  
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.