首页 > 解决方案 > Xamarin 表单的 Keyup 事件处理程序?

问题描述

在我的内容页面 (XAML) 中,假设我有这个:

        <StackLayout>
        <Entry x:Name="courseScore" Placeholder="Course Score" Keyboard="Numeric" />
        <Entry x:Name="courseGrade" Placeholder="Course Grade" Keyboard="Text" IsReadOnly="True" />
    </StackLayout>

如何为 courseScore 创建事件处理程序(例如 onKeyup)?我想根据代码后面的课程分数自动填充课程成绩?

卢卡斯提供的解决方案:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:d="http://xamarin.com/schemas/2014/forms/design"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         xmlns:local="clr-namespace:Xamarin_SQLite.AppFunctions"
         mc:Ignorable="d"
         x:Class="Xamarin_SQLite.Views.StudentCourseResult_Add">
<ContentPage.Resources>
    <ResourceDictionary>
        <local:ScoreConverter x:Key="ScoreConverter" />
    </ResourceDictionary>
</ContentPage.Resources>
<ContentPage.Content>
    <StackLayout>
        <Picker Title="Select a course" x:Name="courseName">
            <Picker.Items>
                <x:String>Web Design</x:String>
                <x:String>Database Design</x:String>
                <x:String>Web Programming</x:String>
            </Picker.Items>
        </Picker>
        <Entry x:Name="courseScore" Placeholder="Course Score" Keyboard="Numeric" />
        <Entry x:Name="courseGrade" Placeholder="Course Grade" Keyboard="Text" IsReadOnly="True" />
        <Button x:Name="btnAdd" Clicked="btnAdd_Clicked" Text="Add"/>
    </StackLayout>
</ContentPage.Content>
</ContentPage>

标签: xamarinxamarin.forms

解决方案


您可以使用 Converter来实现它。

using System;
using System.Globalization;
using Xamarin.Forms;

namespace xxxx
{
    public class ScoreConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if(value!=null&& value.ToString().Length!=0)
            {
                int score = int.Parse(value.ToString()); 

                if (score < 60)
                {
                    return "E";
                }
                else if (score >= 60 && score < 70)
                {
                    return "D";
                }
                else if (score >= 70 && score < 80)
                {
                    return "C";
                }
                else if (score >= 80 && score < 90)
                {
                    return "B";
                }
                else
                {
                    return "A";
                }
            }

            return "";
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return "";
        }
    }

}

在xml中

<ContentPage.Resources>
   <ResourceDictionary>
      <local:ScoreConverter x:Key="ScoreConverter" />
   </ResourceDictionary>
</ContentPage.Resources>
    
<StackLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
        
   <Entry x:Name="courseScore" Placeholder="Course Score" Keyboard="Numeric" />
   <Entry x:Name="courseGrade" Placeholder="Course Grade" Keyboard="Text" IsReadOnly="True"  Text="{Binding Source={x:Reference courseScore}, Path=Text,Converter={StaticResource ScoreConverter} ,Mode=OneWay}"/>
</StackLayout>

在此处输入图像描述


推荐阅读