首页 > 解决方案 > Xamarin.Forms 条目文本已添加但未显示

问题描述

在此处输入图像描述

我在启动应用程序时制作了一个 Pin Validator。这是我的代码:

   <ContentPage.Content>


        <StackLayout Spacing="10" VerticalOptions="Center" Margin="10,0,10,0" >
            <Grid x:Name="LayoutRoot">
                <Grid.RowDefinitions>
                    <RowDefinition Height="1.3*" />
                    <RowDefinition Height="*" />
                    <RowDefinition Height="0.5*" />
                    <RowDefinition Height="*" />
                    <RowDefinition Height="*" />
                    <RowDefinition Height="*" />
                    <RowDefinition Height="*" />
                    <RowDefinition Height="*" />
                    <RowDefinition Height="*" />

                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="2*" />
                    <ColumnDefinition Width="2*" />
                    <ColumnDefinition Width="2*" />
                    <ColumnDefinition Width="*" />

                </Grid.ColumnDefinitions>

                <Grid.RowSpacing>
                    <OnPlatform x:TypeArguments="x:Double" Android="20"  iOS="20"  />
                </Grid.RowSpacing>

                <Grid.ColumnSpacing>
                    <OnPlatform x:TypeArguments="x:Double" Android="20"  iOS="20"  />
                </Grid.ColumnSpacing>

                <Frame BorderColor="#006BE6"  BackgroundColor="WhiteSmoke"  Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="3" >
                    <Entry TextColor="#006BE6"  FontSize="48" IsPassword="True" x:Name="Output" Text="{Binding Pin}"  HorizontalOptions="Center" VerticalOptions="Center" IsReadOnly="True">
                        <Entry.Behaviors>
                            <behaviors:EntryLengthValidatorBehavior MaxLength="4" />
                        </Entry.Behaviors>
                    </Entry>
                </Frame>



                <Button TextColor="#006BE6" Text="1" Grid.Row="3" Grid.Column="1" Command="{Binding LoadNumberCommand}" CommandParameter="1" BorderColor="#006BE6" BorderWidth="1" BackgroundColor="WhiteSmoke"    />

                <Button TextColor="#006BE6" Text="2" Grid.Row="3" Grid.Column="2" Command="{Binding LoadNumberCommand}" CommandParameter="2" BorderColor="#006BE6" BorderWidth="1" BackgroundColor="WhiteSmoke"/>
                <Button TextColor="#006BE6" Text="3" Grid.Row="3" Grid.Column="3" Command="{Binding LoadNumberCommand}" CommandParameter="3" BorderColor="#006BE6" BorderWidth="1" BackgroundColor="WhiteSmoke"/>

                <Button TextColor="#006BE6"  Text="4" Grid.Row="4" Grid.Column="1" Command="{Binding LoadNumberCommand}" CommandParameter="4" BorderColor="#006BE6" BorderWidth="1" BackgroundColor="WhiteSmoke"/>
                <Button TextColor="#006BE6"  Text="5" Grid.Row="4" Grid.Column="2" Command="{Binding LoadNumberCommand}" CommandParameter="5" BorderColor="#006BE6" BorderWidth="1" BackgroundColor="WhiteSmoke"/>
                <Button  TextColor="#006BE6" Text="6" Grid.Row="4" Grid.Column="3" Command="{Binding LoadNumberCommand}" CommandParameter="6" BorderColor="#006BE6" BorderWidth="1" BackgroundColor="WhiteSmoke"/>


                <Button TextColor="#006BE6"  Text="7" Grid.Row="5" Grid.Column="1" Command="{Binding LoadNumberCommand}" CommandParameter="7" BorderColor="#006BE6" BorderWidth="1" BackgroundColor="WhiteSmoke"/>
                <Button TextColor="#006BE6" Text="8" Grid.Row="5" Grid.Column="2" Command="{Binding LoadNumberCommand}" CommandParameter="8" BorderColor="#006BE6" BorderWidth="1" BackgroundColor="WhiteSmoke"/>
                <Button TextColor="#006BE6" Text="9" Grid.Row="5" Grid.Column="3" Command="{Binding LoadNumberCommand}" CommandParameter="9" BorderColor="#006BE6" BorderWidth="1" BackgroundColor="WhiteSmoke"/>

                <Button TextColor="#006BE6" Text="C" Grid.Row="6" Grid.Column="1" Command="{Binding LoadNumberCommand}" CommandParameter="C"  BorderColor="#006BE6" BorderWidth="1" BackgroundColor="WhiteSmoke"/>
                <Button TextColor="#006BE6" Text="0" Grid.Row="6" Grid.Column="2" Command="{Binding LoadNumberCommand}" CommandParameter="0" BorderColor="#006BE6" BorderWidth="1" BackgroundColor="WhiteSmoke"/>
                <Button TextColor="#006BE6" Text="OK" Grid.Row="6" Grid.Column="3" Command="{Binding LoadNumberCommand}" CommandParameter="OK"  BorderColor="#006BE6" BorderWidth="1" BackgroundColor="WhiteSmoke"/>

            </Grid>
        </StackLayout>
    </ContentPage.Content>

一切正常,只是条目中的文本没有显示。我的想法是添加 4 位密码。绑定引脚从数字中获取值,但未显示在条目中。我不知道是什么问题。有人帮忙吗?

在 ViewModel 中添加 Pin 和 Command

 public string pin;
        public string Pin
        {
            get { return pin; }
            set
            {
                if (pin != value)
                {
                    pin = value;

                    SetProperty(ref pin, value);
                    OnPropertyChanged("Pin");

                }
            }
        }

和命令


LoadNumberCommand = new Command(execute: async (value) => await ExecutePinElement(value));

标签: xamarin.formsxamarin.androidxamarin.ios

解决方案


将您的条目IsReadOnly属性设置为 false

或者

更改HorizontalOptions"FillAndExpand"并添加HorizontalTextAlignment="Center"


推荐阅读