[英]How to make Rounded Editor control in Xamarin Forms
有誰知道如何創建圓角編輯器? 我嘗試在兩個平台上實現渲染器,但沒有找到我想要的東西。
你的方向是正確的。 您需要為每個平台創建自定義渲染。 請按照以下步驟在兩個平台中創建一個舍入的編輯器:
PCL中的自定義控件RoundedEditor
:
public class RoundedEditor:Editor { //empty or define your custom fields }
對於Android平台(在YourProject.Android
):
在Resources/Drawable/
創建一個xml RoundedEditText.xml
:
<?xml version="1.0" encoding="utf-8" ?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" android:padding="10dp" > <!--solid defines the fill color of the Editor--> <solid android:color="#FFFFFF"/> <!--stroke defines the border color--> <stroke android:width="2dp" android:color="#FF0000" /> <!--the corner radius--> <corners android:bottomRightRadius="15dp" android:bottomLeftRadius="15dp" android:topLeftRadius="15dp" android:topRightRadius="15dp"/> </shape>
像這樣創建自定義渲染器:
[assembly:ExportRenderer(typeof(RoundedEditor), typeof(RoundedEditorRenderer))] namespace RoundedEditorDemo.Droid { public class RoundedEditorRenderer:EditorRenderer { protected override void OnElementChanged(ElementChangedEventArgs<Editor> e) { base.OnElementChanged(e); if (Control != null) { Control.Background = Xamarin.Forms.Forms.Context.GetDrawable(Resource.Drawable.RoundedEditText); } } } }
對於Windows平台(在YourProject.UWP
):
通過right click your project->Add->New Item->Resource Dictionary->rename to RoundedEditorRes.xaml
創建ResourceDictionary
文件,並將完整的TextBox
默認樣式添加到資源字典中。
編輯Border
元素(添加CornerRadius="15"
並將BorderThickness="{TemplateBinding BorderThickness}"
更改為TextBox的默認樣式的BorderThickness="2"
)並為該樣式添加一個鍵:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:RoundedEditorDemo.UWP"> ... <Border CornerRadius="15" BorderThickness="2" x:Name="BorderElement" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" Grid.ColumnSpan="2" Grid.Row="1" Grid.RowSpan="1"/> ... </ResourceDictionary>
創建自定義渲染器:
[assembly: ExportRenderer(typeof(RoundedEditor), typeof(RoundedEditorRenderer))] namespace RoundedEditorDemo.UWP { public class RoundedEditorRenderer:EditorRenderer { protected override void OnElementChanged(ElementChangedEventArgs<Editor> e) { base.OnElementChanged(e); if (Control != null) { Windows.UI.Xaml.ResourceDictionary dic = new Windows.UI.Xaml.ResourceDictionary(); dic.Source = new Uri("ms-appx:///RoundedEditorRes.xaml"); Control.Style = dic["RoundedEditorStyle"] as Windows.UI.Xaml.Style; } } } }
這是完整的演示: RoundedEditorDemo 。
更新:
我無法重現背景問題,我正在使用Windows更新15063.所以我認為如果您更新到最新更新將修復它。
對於Android部分:請注意我使用的是Xamarin.Forms.Forms.Context.GetDrawable
,它由Xamarin.Forms提供。 請嘗試在您的計算機上運行我的完整演示,以檢查是否收到錯誤。
如果仍然出現錯誤。 你能指出,你在哪收到錯誤?
除非我遺漏了什么,否則你不需要自定義渲染器或類似的東西。 你需要的只是一個框架。
我是這樣做的:
<Grid>
<Frame IsClippedToBounds="true">
<Editor />
</Frame>
</Grid>
為我工作!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.