你是对的。我们无法调整大小MapIcon https://msdn.microsoft.com/library/windows/apps/windows.ui.xaml.controls.maps.mapicon.aspx直接,因为它不提供此类属性或方法。 MapIcon 的大小主要由图像的大小控制,图像的大小由地图图标.Image https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.maps.mapicon.image.aspx财产。我们可以设置该图像的大小而不使用Xamarin.Forms.Image.
要设置该图像的大小,我们可以利用位图解码器类 https://msdn.microsoft.com/en-us/library/windows/apps/xaml/windows.graphics.imaging.bitmapdecoder.aspx, 位图编码器类 https://msdn.microsoft.com/en-us/library/windows/apps/xaml/windows.graphics.imaging.bitmapencoder.aspx and 位图变换类 https://msdn.microsoft.com/en-us/library/windows/apps/windows.graphics.imaging.bitmaptransform.aspx像下面这样:
private async System.Threading.Tasks.Task<RandomAccessStreamReference> ResizeImage(StorageFile imageFile, uint scaledWidth, uint scaledHeight)
{
using (IRandomAccessStream fileStream = await imageFile.OpenAsync(FileAccessMode.Read))
{
var decoder = await BitmapDecoder.CreateAsync(fileStream);
//create a RandomAccessStream as output stream
var memStream = new InMemoryRandomAccessStream();
//creates a new BitmapEncoder and initializes it using data from an existing BitmapDecoder
BitmapEncoder encoder = await BitmapEncoder.CreateForTranscodingAsync(memStream, decoder);
//resize the image
encoder.BitmapTransform.ScaledWidth = scaledWidth;
encoder.BitmapTransform.ScaledHeight = scaledHeight;
//commits and flushes all of the image data
await encoder.FlushAsync();
//return the output stream as RandomAccessStreamReference
return RandomAccessStreamReference.CreateFromStream(memStream);
}
}
然后我们可以使用这个方法先创建一个调整大小的图像流引用,然后将其设置为MapIcon
's Image
like:
var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/Pin/customicon.png"));
var imageReference = await ResizeImage(file, 64, 64);
nativeMap.MapElements.Add(new MapIcon()
{
Title = pin.Name,
Image = imageReference,
Location = new Geopoint(new BasicGeoposition() { Latitude = pin.Position.Latitude, Longitude = pin.Position.Longitude }),
NormalizedAnchorPoint = new Windows.Foundation.Point(0.5, 1.0)
});