Xamarin.Forms 用户界面——控件——Text——Editor

编辑

PDF用于离线使用
示例代码:
相关API:

让我们知道你对此的感受

多行文本输入

Editor控件用于接受多行输入。本文将介绍:

定制

设置和阅读文本

编辑器,如其他文字呈现视图,公开的Text属性。Text可以用来设置和阅读由...提交的文字Editor。以下示例演示如何在XAML中设置文本:

<Editor Text="I am an Editor" />

在C#中:

var MyEditor = new Editor { Text = "I am an Editor" };

要阅读文本,请访问TextC#中的属性:

var text = MyEditor.Text;

键盘

用户与用户交互时显示的键盘Editor可以通过Keyboard属性以编程方式设置。

键盘类型的选项有:

  • 默认 - 默认键盘
  • 聊天 - 用于发短信和表情符号有用的地方
  • 电子邮件 - 用于输入电子邮件地址
  • 数字 - 输入数字时使用
  • 电话 - 输入电话号码时使用
  • 网址 - 用于输入文件路径和网址

我们的食谱部分中有每个键盘示例

颜色

Editor可以通过BackgroundColor属性设置为使用自定义背景颜色。需要特别注意确保每个平台上的颜色可用。因为每个平台的文本颜色的默认值都不同,所以您可能需要为每个平台设置自定义背景颜色。有关优化每个平台的UI的更多信息,请参阅使用平台调整

在C#中:

public partial class EditorPage : ContentPage
{
    public EditorPage ()
    {
        InitializeComponent ();
        var layout = new StackLayout { Padding = new Thickness(5,10) };
        this.Content = layout;
        //dark blue on Windows Phone & Android, light blue on iOS
        var editor = new Editor { BackgroundColor = Device.OnPlatform(Color.FromHex("#A4EAFF"), Color.FromHex("#2c3e50"), Color.FromHex("#2c3e50")) };
        layout.Children.Add(editor);
    }
}

在XAML中:

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="TextSample.EditorPage"
Title="Editor Demo">
    <ContentPage.Content>
        <StackLayout Padding="5,10">
            <Editor>
                <Editor.BackgroundColor>
                    <OnPlatform x:TypeArguments="x:Color"
                        iOS="#a4eaff"
                        Android="#2c3e50"
                        WinPhone="#2c3e50" />
                </Editor.BackgroundColor>
            </Editor>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

确保您选择的背景和文字颜色可在每个平台上使用,并且不要遮盖任何占位符文本。

互动

Editor 公开两件事:

  • TextChanged - 文本在编辑器中更改时引发。提供更改前后的文字。
  • 完成 - 当用户通过按键盘上的返回键结束输入时提高。

已完成

Completed事件用于对完成与...的交互作出反应EditorCompleted当用户通过输入键盘上的返回键结束字段输入时,它被提升。事件的处理程序是一个通用事件处理程序,采用发件人和EventArgs

void EditorCompleted (object sender, EventArgs e)
{
    var text = ((Editor)sender).Text; // sender is cast to an Editor to enable reading the `Text` property of the view.
}

完成的事件可以在代码和XAML中订阅:

在C#中:

public partial class EditorPage : ContentPage
{
    public EditorPage ()
    {
        InitializeComponent ();
        var layout = new StackLayout { Padding = new Thickness(5,10) };
        this.Content = layout;
        var editor = new Editor ();
        editor.Completed += EditorCompleted;
        layout.Children.Add(editor);
    }
}

在XAML中:

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="TextSample.EditorPage"
Title="Editor Demo">
    <ContentPage.Content>
        <StackLayout Padding="5,10">
            <Editor Completed="EditorCompleted" />
        </StackLayout>
    </ContentPage.Content>
</Contentpage>

框TextChanged

TextChanged事件用于对字段内容的更改做出反应。

TextChanged每当TextEditor变化时都会提出。事件的处理程序需要一个实例TextChangedEventArgsTextChangedEventArgs提供对Editor Text通过OldTextValueNewTextValue属性的旧的和新的值的访问:

void EditorTextChanged (object sender, TextChangedEventArgs e)
{
    var oldText = e.OldTextValue;
    var newText = e.NewTextValue;
}

完成的事件可以在代码和XAML中订阅:

代码:

public partial class EditorPage : ContentPage
{
    public EditorPage ()
    {
        InitializeComponent ();
        var layout = new StackLayout { Padding = new Thickness(5,10) };
        this.Content = layout;
        var editor = new Editor ();
        editor.TextChanged += EditorTextChanged;
        layout.Children.Add(editor);
    }
}

在XAML中:

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="TextSample.EditorPage"
Title="Editor Demo">
    <ContentPage.Content>
        <StackLayout Padding="5,10">
            <Editor TextChanged="EditorTextChanged" />
        </StackLayout>
    </ContentPage.Content>
</ContentPage>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Selecting the "Horizontal Scroll" and "Verticla Scroll" styles among the properties of your dialog box in the resource editor, you can add scroll bars to the dialog box. Remember also to select the 'resizing' border style. However for adding functionality to the scroll bars, you need to override the WM_VSCROLL and WM_HSCROLL message handlers. Also,override the WM_SIZE handler to set the scroll bar range if the size is reduced than the original. So you get the original size of the dialog in your OninitDialog(). The code would look something like this. Modify to your needs. 1. To OnInitDialog(),add the following line. GetWindowRect(m_rect); m_nScrollPos = 0; to get the original window size. Make m_rect a member variable of your dialog. Add another variable m_nScrollPos and initialize its value to zero. It stores the current vertical scroll position. 2. Here is the WM_SIZE handler for setting the scroll bar range.Set range 0 if size is increased more than original. void CCharlesDlg::OnSize(UINT nType, int cx, int cy) { CDialog::OnSize(nType, cx, cy); // TODO: Add your message handler code here m_nCurHeight = cy; int nScrollMax; if (cy < m_rect.Height()) { nScrollMax = m_rect.Height() - cy; } else nScrollMax = 0; SCROLLINFO si; si.cbSize = sizeof(SCROLLINFO); si.fMask = SIF_ALL;// SIF_ALL = SIF_PAGE | SIF_RANGE | SIF_POS; si.nMin = 0; si.nMax = nScrollMax; si.nPage = si.nMax/10; si.nPos = 0; SetScrollInfo(SB_VERT, &si, TRUE); } You need m_nCurHeight to store the current height of the dialog and use it to handle the scrolling in OnVScroll. m_ncurHeight is also a member variable of the dialog. 3. Here is the handler for WM_VSCROLL. void CCharlesDlg::OnVScroll(UINT nSBCode,UINT nPos,CScrollBar* pScrollBar) { //TODO:Add your message handler code here and/or call default int nDelta; int nMaxPos = m_rect.Height() - m_nCurHeight; switch(nSBCode) { case SB_LINEDOWN: if (m_nScrollPos >= nMaxPos) return; nDelta = min(nMaxPos/100,nMaxPos-m_nScrollPos); break; case SB_LINEUP: if (m_nScrollPos <= 0) return; nDelta = -min(nMaxPos/100,m_nScrollPos); break; case SB_PAGEDOWN: if (m_nScrollPos >= nMaxPos) return; nDelta = min(nMaxPos/10,nMaxPos-m_nScrollPos); break; case SB_THUMBPOSITION: nDelta = (int)nPos - m_nScrollPos; break; case SB_PAGEUP: if (m_nScrollPos <= 0) return; nDelta = -min(nMaxPos/10,m_nScrollPos); break; default: return; } m_nScrollPos += nDelta; SetScrollPos(SB_VERT,m_nScrollPos,TRUE); ScrollWindow(0,-nDelta); CDialog::OnVScroll(nSBCode, nPos, pScrollBar); } The above code handles the vertical scrolling. For horizontal scrolling add the WM_HSCROLL similarly and add the necessary code to OnSize and OnInitDialog. Information provided in this document and any software that may accompany this document is provided "as is" without warranty of any kind, either expressed or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose. The user assumes the entire risk as to the accuracy and the use of this information.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值