Xamarin.forms:输入一个字符后输入不重点

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

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

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

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

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

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

c# listview xamarin.forms observablecollection
1个回答
0
投票

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

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

             }
         }           
    }
© www.soinside.com 2019 - 2024. All rights reserved.