C# WPF Binding INotifyPropertyChanged
Bonjour,
Je développe une application en C# qui utilise WPF pour obtenir les informations envoyées par une Wii Balance Board en utilisant une bibli spécifique biensûr (WiiMoteLib)
La classe est faite, les variables définies, la connexion se fait bien, le graphique est complet...
Les tests consoles sont également concluants.
Mon problème c'est que je suis passé à l'étape de Binding des variables de ma classe pour une de mes pages XML (TextBlock) avec un INotify.. pour avoir mes variables en temps réel (Update quoi).
Voici mon code complet des éléments où je taff en ce moment :
Il y a quelques modifications non concluantes...
Code:
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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
| using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WiimoteLib; //Ma bibliothèque pour la WiiBalanceBoard
using System.Globalization;
using System.ComponentModel;
using System.Windows;
using System.Windows.Threading;
namespace WiiKinéApp
{
public class WiiBalance
{
private Wiimote bb = new Wiimote();
public WiiBalance()
{
//Connecte la WiiBalanceBoard :
bb.Connect();
//Fixe la led frontale de la WiiBlanceBoard :
bb.SetLEDs(1);
}
private InfoPoids monInfosPoids;
public void Update()
{
//Appels :
WiimoteState s = bb.WiimoteState;
monInfosPoids = new InfoPoids();
// Définition des variables:
//Capteur haut gauche, valeur en newton(kg) :
string cpHgauche = s.BalanceBoardState.SensorValuesKg.TopLeft.ToString();
//Capteur haut droit, valeur en newton(kg) :
string cpHdroit = s.BalanceBoardState.SensorValuesKg.TopRight.ToString();
//Capteur bas gauche, valeur en newton(kg) :
string cpBgauche = s.BalanceBoardState.SensorValuesKg.BottomLeft.ToString();
//Capteur bas droit, valeur en newton(kg) :
string cpBdroit = s.BalanceBoardState.SensorValuesKg.BottomRight.ToString();
//Là je fais mes convertions pour mes calculs par la suite:
float PHgauche = float.Parse(cpHgauche, CultureInfo.InvariantCulture.NumberFormat);
float PHdroit = float.Parse(cpHdroit, CultureInfo.InvariantCulture.NumberFormat);
float PBgauche = float.Parse(cpBgauche, CultureInfo.InvariantCulture.NumberFormat);
float PBdroit = float.Parse(cpBdroit, CultureInfo.InvariantCulture.NumberFormat);
//Ici je fais les calculs pour faire mes pourcentages:
float centPcent = PHgauche + PHdroit + PBgauche + PBdroit;
monInfosPoids.CPgauchePCENT = (PHgauche / centPcent) * 100;
monInfosPoids.CPHdroitPCENT = (PHdroit / centPcent) * 100;
monInfosPoids.CPBgauchePCENT = (PBgauche / centPcent) * 100;
monInfosPoids.CPBdroitPCENT = (PBdroit / centPcent) * 100;
//Poids total en Kg :
monInfosPoids.PoidsTTL = s.BalanceBoardState.WeightKg.ToString();
//Pour la batterie :
monInfosPoids.Battery = s.Battery;
}
}
//Ce que j'utiliserai vraiment au final:
public class InfoPoids : INotifyPropertyChanged
{
public void Maj()
{
DispatcherTimer timer = new System.Windows.Threading.DispatcherTimer();
timer.Tick += timerTicked;
timer.Interval = new TimeSpan(0, 0, 1);
timer.Start();
}
private void timerTicked(object sender, EventArgs e)
{
//Si tu veux notifier le changement de toutes les propriétés :
OnPropertyChanged(String.Empty);
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private float cpgauchePCENT;
public float CPgauchePCENT
{
get
{
return cpgauchePCENT;
}
set
{
cpgauchePCENT = value;
this.OnPropertyChanged("CPgauchePCENT");
}
}
private float cpHdroitPCENT;
public float CPHdroitPCENT
{
get
{
return cpHdroitPCENT;
}
set
{
cpHdroitPCENT = value;
this.OnPropertyChanged("CPHdroitPCENT");
}
}
private float cpBgauchePCENT;
public float CPBgauchePCENT
{
get
{
return cpBgauchePCENT;
}
set
{
cpBgauchePCENT = value;
this.OnPropertyChanged("CPBgauchePCENT");
}
}
private float cpBdroitPCENT;
public float CPBdroitPCENT
{
get
{
return cpBdroitPCENT;
}
set
{
cpBdroitPCENT = value;
this.OnPropertyChanged("CPBdroitPCENT");
}
}
private string poidsTTL;
public string PoidsTTL
{
get
{
return poidsTTL;
}
set
{
poidsTTL = value;
this.OnPropertyChanged("PoidsTTL");
}
}
private float battery;
public float Battery
{
get
{
return battery;
}
set
{
battery = value;
this.OnPropertyChanged("Battery");
}
}
}
} |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| <UserControl
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"
mc:Ignorable="d"
xmlns:local ="clr-namespace:WiiKinéApp"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
x:Class="WiiKinéApp.LoadGame"
Loaded="Window_Loaded"
d:DesignWidth="640" d:DesignHeight="480" Width="800" Height="600">
<Grid>
<TextBlock Height="40" HorizontalAlignment="Left" Margin="0,119,0,0" VerticalAlignment="Top" Width="800" Text="B A L A N C E" TextWrapping="Wrap" FontSize="21.333" TextAlignment="Center" />
<Image Height="298" HorizontalAlignment="Left" Margin="116,175,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="561" Source="/WiiKinéApp;component/Images/WiiBb.png" />
<TextBlock HorizontalAlignment="Left" Margin="276,492,0,0" Name="textBlock1" Text="Vous pesez : " VerticalAlignment="Top" />
<TextBlock HorizontalAlignment="Left" Margin="480,492,0,0" Name="textBlock2" Text="Kg" VerticalAlignment="Top" />
<TextBlock Text="{Binding Path=PoidsTTL, UpdateSourceTrigger=PropertyChanged}" Margin="400,480,350,82" FontSize="20"/>
<local:MenuModel Height="600" HorizontalAlignment="Left" x:Name="menuModel1" VerticalAlignment="Top" Width="800" />
</Grid>
</UserControl> |
Code:
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
| using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Globalization;
using System.Windows.Data;
namespace WiiKinéApp
{
public partial class LoadGame : UserControl, ISwitchable
{
private InfoPoids myInfo;
public LoadGame()
{
myInfo= new InfoPoids();
this.DataContext = myInfo;
// Required to initialize variables
InitializeComponent();
}
private void Window_Loaded(object sender, System.Windows.RoutedEventArgs e)
{
myInfo.Maj();
}
#region ISwitchable Members
public void UtilizeState(object state)
{
throw new NotImplementedException();
}
#endregion
}
} |
Je pense que j'ai mis ce qu'il fallait.
Le problème c'est que mon Binding ne fonctionne pas, la valeur de ma variable ne s'affiche pas et n'est pas mise à jour...
J'ai essayé énormément de choses déjà, j'ai passé environ 15h sur ce problème que je n'arrive vraiment pas à résoudre malgré tout ce que j'ai déjà lu.
J'ai besoin de vos lumière, merci d'avance.