IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Windows Phone .NET Discussion :

[MVVM] Erreur bizarre dans une Listbox


Sujet :

Windows Phone .NET

  1. #1
    Membre expérimenté Avatar de DotNET74
    Homme Profil pro
    Watch R&D Engineer & Apprenti .NET
    Inscrit en
    Août 2003
    Messages
    1 986
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 51
    Localisation : France

    Informations professionnelles :
    Activité : Watch R&D Engineer & Apprenti .NET

    Informations forums :
    Inscription : Août 2003
    Messages : 1 986
    Points : 1 453
    Points
    1 453
    Par défaut [MVVM] Erreur bizarre dans une Listbox
    Hello,

    J'ai une ListBox comme suit:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    <Grid Background="Transparent">
     <Grid.RowDefinitions>
      <RowDefinition></RowDefinition>
      <RowDefinition Height="60"></RowDefinition>
     </Grid.RowDefinitions>
     <ListBox x:Name="ListBoxCategories" ItemsSource="{Binding Path=Categories}" SelectedItem="{Binding Path=SelectedCategory,Mode=TwoWay}" Grid.Row="0">
      <ToolKit:ContextMenuService.ContextMenu>
       <ToolKit:ContextMenu>
       <ToolKit:MenuItem x:Name="ContextMenuMenuItemStock" Header="{Binding Path=MainPageContextMenuStock}" Click="ContextMenuMenuItemStock_Click" />
       </ToolKit:ContextMenu>
      </ToolKit:ContextMenuService.ContextMenu>
     </ListBox>
    </Grid>
    Ensuite le code du Click est:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    private void ContextMenuMenuItemStock_Click(object sender,System.Windows.RoutedEventArgs e)
    {
      GalaSoft.MvvmLight.Messaging.Messenger.Default.Send<string>("","StockManagmentRequest");
    }
    Ensuite, dans mon ViewModel la commande est:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    private void StockManagmentCommand(string Message)
    {
      ViewModelLocator.CategoriesStockStatic.Inputs=this.SelectedCategory.Inputs;
      GalaSoft.MvvmLight.Messaging.Messenger.Default.Send<string>("/Views/CategoriesStockPage","NavigationRequest");
    }
    Donc rien de bien méchant !

    Le problème est que la première fois que j'exécute la commande tout se passe bien. Je ferme la page CategoriesStockPage et je reviens sur ma Listbox et recommence la commande et là ça plante sur:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    // Code to execute on Unhandled Exceptions
    private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
    {
      if (System.Diagnostics.Debugger.IsAttached)
     {
      // An unhandled exception has occurred; break into the debugger
      System.Diagnostics.Debugger.Break();
     }
    }
    Et le message d'erreur dont je ne comprends rien me dit ça:

    (e.exception).Message="SelectedIndex"
    La StackTrace est:

    at Microsoft.Phone.Controls.Pivot.UpdateSelectedIndex(Int32 oldIndex, Int32 newIndex)
    at Microsoft.Phone.Controls.Pivot.OnSelectedIndexPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    at System.Windows.DependencyObject.RaisePropertyChangeNotifications(DependencyProperty dp, Object oldValue, Object newValue)
    at System.Windows.DependencyObject.UpdateEffectiveValue(DependencyProperty property, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, ValueOperation operation)
    at System.Windows.DependencyObject.SetValueInternal(DependencyProperty dp, Object value, Boolean allowReadOnlySet)
    at System.Windows.DependencyObject.SetValueInternal(DependencyProperty dp, Object value)
    at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
    at Microsoft.Phone.Controls.Pivot.set_SelectedIndex(Int32 value)
    at Microsoft.Phone.Controls.Pivot.SetSelectedIndexInternal(Int32 newIndex)
    at Microsoft.Phone.Controls.Pivot.UpdateSelectedItem(Object oldValue, Object newValue)
    at Microsoft.Phone.Controls.Pivot.OnSelectedItemPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    at System.Windows.DependencyObject.RaisePropertyChangeNotifications(DependencyProperty dp, Object oldValue, Object newValue)
    at System.Windows.DependencyObject.UpdateEffectiveValue(DependencyProperty property, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, ValueOperation operation)
    at System.Windows.DependencyObject.RefreshExpression(DependencyProperty dp)
    at System.Windows.Data.BindingExpression.RefreshExpression()
    at System.Windows.Data.BindingExpression.SendDataToTarget()
    at System.Windows.Data.BindingExpression.SourceAcquired()
    at System.Windows.Data.BindingExpression.System.Windows.IDataContextChangedListener.OnDataContextChanged(Object sender, DataContextChangedEventArgs e)
    at System.Windows.Data.BindingExpression.DataContextChanged(Object sender, DataContextChangedEventArgs e)
    at System.Windows.FrameworkElement.OnDataContextChanged(DataContextChangedEventArgs e)
    at System.Windows.FrameworkElement.OnAncestorDataContextChanged(DataContextChangedEventArgs e)
    at System.Windows.FrameworkElement.NotifyDataContextChanged(DataContextChangedEventArgs e)
    at System.Windows.FrameworkElement.OnAncestorDataContextChanged(DataContextChangedEventArgs e)
    at System.Windows.FrameworkElement.NotifyDataContextChanged(DataContextChangedEventArgs e)
    at System.Windows.FrameworkElement.OnTreeParentUpdated(DependencyObject newParent, Boolean bIsNewParentAlive)
    at System.Windows.DependencyObject.UpdateTreeParent(IManagedPeer oldParent, IManagedPeer newParent, Boolean bIsNewParentAlive, Boolean keepReferenceToParent)
    at MS.Internal.FrameworkCallbacks.ManagedPeerTreeUpdate(IntPtr oldParentElement, IntPtr parentElement, IntPtr childElement, Byte bIsParentAlive, Byte bKeepReferenceToParent)
    at MS.Internal.XcpImports.MeasureOverrideNative(IntPtr element, Single inWidth, Single inHeight, Single& outWidth, Single& outHeight)
    at MS.Internal.XcpImports.FrameworkElement_MeasureOverride(FrameworkElement element, Size availableSize)
    at System.Windows.FrameworkElement.MeasureOverride(Size availableSize)
    at Microsoft.Phone.Controls.PhoneApplicationFrame.MeasureOverride(Size availableSize)
    at System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget, Double inWidth, Double inHeight, Double& outWidth, Double& outHeight)
    Là ça fait une semaine et demie que je planche sur le truc et je ne vois pas pourquoi ça fonctionne une fois et la deuxième non !!!

    Merci pour votre aide.
    La Théorie c'est quand on comprends tout mais que rien ne fonctionne.
    La Pratique c'est quand tout fonctionne mais qu'on ne sait pas pourquoi !

    Si vous aimez ma réponse, cliquez sur la main verte Merci

  2. #2
    Membre habitué Avatar de danuz
    Homme Profil pro
    Expert .NET (WPF, Silverlight, Windows Phone) chez Soat
    Inscrit en
    Avril 2003
    Messages
    102
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : Expert .NET (WPF, Silverlight, Windows Phone) chez Soat

    Informations forums :
    Inscription : Avril 2003
    Messages : 102
    Points : 153
    Points
    153
    Par défaut
    Tu peux remplacer ton évènement click par une commande puisque tu utilises MVVM, comme ça, tout sera implémenté dans ton ViewModel.

    Ensuite, il ne manque pas .xaml pour /Views/CategoriesStockPage ?

    Comment tu bindes SelectedCategory ?
    Ma réponse vous a aidé ? Pensez à voter pour elle. N'oubliez pas non plus de changer le statut de votre thread en Résolu.

    Mon blog
    Ma société: So@t

  3. #3
    Membre expérimenté Avatar de DotNET74
    Homme Profil pro
    Watch R&D Engineer & Apprenti .NET
    Inscrit en
    Août 2003
    Messages
    1 986
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 51
    Localisation : France

    Informations professionnelles :
    Activité : Watch R&D Engineer & Apprenti .NET

    Informations forums :
    Inscription : Août 2003
    Messages : 1 986
    Points : 1 453
    Points
    1 453
    Par défaut
    Hello,

    le Click c'est celui du ContextMenu du ToolKit et je ne vois pas comment l'implanter dans le ViewModel. C'est pourquoi je passe par la Messagerie.

    Ensuite, il ne manque pas .xaml car ça fonctionne la première fois et pas la deuxième !!!!

    C'est la le truc de dingue ........

    Merci
    La Théorie c'est quand on comprends tout mais que rien ne fonctionne.
    La Pratique c'est quand tout fonctionne mais qu'on ne sait pas pourquoi !

    Si vous aimez ma réponse, cliquez sur la main verte Merci

  4. #4
    Membre habitué Avatar de danuz
    Homme Profil pro
    Expert .NET (WPF, Silverlight, Windows Phone) chez Soat
    Inscrit en
    Avril 2003
    Messages
    102
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : Expert .NET (WPF, Silverlight, Windows Phone) chez Soat

    Informations forums :
    Inscription : Avril 2003
    Messages : 102
    Points : 153
    Points
    153
    Par défaut Correction code + explication
    D'accord!

    Alors, pour implémenter le click avec une command :
    Il te faut tout d'abord rajouter les espaces de nom liés à l'interactivité et aux commandes :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
    xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WP7"

    Ensuite, tu étends ton MenuContextItem

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    <ToolKit:MenuItem x:Name="ContextMenuMenuItemStock" Header="{Binding Path=MainPageContextMenuStock}">
    <i:Interaction.Triggers>
                                    <i:EventTrigger EventName="Click">
                                        <cmd:EventToCommand Command="{Binding ContextMenuItemCommand}" />
                                    </i:EventTrigger>
                                </i:Interaction.Triggers>
    </ToolKit:MenuItem>
    Il te restera plus qu'à définir ta commande ContextMenuItemCommand dans ton ViewModel.
    Tu peux même passer des paramètres à ta commande aussi si tu le souhaites via la propriété CommandParameter de EventToCommand.

    Pour les autres problèmes, je réfléchis
    Ma réponse vous a aidé ? Pensez à voter pour elle. N'oubliez pas non plus de changer le statut de votre thread en Résolu.

    Mon blog
    Ma société: So@t

  5. #5
    Membre expérimenté Avatar de DotNET74
    Homme Profil pro
    Watch R&D Engineer & Apprenti .NET
    Inscrit en
    Août 2003
    Messages
    1 986
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 51
    Localisation : France

    Informations professionnelles :
    Activité : Watch R&D Engineer & Apprenti .NET

    Informations forums :
    Inscription : Août 2003
    Messages : 1 986
    Points : 1 453
    Points
    1 453
    Par défaut
    Ok,

    je regarde ça ce soir pour le transfert de la commande....

    Merci pour ton aide matinale
    La Théorie c'est quand on comprends tout mais que rien ne fonctionne.
    La Pratique c'est quand tout fonctionne mais qu'on ne sait pas pourquoi !

    Si vous aimez ma réponse, cliquez sur la main verte Merci

  6. #6
    Membre expérimenté Avatar de DotNET74
    Homme Profil pro
    Watch R&D Engineer & Apprenti .NET
    Inscrit en
    Août 2003
    Messages
    1 986
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 51
    Localisation : France

    Informations professionnelles :
    Activité : Watch R&D Engineer & Apprenti .NET

    Informations forums :
    Inscription : Août 2003
    Messages : 1 986
    Points : 1 453
    Points
    1 453
    Par défaut
    En fait,

    je vois pas en quoi ça va changer les choses d'implémenter la commande comme tu l'as indiqué.

    Utiliser la Messagerie c'est bon aussi non ?

    Quelle serait la différence ?
    La Théorie c'est quand on comprends tout mais que rien ne fonctionne.
    La Pratique c'est quand tout fonctionne mais qu'on ne sait pas pourquoi !

    Si vous aimez ma réponse, cliquez sur la main verte Merci

  7. #7
    Membre expérimenté Avatar de DotNET74
    Homme Profil pro
    Watch R&D Engineer & Apprenti .NET
    Inscrit en
    Août 2003
    Messages
    1 986
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 51
    Localisation : France

    Informations professionnelles :
    Activité : Watch R&D Engineer & Apprenti .NET

    Informations forums :
    Inscription : Août 2003
    Messages : 1 986
    Points : 1 453
    Points
    1 453
    Par défaut
    Alors,

    j'ai implanter la commande comme tu me l'as indiqué et ça ne change rien

    malheureusement.........
    La Théorie c'est quand on comprends tout mais que rien ne fonctionne.
    La Pratique c'est quand tout fonctionne mais qu'on ne sait pas pourquoi !

    Si vous aimez ma réponse, cliquez sur la main verte Merci

  8. #8
    Membre habitué Avatar de danuz
    Homme Profil pro
    Expert .NET (WPF, Silverlight, Windows Phone) chez Soat
    Inscrit en
    Avril 2003
    Messages
    102
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : Expert .NET (WPF, Silverlight, Windows Phone) chez Soat

    Informations forums :
    Inscription : Avril 2003
    Messages : 102
    Points : 153
    Points
    153
    Par défaut
    Lol, c'était une "bonne" pratique pour rester dans MVVM comme tu avais déjà commencé, mais, ce n'était pas encore une solution !

    Ta commande StockManagmentCommand prend un paramètre, mais, n'en utilise pas, c'est volontaire ?

    Mmm, sinon, si le message, c'est SelectedIndex, peut être que quand tu reviens sur ta page, l'Item sélectionné n'est pas correct (ton SelectedCategory en fait).
    Ma réponse vous a aidé ? Pensez à voter pour elle. N'oubliez pas non plus de changer le statut de votre thread en Résolu.

    Mon blog
    Ma société: So@t

  9. #9
    Membre expérimenté Avatar de DotNET74
    Homme Profil pro
    Watch R&D Engineer & Apprenti .NET
    Inscrit en
    Août 2003
    Messages
    1 986
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 51
    Localisation : France

    Informations professionnelles :
    Activité : Watch R&D Engineer & Apprenti .NET

    Informations forums :
    Inscription : Août 2003
    Messages : 1 986
    Points : 1 453
    Points
    1 453
    Par défaut
    re,

    en fait quand je reviens sur ma page, l'élément est bien toujours sélectionné c'est là que je comprends plus !!!

    ça fonctionne la première fois et après c'est fini..........

    en tout cas merci pour ton aide.

    Je l'aurais..........
    La Théorie c'est quand on comprends tout mais que rien ne fonctionne.
    La Pratique c'est quand tout fonctionne mais qu'on ne sait pas pourquoi !

    Si vous aimez ma réponse, cliquez sur la main verte Merci

  10. #10
    Membre expérimenté Avatar de DotNET74
    Homme Profil pro
    Watch R&D Engineer & Apprenti .NET
    Inscrit en
    Août 2003
    Messages
    1 986
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 51
    Localisation : France

    Informations professionnelles :
    Activité : Watch R&D Engineer & Apprenti .NET

    Informations forums :
    Inscription : Août 2003
    Messages : 1 986
    Points : 1 453
    Points
    1 453
    Par défaut
    Hello,

    j'ai trouvé d'où ça vient !

    c'est le contrôle Pivot qui me génère ce problème sur ma Listbox !

    J'ai enlevé le contrôle Pivot et plus de problème !


    Cela dit je n'arrive pas à expliquer le pourquoi du comment.

    Merci pour votre aide en tout cas.........
    La Théorie c'est quand on comprends tout mais que rien ne fonctionne.
    La Pratique c'est quand tout fonctionne mais qu'on ne sait pas pourquoi !

    Si vous aimez ma réponse, cliquez sur la main verte Merci

  11. #11
    Membre habitué Avatar de danuz
    Homme Profil pro
    Expert .NET (WPF, Silverlight, Windows Phone) chez Soat
    Inscrit en
    Avril 2003
    Messages
    102
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : Expert .NET (WPF, Silverlight, Windows Phone) chez Soat

    Informations forums :
    Inscription : Avril 2003
    Messages : 102
    Points : 153
    Points
    153
    Par défaut
    Vraiment étrange en tout cas, mais ravit que tu aies pu trouver une solution.

    Ton Pivot n'était pas bindé à quoique ce soit ?
    Ma réponse vous a aidé ? Pensez à voter pour elle. N'oubliez pas non plus de changer le statut de votre thread en Résolu.

    Mon blog
    Ma société: So@t

  12. #12
    Membre expérimenté Avatar de DotNET74
    Homme Profil pro
    Watch R&D Engineer & Apprenti .NET
    Inscrit en
    Août 2003
    Messages
    1 986
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 51
    Localisation : France

    Informations professionnelles :
    Activité : Watch R&D Engineer & Apprenti .NET

    Informations forums :
    Inscription : Août 2003
    Messages : 1 986
    Points : 1 453
    Points
    1 453
    Par défaut
    J'ai bindé la SelectedItem du Pivot pour pouvoir réupérer le pivot sélectionné.

    Mais j'ai ça aussi dans une autre Page et ça fonctionne.....

    Les mystères de l'informatique
    La Théorie c'est quand on comprends tout mais que rien ne fonctionne.
    La Pratique c'est quand tout fonctionne mais qu'on ne sait pas pourquoi !

    Si vous aimez ma réponse, cliquez sur la main verte Merci

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Erreur de conversion bizarre dans une requête
    Par SoaB dans le forum Développement
    Réponses: 7
    Dernier message: 03/05/2010, 10h56
  2. Réponses: 3
    Dernier message: 05/11/2009, 12h09
  3. erreur bizarre dans une page asp
    Par leclone dans le forum ASP
    Réponses: 1
    Dernier message: 19/05/2006, 01h28
  4. [debutant][servlet]erreur causée par une listBox
    Par omega dans le forum Servlets/JSP
    Réponses: 2
    Dernier message: 05/03/2004, 10h53
  5. Icône a coté du texte dans une ListBox
    Par joce3000 dans le forum C++Builder
    Réponses: 6
    Dernier message: 05/12/2003, 02h25

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo