Windows 8 WinRT UserControl的参数化构造函数
我正在尝试在WinRT应用程序中为Bing地图创建自定义图钉。我的问题是,我需要从我的页面中引用实际的地图,以便将图标正确地固定在我的userControl中。例如,这是我的DataTemplate,它被绑定到地图上,对普通图钉工作良好。为了使自定义userControl正确定位,我需要在userControl中引用父映射 这是我的XAML:Windows 8 WinRT UserControl的参数化构造函数,windows-8,microsoft-metro,winrt-xaml,.net-4.5,bing-maps,Windows 8,Microsoft Metro,Winrt Xaml,.net 4.5,Bing Maps,我正在尝试在WinRT应用程序中为Bing地图创建自定义图钉。我的问题是,我需要从我的页面中引用实际的地图,以便将图标正确地固定在我的userControl中。例如,这是我的DataTemplate,它被绑定到地图上,对普通图钉工作良好。为了使自定义userControl正确定位,我需要在userControl中引用父映射 这是我的XAML: <m:MapItemsControl x:Name="Pushpinss" ItemsSource="{Binding InventoryItems
<m:MapItemsControl x:Name="Pushpinss" ItemsSource="{Binding InventoryItems}">
<m:MapItemsControl.ItemTemplate>
<DataTemplate>
<!-- NORMAL PUSHPIN WORKS -->
<m:Pushpin>
<m:MapLayer.Position>
<m:Location Latitude="{Binding WarehouseLatitude}"
Longitude="{Binding WarehouseLongitude}" />
</m:MapLayer.Position>
</m:Pushpin>
<!-- CUSTOM CONTROL DISPLAYS BUT DOES NOT POSITION CORRECTLY BECAUSE I NEED A REFERENCE TO THE MAP-->
<View:GPSIcon Latitude="{Binding WarehouseLatitude}"
Longitude="{Binding WarehouseLongitude}"
Radius="100000"/>
<x:Arguments>
</x:Arguments>
</DataTemplate>
</m:MapItemsControl.ItemTemplate>
</m:MapItemsControl>
这是我的自定义控件:
public sealed partial class GPSIcon : UserControl
{
private Map _map;
private const double EARTH_RADIUS_METERS = 6378137;
public GPSIcon(Map map)
{
this.InitializeComponent();
_map = map;
_map.ViewChanged += (s, e) =>
{
UpdateAccuracyCircle();
};
}
public static readonly DependencyProperty LatitudeProperty =
DependencyProperty.Register("Latitude", typeof(double), typeof(GPSIcon), new PropertyMetadata(0));
public static readonly DependencyProperty LongitudeProperty =
DependencyProperty.Register("Longitude", typeof(double), typeof(GPSIcon), new PropertyMetadata(0));
public static readonly DependencyProperty RadiusProperty =
DependencyProperty.Register("Radius", typeof(double), typeof(GPSIcon), new PropertyMetadata(0));
public double Latitude
{
get { return (double)GetValue(LatitudeProperty); }
set { SetValue(LatitudeProperty, value); }
}
public double Longitude
{
get { return (double)GetValue(LongitudeProperty); }
set { SetValue(LongitudeProperty, value); }
}
/// <summary>
/// Radius in Metres
/// </summary>
public double Radius
{
get { return (double)GetValue(RadiusProperty); }
set
{
SetValue(RadiusProperty, value);
UpdateAccuracyCircle();
}
}
private void UpdateAccuracyCircle()
{
if (_map != null && Radius >= 0)
{
double groundResolution = Math.Cos(_map.Center.Latitude * Math.PI / 180) * 2 * Math.PI * EARTH_RADIUS_METERS / (256 * Math.Pow(2, _map.ZoomLevel));
double pixelRadius = Radius / groundResolution;
AccuracyCircle.Width = pixelRadius;
AccuracyCircle.Height = pixelRadius;
AccuracyCircle.Margin = new Thickness(-pixelRadius / 2, -pixelRadius / 2, 0, 0);
}
}
}
公共密封部分类GPSIcon:UserControl
{
私人地图(private Map);;
私人建筑双地球半径米=6378137;
公共GPSIcon(地图)
{
this.InitializeComponent();
_地图=地图;
_map.ViewChanged+=(s,e)=>
{
UpdateAccuracyCircle();
};
}
公共静态只读从属属性LatitudeProperty=
DependencyProperty.Register(“纬度”、typeof(双精度)、typeof(GPSIcon)、新PropertyMetadata(0));
公共静态只读从属属性LongitudeProperty=
DependencyProperty.Register(“经度”、typeof(双精度)、typeof(GPSIcon)、新PropertyMetadata(0));
公共静态只读从属属性RadiusProperty=
DependencyProperty.Register(“半径”、typeof(双精度)、typeof(GPSIcon)、新PropertyMetadata(0));
公共双纬度
{
获取{return(double)GetValue(LatitudeProperty);}
set{SetValue(LatitudeProperty,value);}
}
公共双经度
{
获取{return(double)GetValue(LongitudeProperty);}
set{SetValue(LongitudeProperty,value);}
}
///
///半径(米)
///
公共双半径
{
获取{return(double)GetValue(radiuproperty);}
设置
{
设置值(半径属性,值);
UpdateAccuracyCircle();
}
}
私有void UpdateAccuracyCircle()
{
如果(_map!=null&&Radius>=0)
{
双地面分辨率=Math.Cos(_map.Center.lation*Math.PI/180)*2*Math.PI*地球半径_米/(256*Math.Pow(2,_map.ZoomLevel));
双像素半径=半径/地面分辨率;
精度圆宽度=像素半径;
精度圆高度=像素半径;
精度圆边距=新厚度(-pixelRadius/2,-pixelRadius/2,0,0);
}
}
}
这可能吗?我还尝试使用x:Arguments指令,如下所述:
谢谢更新1 做以下更改 1) 添加空构造函数
public GPSIcon()
{
this.InitializeComponent();
}
2) 声明类型为Map
public Map MyMap
{
get { return (Map)GetValue(MyMapProperty); }
set { SetValue(MyMapProperty, value); }
}
public static readonly DependencyProperty MyMapProperty =
DependencyProperty.Register("MyMap", typeof(Map), typeof(GPSIcon), new PropertyMetadata(default(Map), OnMapSet));
private static void OnMapSet(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
_map = ((GPSIcon)(d)).MyMap;
_map.ViewChanged += (ss, ee) =>
{
((GPSIcon)(d)).UpdateAccuracyCircle();
};
}
3) 在XAML中传递像这样的对象
<m:Map x:Name="objMap">
<m:MapItemsControl x:Name="Pushpinss" ItemsSource="{Binding InventoryItems}">
<m:MapItemsControl.ItemTemplate>
<DataTemplate>
<View:GPSIcon Latitude="{Binding WarehouseLatitude}"
Longitude="{Binding WarehouseLongitude}"
Radius="100000"
MyMap="{Binding ElementName=objMap}"/>
</DataTemplate>
</m:MapItemsControl.ItemTemplate>
</m:MapItemsControl>
</m:Map>
声明另一个类型为
Map
的依赖属性,然后您应该在
简单地说,您需要遵循与如何使自定义UIElement在地图上正确定位相同的逻辑。您可以在代码中不这样做,只需按照与设置图钉位置相同的方式设置UIElement的位置 例如:
<View:GPSIcon Radius="100000">
<m:MapLayer.Position>
<m:Location Latitude="{Binding WarehouseLatitude}"
Longitude="{Binding WarehouseLongitude}" />
</m:MapLayer.Position>
</View:GPSIcon>
谢谢,我已经试过了,但无法绑定。WinRT应该使用什么绑定语法?视觉树非常感谢。我等不及Win8.1发布了。调试是一场噩梦。我不得不重新创建这个应用程序,因为我的自定义控件中没有任何东西可以再使用了,而且由于某些原因仍然不能使用。你的样品有道理。我会让你知道,如果我得到它来建设:)这是伟大的。非常感谢你的评论