在我的内容页面(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>
答案 0 :(得分:1)
您可以使用转换器来实现它。
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 "";
}
}
}
<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>