我正在使用 C# WPF MVVM 制作 Bing Map 程序。
每当 Bing Map 中的地图视图发生变化时,我都想将 BoundingRectangle 绑定到 ViewModel 并保存它。
但 BoundingRectangle 只是一个二传手。
所以,我试图连接一个事件而不是一般的命令绑定并将其作为命令参数发送。
我在下面写
Xaml
<Grid Grid.Row="0" Grid.Column="1">
<bm:Map x:Name="bm"
Margin="0,10,10,10"
CredentialsProvider="..."
Mode="AerialWithLabels"
BorderBrush="{StaticResource DefalutBorderBrush}"
BorderThickness="1"
Center="{Binding CenterLocation, Mode=TwoWay,Converter={StaticResource Converter}}"
ZoomLevel="{Binding CurrentZoomLevel, Mode=TwoWay}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="ViewChangeOnFrame">
<i:InvokeCommandAction Command="{Binding ViewChangedCommand, Mode=OneWay, Converter={StaticResource TestConverter}}"
CommandParameter="{Binding BoundingRectangle, Mode=OneWay, ElementName=bm, Converter={StaticResource ConverterRect}}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</bm:Map>
查看模型
public class TestViewModel
{
private double[] _boundingBox;
public double[] BingMapBoundingBox
{
get
{
return _boundingBox;
}
set
{
SetProperty(ref _boundingBox, value);
}
}
public ICommand ViewChangedCommand
{
get;
set;
}
public TestViewModel()
{
ViewChangedCommand = new RelayCommand<double[]>(ViewChanged);
}
public void ViewChanged(double[] boundingBox)
{
BingMapBoundingBox = boundingBox;
}
}
转换器
public class LocationRectToDoubleArrayConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
double[] resultArry = new double[4] { 0, 0, 0, 0 };
if (value == null)
return resultArry;
LocationRect currLocation = value as LocationRect;
resultArry[0] = currLocation.Northwest.Longitude;
resultArry[1] = currLocation.Northwest.Latitude;
resultArry[2] = currLocation.Southeast.Longitude;
resultArry[3] = currLocation.Southeast.Latitude;
return resultArry;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
LocationRect resultLocation = new LocationRect();
if (value is null)
return resultLocation;
double[] dblArray = value as double[];
resultLocation.Northwest.Longitude = dblArray[0];
resultLocation.Northwest.Latitude = dblArray[1];
resultLocation.Southeast.Longitude = dblArray[2];
resultLocation.Southeast.Latitude = dblArray[3];
return resultLocation;
}
}
每次更改视图时都会执行该事件,但CommandParameter仅在程序第一次执行时执行一次,之后不再执行。
每当视图发生变化时,我都希望 BoundingRectangle 作为 CommandParameter 进来。我应该怎么办?
很抱歉没有解释,因为英语不是我的母语。
谢谢你