2016-09-21 41 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