【问题标题】:Xamarin.forms: Entry unfocus after entering one charXamarin.forms:输入一个字符后输入不聚焦
【发布时间】:2017-12-14 18:12:01
【问题描述】:

我的页面有一个奇怪的行为。

如果我关注我的条目并输入一个字符,事件就会消失(创建新条目),我没有时间输入单词的结尾。

精度不高,我的 DataTemplate 中只有一个条目,我有时间输入我的单词并且预期的行为工作(如果当前条目不为空,则创建新条目)

谢谢帮助,我不明白这个问题

xaml.page

 <ListView SeparatorVisibility="None" HasUnevenRows="True" x:Name="listevisible" ItemsSource="{Binding ChirurgieList}">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <local:Chirurgie>
                            <StackLayout x:Name="{Binding IdEntry}">

                                <StackLayout Orientation="Horizontal">

                                    <Frame BackgroundColor="{StaticResource Leviolet}" Padding="2" HasShadow="False" HorizontalOptions="FillAndExpand" InputTransparent="True">
                                        <Entry  Text="{Binding Chir}"
                                                x:Name="{Binding IdEntry}"
                                                ClassId="{Binding IdEntry}"
                                                Keyboard="Text"                                                   
                                                Placeholder="(vide)"                           
                                                Style="{StaticResource Poursaisi}"                           
                                                FontSize="Medium"                           
                                                BackgroundColor="#f1f0f0"                           
                                                HorizontalTextAlignment="Start"                                                                                                        

                                                Focused="Entry_Focusedchir"
                                                Unfocused="Saisichir_Unfocused"/>
                                    </Frame>

                                    <Frame BackgroundColor="{StaticResource Leviolet}" Padding="2" HasShadow="False" WidthRequest="60" >
                                        <Entry  Text="{Binding Annee}"
                                                x:Name="{Binding Iddate}"
                                                ClassId="{Binding Iddate}"
                                                Keyboard="Numeric"                                                   
                                                Placeholder="(vide)"                           
                                                Style="{StaticResource Poursaisi}"                           
                                                FontSize="Medium"                           
                                                BackgroundColor="#f1f0f0"                           
                                                HorizontalTextAlignment="Center"

                                                Focused="Date_focus"
                                                Unfocused="Date_Unfocus"/>
                                    </Frame>



                                    <StackLayout ClassId="{Binding IdEntry}" 
                                                 Orientation="Horizontal" 
                                                 HorizontalOptions="End" 
                                                 WidthRequest="27" >
                                        <Image Source="{StaticResource effacement}" WidthRequest="20"/>
                                        <Image.GestureRecognizers>
                                            <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped"/>
                                        </Image.GestureRecognizers>
                                        <Image/>
                                    </StackLayout>

                                </StackLayout>

                                <BoxView HeightRequest="5"/>
                            </StackLayout>
                        </local:Chirurgie>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

和c#代码

public partial class Page6 : ContentPage
{
    public ObservableCollection<Chirurgie> listchir = new ObservableCollection<Chirurgie>();
    public string AVsaisi;
    public int compteur = 0;

    public Page6()
    {
        InitializeComponent();

        foreach (var s in (Array)Application.Current.Resources["AtcdChir"])
        { listevisible.ItemsSource = CreateItems(((Chirurgie)s).Chir, ((Chirurgie)s).Annee);}            
    }

    //################### UNFOCUS  #######################
    private void Saisichir_Unfocused(object sender, FocusEventArgs e)
    {                         
        //on definit l'index de l'item 
        int indexCh = 25;
        foreach (var item in ChirurgieList)
        {                
            if (((Entry)sender).ClassId == item.IdEntry.ToString())
            { indexCh = ChirurgieList.IndexOf(item); }
        }

        //-----------------------------
        // zone de saisi est vide 
        if (string.IsNullOrWhiteSpace(((Entry)sender).Text))
        {
            // elle l'était déja
            if(ChirurgieList[indexCh].champsvide == true)
            { } // on ne fait rien

            // elle etait pleine
            else
            {
                //on la supprime
                ChirurgieList.RemoveAt(indexCh);
            }
        }

        // zone de saisi est pleine
        else
        {
            // elle l'était déja
            if (ChirurgieList[indexCh].champsvide == false)
            {
                // on ne fait rien sauf mettre a jour les valeurs
                //ChirurgieList[indexCh].Chir = ((Entry)sender).Text;
                ((Entry)sender).Unfocus();                   
            }

            //et elle était vide
            else
            {
                ChirurgieList[indexCh].champsvide = false;
                ChirurgieList[indexCh].Chir = ((Entry)sender).Text;
                listevisible.ItemsSource = CreateItems(string.Empty, string.Empty);
            } }          



    public ObservableCollection<Chirurgie> CreateItems(string lachir, string lanee)
     {
     var items = ChirurgieList;
        var uniqueID = compteur;

        ChirurgieList.Add(new Chirurgie() {IdEntry = uniqueID, Chir = lachir, Iddate = "A" + uniqueID, Annee = lanee, champsvide=true });                      
        compteur++;
        return items;
}

public class Chirurgie
{
    public string Chir { get; set; }
     public int IdEntry { get; set; }
    public string Iddate { get; set; }
    public string Annee { get; set; }
    public bool champsvide { get; set; }
}

如果我反转两个条目,行为也会改变,所以我认为每个条目都缺少唯一标识符,但我不确定。

【问题讨论】:

    标签: c# listview xamarin.forms observablecollection


    【解决方案1】:

    好的,我改变了我的页面。 网格是一个解决方案:)

    <ListView SeparatorVisibility="None" HasUnevenRows="True" x:Name="listevisible">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell>
                                <Grid ColumnSpacing="2">
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto"/>
                                        <RowDefinition Height="5"/>
                                    </Grid.RowDefinitions>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="60" />
                                        <ColumnDefinition Width="*"/>
                                        <ColumnDefinition Width="30" />
                                    </Grid.ColumnDefinitions>
    
                                    <Frame BackgroundColor="{StaticResource Leviolet}" Padding="2" HasShadow="False" WidthRequest="60" Grid.Column="0" Grid.Row="0" >
                                        <local:EntryAnn  Text="{Binding Itemanee.Lannee}"                                                   
                                                    ClassId="{Binding Itemanee.Iddate}"
                                                    Keyboard="Numeric"                                                   
                                                    Placeholder="(vide)"                           
                                                    Style="{StaticResource Poursaisi}"                           
                                                    FontSize="Medium"                           
                                                    BackgroundColor="#f1f0f0"                           
                                                    HorizontalTextAlignment="Center">
                                        </local:EntryAnn>
                                    </Frame>
    
                                    <Frame BackgroundColor="{StaticResource Leviolet}" Padding="2" HasShadow="False" HorizontalOptions="FillAndExpand" Grid.Column="1" Grid.Row="0">
                                        <local:EntryChir Text="{Binding Itemchir.Chir}"                                                   
                                                         ClassId="{Binding Itemchir.IdEntry}"
                                                         Keyboard="Text"                                                   
                                                         Placeholder="(vide)"                           
                                                         Style="{StaticResource Poursaisi}"                           
                                                         FontSize="Medium"                           
                                                         BackgroundColor="#f1f0f0"                           
                                                         HorizontalTextAlignment="Start"
                                                         Completed="champcomplet"  
                                                         Unfocused="Saisichir_Unfocused"/>
                                    </Frame>
    
                                    <StackLayout ClassId="{Binding Num}" Grid.Column="2" Grid.Row="0" Orientation="Horizontal" HorizontalOptions="End" WidthRequest="27" >
                                        <Image Source="{StaticResource effacement}" WidthRequest="20"/>
                                        <Image.GestureRecognizers>
                                            <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped"/>
                                        </Image.GestureRecognizers>
                                        <Image/>
                                    </StackLayout>
    
                                    <BoxView HeightRequest="5" Grid.Column="0" Grid.Row="1"/>
                                    <BoxView HeightRequest="5" Grid.Column="1" Grid.Row="1"/>
                                    <BoxView HeightRequest="5" Grid.Column="2" Grid.Row="1"/>
    
                                </Grid>
                                </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
    

    和后面的代码

        private void champcomplet(object sender, EventArgs e)    
        {
            ((EntryChir)sender).Unfocus();
    
            testeur.Text = ((EntryChir)sender).ClassId.ToString();
    
             //on definit l'index de l'item 
            int indexCh = 25;
             foreach (var item in Conteneur)
             {                
                 if (((EntryChir)sender).ClassId == item.Num.ToString())  // tester Entrychir partout
                 { indexCh = Conteneur.IndexOf(item); }
             }
    
             //-----------------------------
             // zone de saisi est vide 
             if (string.IsNullOrWhiteSpace(((EntryChir)sender).Text))
             {
                 // elle l'était déja
                 if(Conteneur[indexCh].Itemchir.Champsvide == true)
                 { } // on ne fait rien
    
                 // elle etait pleine
                 else
                 {
                     //on la supprime
                     Conteneur.RemoveAt(indexCh);                   
                 }
             }
    
             // zone de saisi est pleine
             else
             {
                 // elle l'était déja
                 if (Conteneur[indexCh].Itemchir.Champsvide == false)
                 {                    
                     // on ne fait rien sauf mettre a jour les valeurs
                     //ChirurgieList[indexCh].Chir = ((Entry)sender).Text;
                }
    
                 //et elle était vide
                 else
                 {
                     Conteneur[indexCh].Itemchir.Champsvide = false;
                     Conteneur[indexCh].Itemchir.Chir = ((Entry)sender).Text;
    
                     listevisible.ItemsSource = Creaconteneur("", "");
                     //listevisible.ItemsSource = CreateItems(string.Empty, string.Empty);
    
                 }
             }           
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-03
      • 2021-12-14
      • 1970-01-01
      • 2022-01-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多