2016-09-24 40 views
0

我正在嘗試開發xamarin表單應用程序。我需要使用自定義的UI元素。例如,而不是使用Button,我需要使用MyAppButton。如何使用xaml中的自定義元素for xamarin表單應用程序

該功能如何實現?我認爲通過繼承Button來實現類MyAppButton是一個解決方案。這是正確的方法嗎?有沒有可用的示例實現?

感謝

+0

請參閱https://developer.xamarin.com/guides/xamarin-forms/custom-renderer/ –

+0

感謝您的回覆。我可以通過引用https://github.com/kirtisagar/XamarinFormsRadioButtonXAML來創建單選按鈕的自定義控件。我需要定製單選按鈕圖標。誰能幫我這個? – user3165999

+1

是的,但你必須展示你做了什麼,你在哪裏掙扎,你的想法是什麼。請用這些信息更新答案。提示:您必須查看如何在Android和iOS上更改此圖標並修改渲染器。 –

回答

0

@ user3165999回覆: 「我需要自定義單選按鈕圖標。誰能幫我這個?'

您需要做的第一件事是創建元素的自定義渲染器。 之後,您需要在* Renderer.cs中編寫自定義樣式,修改控件的字段(即元素)。

例如在Android中,你可以這樣做:

public class RadioButtonRenderer: ViewRenderer<CustomRadioButton, RadioButton> 
{ 
    protected override void OnLayout(bool changed, int l, int t, int r, int b) 
    { 
     base.OnLayout(changed, l, t, r, b); 

     if (Control != null) 
     { 
      var radioButton = Control; 
      // Resource.Drawable.backgroundradiobutton is an XML file that you have to create and insert in Resources -> Drawable -> backgroundradiobutton.xml 
      Drawable drawable = ResourcesCompat.GetDrawable(Resources, Resource.Drawable.backgroundradiobutton, Resources.NewTheme()); 
      radioButton.SetButtonDrawable(drawable); 
     } 
    } 
} 

backgroundradiobutton.xml

在這種情況下,這將必須是包含的你單選按鈕的圖形文件。 請注意,單選按鈕可以使用'State List Drawables'類來根據RadioButton的狀態更改圖形。 More Info Here

大多數定製圖形元件(例如按鈕,滑塊等)Android中經由XML文件所做的。 如果你不知道這個環境,你應該看看。

也許this會有幫助。

在iOS上,你可以這樣做:

class RadioButtonRenderer : ViewRenderer<CustomRadioButton, RadioButtonView> 
{ 
    protected override void OnElementChanged(ElementChangedEventArgs<RadioButton> e) 
    { 
     base.OnElementChanged(e); 
     var radioButton = Control; 
     radioButton.SetBackgroundImage(UIImage.FromFile("pathofimageinreourcesfolder.png"), UIControlState.Application); 
     // You need to set background image for each state. 
    } 
} 

正如你所看到的iOS渲染接受圖像作爲圖形資源。

事實上,(我認爲)的iOS定製是相對比較簡單

我希望我是清楚的,對不起,我的英語。
Byee。

相關問題