2017-10-12 123 views
0

我跟隨了James Montemagno的指南,介紹如何爲我的Xamarin Forms共享項目中的圓形圖像製作自定義渲染器。Xamarin Forms native customrenderer

https://blog.xamarin.com/elegant-circle-images-in-xamarin-forms/

(爲導向的真實副本,感覺多餘的實際代碼本身添加到我的項目,但請評論,如果事實並非如此)

據工作完美無瑕,不過,我需要在應用程序運行時按下按鈕來動態更改圓形邊框的顏色。

但是由於圓形的顏色是在每個渲染器中本地設置的,我不確定我可以如何從共享代碼中更改它。

+0

爲什麼不公開一個屬性來設置顏色? – Jason

+0

優秀的建議,絕對理想。我自己的嘗試是徒勞的,因此是個問題。 –

+1

[ImageCirclePlugin](https://github.com/jamesmontemagno/ImageCirclePlugin)已經有一個'BorderColor',您可以在共享代碼中進行更新。渲染器將​​檢測到更改並相應更新。 – Ada

回答

1

也許這個片段可以幫助:

public class CircleImage : Image 
{ 

    public static readonly BindableProperty CurvedBackgroundColorProperty = 
     BindableProperty.Create(
      nameof(CurvedBackgroundColor), 
      typeof(Color), 
      typeof(CurvedCornersLabel), 
      Color.Default); 

    public Color CurvedBackgroundColor 
    { 
     get { return (Color)GetValue(CurvedBackgroundColorProperty); } 
     set { SetValue(CurvedBackgroundColorProperty, value); } 
    } 

} 

//Android/iOS 

[assembly: ExportRenderer(typeof(CircleImage), typeof(CircleImageRenderer))] 
namespace SchedulingTool.iOS.Renderers 
{ 
    public class CircleImageRenderer : ImageRenderer 
    { 
     protected override void OnElementChanged(ElementChangedEventArgs<Image> e) 
     { 
      base.OnElementChanged(e); 

      if (e.NewElement != null) 
      { 
       var xfViewReference = (CircleImage)Element; 
       //Here you can reference xfViewReference.CurvedBackgroundColor to assign what ever is binded. 
      } 
     } 
    } 
} 

我希望你主要的想法,你可以創建自己的可綁定屬性和訪問他們在本地渲染。

如果如預期,你可以隨時下載的NuGet(已你需要的一切)一切不走:

https://github.com/jamesmontemagno/ImageCirclePlugin