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

Silverlight Discussion :

Petite question de débutant Instance d'Objet


Sujet :

Silverlight

  1. #1
    Membre averti
    Profil pro
    Inscrit en
    Juillet 2009
    Messages
    55
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2009
    Messages : 55
    Par défaut Petite question de débutant Instance d'Objet
    Slt,

    J'ai un petit soucis d'instance avec un objet.
    Voila dans mon fichier xaml j'ai ca:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    <StackPanel Name="MyStackPanel" Orientation="Vertical" Margin="20,15,20,15">
                                <StackPanel Orientation="Horizontal" Margin="0,0,0,6">
                                    <TextBlock Name="textX" Foreground="#FFFFFFFF" FontSize="12" FontStyle="Italic" FontFamily="Portable User Interface" />
                                </StackPanel>
    </StackPanel>
    dans mon fichier c#
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    textX = new TextBlock();
    this.textX.Text = "Toto";
    Le problème est le suivant : je n'ai pas de valeur pour mon textBock = Toto.
    Si je n'instancie pas mon textX, je n'ai pas d'erreur Visual Studio par contre j'ai une erreur
    Object reference not set to an instance of an object.
    Comment faire ?
    Merci d'avance

  2. #2
    Membre Expert
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Novembre 2009
    Messages
    2 056
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Novembre 2009
    Messages : 2 056
    Par défaut
    Si tu mets un control dans le xaml, tu n'as pas besoin de l'instancier.
    Enleves
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    textX = new TextBlock();
    et normalement ca devrait être bon.

    edit: j'ai lut trop vite...
    Peut être cela vient de ton font family? Enleve cette propriété pour voir.
    Ah et puis surtout mets un X:Name au lieu de Name tout court.

  3. #3
    Membre averti
    Profil pro
    Inscrit en
    Juillet 2009
    Messages
    55
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2009
    Messages : 55
    Par défaut
    J'ai fait ce que t'as dit mais ca ne marche toujours pas.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    <TextBlock x:Name="textX" Foreground="#FFFFFFFF"/>
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    textX = new TextBlock();
    textX.Text = "okokokok";
    Une idée !!

  4. #4
    Membre éprouvé
    Profil pro
    Inscrit en
    Septembre 2008
    Messages
    121
    Détails du profil
    Informations personnelles :
    Âge : 38
    Localisation : Belgique

    Informations forums :
    Inscription : Septembre 2008
    Messages : 121
    Par défaut
    Il ne faut pas faire sa ! Tu remplaces ta textblock par une nouvelle instance vide

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    textX = new TextBlock();

  5. #5
    Membre averti
    Profil pro
    Inscrit en
    Juillet 2009
    Messages
    55
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2009
    Messages : 55
    Par défaut
    D'accord avec toi mais si je ne mets pas une nouvelle instance, il me catch une erreur :
    Object reference not set to an instance of an object.

  6. #6
    Membre Expert
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Novembre 2009
    Messages
    2 056
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Novembre 2009
    Messages : 2 056
    Par défaut
    Et si tu essayes de mettre le texte directement dans le xaml, et que tu enleves tout code behind?
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    <StackPanel Name="MyStackPanel" Orientation="Vertical" Margin="20,15,20,15">
                                <StackPanel Orientation="Horizontal" Margin="0,0,0,6">
                                    <TextBlock Name="textX" Foreground="#FFFFFFFF" FontSize="12" FontStyle="Italic" FontFamily="Portable User Interface" >Test du textblock</TextBlock>
                                </StackPanel>
    </StackPanel>
    sinon tu as deja reussis a faire quelquechose en silverlight?Peut être que ca vient de l'installation
    http://www.developpez.net/forums/d57...-of-an-object/

  7. #7
    Membre Expert
    Avatar de Pragmateek
    Homme Profil pro
    Formateur expert .Net/C#
    Inscrit en
    Mars 2006
    Messages
    2 635
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Formateur expert .Net/C#
    Secteur : Conseil

    Informations forums :
    Inscription : Mars 2006
    Messages : 2 635
    Par défaut
    Sans doute un problème d'initialisation de ton instance.

    1) Est-ce que ton constructeur fait bien appel à InitializeComponent() ?
    Si non ajoute un appel à InitializeComponent() au tout début de ton constructeur.
    2) Si oui : est-ce que ton code ne serait pas par hasard placé avant l'appel à InitializeComponent() et pas après ?
    InitializeComponent() devrait être la première instruction du constructeur.

  8. #8
    Expert confirmé
    Avatar de Skyounet
    Homme Profil pro
    Software Engineer
    Inscrit en
    Mars 2005
    Messages
    6 380
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : Etats-Unis

    Informations professionnelles :
    Activité : Software Engineer
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2005
    Messages : 6 380
    Par défaut
    Tous les codes utilisant les contrôles doivent être au minimum placé dans le Loaded du UserControl. Dans la constructeur, même après le InitializeComponent, les contrôles ne sont pas obligatoirement chargé (je parle des contrôles enfants des contrôles de l'UC par exemple).

  9. #9
    Membre averti
    Profil pro
    Inscrit en
    Juillet 2009
    Messages
    55
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2009
    Messages : 55
    Par défaut
    Merci pour vos commentaires mais c'est un peu abstrait .

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
     public MainPage()
            {
                InitializeComponent();
            }
     
            private void button1_Click(object sender, RoutedEventArgs e)
            {
              textX.Text = "okokokok";
             }
    Pour répondre à seriousme, voici le code.

  10. #10
    Membre Expert
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Novembre 2009
    Messages
    2 056
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Novembre 2009
    Messages : 2 056
    Par défaut
    Je vois vraiment pas ce qu'il y a de mal dans ton code,
    tu as regardé dans le lien sur l'installation?( http://www.developpez.net/forums/d57...-of-an-object/ )

  11. #11
    Membre averti
    Profil pro
    Inscrit en
    Juillet 2009
    Messages
    55
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2009
    Messages : 55
    Par défaut
    Je suis Visual Studio 2010 Premium donc je pense que l'article ne me concerne pas ...

  12. #12
    Membre Expert
    Avatar de Samuel Blanchard
    Homme Profil pro
    Expert .NET
    Inscrit en
    Février 2010
    Messages
    1 504
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 51
    Localisation : France

    Informations professionnelles :
    Activité : Expert .NET

    Informations forums :
    Inscription : Février 2010
    Messages : 1 504
    Par défaut
    Bonjour,

    Poste ton projet (si il est pas trop gros), on jettera un coup d'oeil.

  13. #13
    Membre averti
    Profil pro
    Inscrit en
    Juillet 2009
    Messages
    55
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2009
    Messages : 55
    Par défaut ESRI api
    OK merci.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
     
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using ESRI.ArcGIS.Client;
    using ESRI.ArcGIS.Client.Geometry;
     
    namespace Test_try
    {
        public partial class MainPage : UserControl
        {
            public MainPage()
            {
                InitializeComponent();
                theBlock.Text = "Toto";
                theBlock2.Text = "titi";
                AddPolygonGraphics();
            }
            private void AddPolygonGraphics()
            {
                string coordinateString1 = "130,5.59 118.42,3.92 117.3,23.3 143.2,22.9 130,5.59";
     
                GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
     
                PointCollectionConverter pointConverter = new PointCollectionConverter();
                ESRI.ArcGIS.Client.Geometry.PointCollection pointCollection1 =
                    pointConverter.ConvertFromString(coordinateString1) as ESRI.ArcGIS.Client.Geometry.PointCollection;
     
                ESRI.ArcGIS.Client.Geometry.Polygon polygon1 = new ESRI.ArcGIS.Client.Geometry.Polygon();
                polygon1.Rings.Add(pointCollection1);
     
                Graphic graphic = new Graphic()
                {
                    Geometry = polygon1,
                    Symbol = DefaultFillSymbol
                };
     
                graphicsLayer.Graphics.Add(graphic);
            }
     
        }
    }
    Le block Toto marche correctement mais le block titi ne marche pas :
    Object reference not set to an instance of an object.
    Fichier xaml

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    <UserControl x:Class="Test_try.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                     xmlns:esri="clr-namespace:ESRI.ArcGIS.Client;assembly=ESRI.ArcGIS.Client"
         xmlns:esriSymbols="clr-namespace:ESRI.ArcGIS.Client.Symbols;assembly=ESRI.ArcGIS.Client"
                 mc:Ignorable="d"
        d:DesignHeight="300" d:DesignWidth="400">
     
        <Grid x:Name="LayoutRoot" Background="White">
            <Grid.Resources>
            <esriSymbols:SimpleFillSymbol x:Name="DefaultFillSymbol" Fill="Green" BorderBrush="Blue" 
                    BorderThickness="3" />
            </Grid.Resources>
            <esri:Map x:Name="MyMap">
                <esri:ArcGISTiledMapServiceLayer ID="MyLayer" 
                    Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer" />
     
                <esri:GraphicsLayer ID="MyGraphicsLayer">
                    <esri:GraphicsLayer.MapTip>
                        <Border esri:GraphicsLayer.MapTipHideDelay="00:00:01.5" CornerRadius="10" BorderBrush="#FF222957" BorderThickness="3" Margin="0,0,15,15">
                            <Border.Background>
                                <LinearGradientBrush EndPoint="1.038,1.136" StartPoint="0.015,0.188">
                                    <GradientStop Color="#FFD1DFF2"/>
                                    <GradientStop Color="#FF092959" Offset="0.946"/>
                                </LinearGradientBrush>
                            </Border.Background>
                            <Border.Effect>
                                <DropShadowEffect ShadowDepth="10" BlurRadius="14" Direction="300" />
                            </Border.Effect>
                            <StackPanel>
                                <TextBlock x:Name="theBlock2"></TextBlock>
                            </StackPanel>
     
     
                        </Border>
                    </esri:GraphicsLayer.MapTip>
                </esri:GraphicsLayer>
     
     
            </esri:Map>
            <StackPanel>
                <Button Width="100" Height="20"/>
                <TextBlock x:Name="theBlock"></TextBlock>
            </StackPanel>
        </Grid>
    </UserControl>
    Merci pour votre aide

  14. #14
    Membre Expert
    Avatar de Samuel Blanchard
    Homme Profil pro
    Expert .NET
    Inscrit en
    Février 2010
    Messages
    1 504
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 51
    Localisation : France

    Informations professionnelles :
    Activité : Expert .NET

    Informations forums :
    Inscription : Février 2010
    Messages : 1 504
    Par défaut
    Ton control GraphicsLayer est-il un UserControl (je le suspecte) ? Si c'est le cas, c'est un comportement normal puisque les UserControl ne sont pas normalement habilités à contenir des controles nommés. En gros, les FindName ne fonctionnent pas sur les contenus d'un UserControl.

    Il faut donc :
    soit fixer la référence de ton control à la main
    soit utilisé du Binding pour le modifier
    soit transformer ce UserControl GraphicsLayer en TemplatedControl (plus complexe à réaliser).

  15. #15
    Membre averti
    Profil pro
    Inscrit en
    Juillet 2009
    Messages
    55
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2009
    Messages : 55
    Par défaut
    Alors :
    • soit fixer la référence de ton control à la main
    • soit utilisé du Binding pour le modifier
    • soit transformer ce UserControl GraphicsLayer en TemplatedControl (plus complexe à réaliser).


    J'ai fait du binding pour le modifier mais toujours la meme erreur.


    Fixer la référence de mon control ? Comment faire?

  16. #16
    Membre Expert
    Avatar de Samuel Blanchard
    Homme Profil pro
    Expert .NET
    Inscrit en
    Février 2010
    Messages
    1 504
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 51
    Localisation : France

    Informations professionnelles :
    Activité : Expert .NET

    Informations forums :
    Inscription : Février 2010
    Messages : 1 504
    Par défaut
    Etrange, car le Binding devrait fonctionner si tu n'as pas rerouter le DataContext.

    En revanche pour fixer les références :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    <esri:GraphicsLayer x:Name="MonGraphicsLayer" ID="MyGraphicsLayer">
                    <esri:GraphicsLayer.MapTip>
                          ...
                          <TextBlock x:Name="theBlock2"></TextBlock>
                          ...
                    </esri:GraphicsLayer.MapTip>
    </esri:GraphicsLayer>
    Je pense que ça devrait fonctionner (mais pas tester) :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    this.theBlock2 = this.MonGraphicsLayer.MapTip.FindName("theBlock2");

  17. #17
    Membre averti
    Profil pro
    Inscrit en
    Juillet 2009
    Messages
    55
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2009
    Messages : 55
    Par défaut
    Bon j'ai réussi à contourner le problème .

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
     <esri:GraphicsLayer.MapTip>
                        <Grid Background="#99000000">
                            <StackPanel Margin="5">
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Text="x: " Foreground="White" />
                                    <TextBlock Text="{Binding Converter={StaticResource MyDictConverter}, ConverterParameter=ID, Mode=OneWay}" Foreground="White" />
                                </StackPanel>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Text="y: " Foreground="White" />
                                    <TextBlock Text="{Binding Converter={StaticResource MyDictConverter}, ConverterParameter=ID2, Mode=OneWay}" Foreground="White" />
                                </StackPanel>
                            </StackPanel>
                        </Grid>
                    </esri:GraphicsLayer.MapTip>

    Pour le c#
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
                            graphic.Attributes.Add("ID", x.ToString());
                            graphic.Attributes.Add("ID2", y.ToString());
                            graphic.Attributes.Add("Size", 20);
                            graphic.Attributes.Add("Fill", GetRandomColor());
    et ca marche bien !!
    Merci pour votre aide

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

Discussions similaires

  1. SessionBehavior Petite question de débutant
    Par ZOlivier dans le forum W4 Express
    Réponses: 2
    Dernier message: 28/08/2007, 13h47
  2. Petite question de débutant
    Par will2taz dans le forum VB.NET
    Réponses: 10
    Dernier message: 10/07/2007, 20h49
  3. Petite question de débutant
    Par Arry_C dans le forum Access
    Réponses: 2
    Dernier message: 03/03/2007, 21h02
  4. Petites questions de débutant
    Par mickael.be dans le forum Débuter
    Réponses: 15
    Dernier message: 28/12/2005, 17h06
  5. question de débutant sur les objets
    Par boucher_emilie dans le forum ASP
    Réponses: 3
    Dernier message: 06/08/2004, 10h51

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