我目前正在进行不同的练习,以便了解UserControls的绑定。
我有以下UserControl:
<UserControl x:Class="Overvaagning.View.AccGyrValues"
xmlns:vm="clr-namespace:Overvaagning.Model"
mc:Ignorable="d" Width="404.478" Height="467.164">
<UserControl.DataContext>
<vm:Accelerometer/>
</UserControl.DataContext>
<Grid Margin="10,0,0,0">
<Label x:Name="labelValX" Content="{Binding Path=xValue}" HorizontalAlignment="Left" Margin="282,51,0,0" VerticalAlignment="Top" FontSize="30" RenderTransformOrigin="0.519,0.24" Width="88" Height="44"/>
</Grid>
ViewModel:
public class Accelerometer : Values, INotifyPropertyChanged, Overvaagning.Model.Values.ISensorOperations
{
public SensorAccelerometer BTAccSensor { get; internal set; }
// Property changed event handleren initialiseres
public event PropertyChangedEventHandler PropertyChanged;
// Her initiliserer jeg Universalt Entydigt Identification (UUID) for at få adgang til Accelerometer tjenesten i kittet
static Guid SensorDeviceUuid = Guid.Parse("F000AA10-0451-4000-B000-000000000000");
public Accelerometer()
{
// Definer sensoren til en accelerometer sensor.
BTAccSensor = new SensorAccelerometer();
// Sensoren service UUID defineres til Accelerometer UUID.
BTAccSensor.SensorDeviceUuid = SensorDeviceUuid;
// Denne UUID er en karakteristisk UUID i accelerometer tjenesten der har opgaven til at aktivere accelerometeret
BTAccSensor.DataUuid = Guid.Parse("F000AA11-0451-4000-B000-000000000000");
// Denne UUID bruges til at konfigurere acceleromteret
BTAccSensor.ConfigurationUuid = Guid.Parse("F000AA12-0451-4000-B000-000000000000");
// Denne UUID bruges til at modificere acceleromter følsomheden, alt afhængig af, hvor mange notifikationer
// accelerometer skal sende pr. sekund
BTAccSensor.SpeedUuid = Guid.Parse("F000AA13-0451-4000-B000-000000000000");
// PropertyChanged defineres til den metode der skal kaldes, så snart der modtages data fra accelerometet
BTAccSensor.PropertyChanged += sensor_PropertyChanged;
}
public sbyte XRaw { get; internal set; }
public sbyte YRaw { get; internal set; }
public sbyte ZRaw { get; internal set; }
public byte ValueToSend;
public double X { get; internal set; }
public double Y { get; internal set; }
public double Z { get; internal set; }
public double G { get; internal set; }
public static Task<IList<Values.SensorInitData>> CreateAllAsync(Readings readings, FrameworkElement el)
{
return SensorCreateAll<Accelerometer>.CreateAllAsync(SensorDeviceUuid, readings, el);
}
public Task<int> InitializeAsync(GattDeviceService device, Readings readings, FrameworkElement el, byte ValueToSend)
{
return BTAccSensor.InitializeAsync(device, readings, el, ValueToSend);
}
private string _xval;
public string xValue
{
get { return _xval; }
set
{
if (value != _xval)
{
_xval = value;
OnPropertyChanged("xValue");
}
}
}
public void sensor_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
sbyte value;
switch (e.PropertyName)
{
case "B0Raw":
value = (sbyte)BTAccSensor.B0Raw;
if (value != XRaw)
{
XRaw = value;
X = value / 16.0;
X = Math.Round(X, 2);
xValue = X.ToString();
}
break;
case "B1Raw":
value = (sbyte)BTAccSensor.B1Raw;
if (value != YRaw)
{
YRaw = value;
Y = ((double)(sbyte)value) / 16.0;
Y = Math.Round(Y, 2);
G = Math.Sqrt((X * X) + (Y * Y) + (Z * Z));
G = Math.Round(G, 2);
}
break;
case "B2Raw":
value = (sbyte)BTAccSensor.B2Raw;
if (value != ZRaw)
{
ZRaw = value;
Z = ((double)(sbyte)value) / 16.0;
Z = Math.Round(Z, 2);
}
break;
}
}
// Metoden for propertychanged eventhandler
protected virtual void OnPropertyChanged(string propName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
我实际上是将标签内容绑定到字符串xValue。每次我从外部传感器接收数据时,都会触发sensor_PropertyChanged()
方法,我实际上想要将xValue定义为我从传感器接收的double X
。问题是propertyChangedEventHandler
总是等于null
。为什么会出现这个问题?因为我确信View和VievModel之间的绑定确实有效。