2016-09-21 32 views
2

对于个人需求,对于Xamarin.Forms.Map控制,我需要创建一个CustomPin扩展名。 UWP部分(PCL项目)Xamarin表单 - 图像到/从IRandomAccessStreamReference

我创建了一个MapIcon喜欢它:

nativeMap.MapElements.Add(new MapIcon() 
{ 
    Title = pin.Name, 
    Image = RandomAccessStreamReference.CreateFromUri(new Uri("ms-appx:///Assets/Pin/customicon.png")), 
    Location = new Geopoint(new BasicGeoposition() { Latitude = pin.Position.Latitude, Longitude = pin.Position.Longitude }), 
    NormalizedAnchorPoint = new Windows.Foundation.Point(0.5, 1.0) 
}); 

然而,通过这种方式,我不能设置Image的大小。

然后我想从我的PCL部分使用Image,调整它的大小并将其转换为IRandomAccessStreamReference。为了实现它,我需要我的Image转换成流,但我找不到,使其作品的功能> <

实例所需要的方式:

private IRandomAccessStreamReference ImageToIRandomAccessStreamReference(Image image) 
{ 
    //Here I can set the size of my Image 

    //I convert it into a stream 
    IRandomAccessStreamReference irasr = RandomAccessStreamReference.CreateFromStream(/* img? */); 

    //irasr is then created from img 

    //I return the IRandomAccessStreamReference needed by the MapIcon element 
    return irasr; 
} 

注:Image放慢参数IMGXamarin.Forms.Image

因此,首先,这可能吗?如果是的话,那么谢谢任何可以帮助我的帮助..我已经搜索了如何调整MapIcon的大小,并且它不可能直接从班级[MapIcon]中获得(https://msdn.microsoft.com/library/windows/apps/windows.ui.xaml.controls.maps.mapicon.aspx

感谢您的帮助!

回答

3

你说得对。我们无法直接调整MapIcon,因为它不提供此类属性或方法。 MapIcon的大小主要受MapIcon.Image属性设置的图像大小控制。我们可以在不使用Xamarin.Forms.Image的情况下设置此图片的尺寸。

要设置这个图像的大小,我们可以利用的BitmapDecoder classBitmapEncoder classBitmapTransform class类似以下内容:

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); 
    } 
} 

,然后我们可以用这个方法首先创建一个调整后的图像流的参考和然后将其设置为MapIconImage像:

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) 
}); 
+0

嗨!何神,我正在寻找这么久!我今晚会试一试,谢谢! :) – Emixam23

+0

@ Emixam23嗨,任何更新?如果我的回答没有解决您的问题,请随时通知我。 –

+0

它完美的作品,谢谢:) – Emixam23