xamarin - 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>
解决方案
您可以使用 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>
推荐阅读
- excel - Excel 移动栏中的文本
- wordpress - 本地主机 wordpress xampp(在此服务器上找不到请求的 URL。)
- html - 我在需要 CSS 代码的网站中添加杂耍时遇到问题
- flutter - StreamProvider - 显示加载微调器(预加载器)以在 Flutter 中加载数据的问题
- javascript - 使用 Typescript 构建到 dist/ 文件夹时保持 src/ 文件夹结构,但仍然能够引用外部文件?
- lightningchart - 如何从闪电图中的选定点获取数据
- c++ - 如何从 Base 获取未覆盖的函数以更新到派生的覆盖函数?
- c++ - 为什么这个嵌套的 for 循环会打印这个?
- c# - 运行 PuppeteerExtraSharp 的演示代码时如何解决错误
- python - 使用 React-konva 时 React 中出现错误“e.GetBoundingClientRect() 不是函数”