代码之家  ›  专栏  ›  技术社区  ›  pyriame

沙马林。表单:输入一个字符后取消焦点输入

  •  0
  • pyriame  · 技术社区  · 7 年前

    我的页面中有一种奇怪的行为。

    如果我集中输入条目,并输入一个字符,事件将关闭(创建新条目),我没有时间输入单词的结尾。

    精度很低,在我的数据模板中只有一个条目,我有时间输入我的单词和预期的行为(如果当前条目不为空,则创建新条目)

    thx寻求帮助,我不理解问题所在

    xaml。页

     <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; }
    }
    

    如果我反转这两个条目,行为也会改变,因此我认为这一定与每个条目缺少唯一标识符有关,但我不确定。

    1 回复  |  直到 7 年前
        1
  •  0
  •   pyriame    7 年前

    好的,我更改页面。 网格解决方案:)

    <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);
    
                 }
             }           
        }