Xamarin字符串列表和标签文本之间的比较

时间:2017-06-15 22:48:50

标签: c# xaml xamarin xamarin.forms

我是Xamarin的新手,我有两个字符串的列表,我想比较小写列表的元素是否等于小写标签的元素。

当我将.ToLower()添加到标签的元素时,调试停止并且我不知道原因。

这是我的代码:

Dictionary<int, string> WordsList = new Dictionary<int, string>();
Dictionary<int, string> WordsList2 = new Dictionary<int, string>();

public TestWords()
{
   InitializeComponent();

   mywordsdatabase = new MyWordsDatabase();

   var mywords = mywordsdatabase.GetWords();

   int i = 0;

   // TestAnswer.IsVisible = false;
   foreach (var myword in mywords)
   {
       WordsList[i] = myword.Word1;
       WordsList2[i] = myword.Word2.ToLower(); // this is ok

       i++;
   }

   word10.Text = WordsList[0];
   word11.Text = WordsList[1];

   word20.Focus(); 
   word21.Focus();
}

//Correction for the test
void OnOKTest(object sender, EventArgs args)
{
     int yes = 0;
     int no = 0;

     //############ if == #############
     if (WordsList2[0] == word20.Text.ToLower())  // but this is not accepted
     {
         true0.Text = "✔";
         yes++;
     }

     if (WordsList2[1] == word21.Text.ToLower())  // but this is not accepted
     {
         true1.Text = "✔";
         yes++;
     }
} 

这是我的xaml:

<Grid VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" Padding="5">
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>

    <Label x:Name="word10" Text="Your Text" FontSize="20" Grid.Row="0" Grid.Column="0" />
    <Label x:Name="word11" Text="Your Text" FontSize="20" Grid.Row="1" Grid.Column="0" />
    <Entry x:Name="word20" Placeholder="The word traduction" FontSize="20" Grid.Row="0" Grid.Column="2" ></Entry>
    <Entry x:Name="word21" Placeholder="The word traduction" FontSize="20" Grid.Row="1" Grid.Column="2" ></Entry>
    <Label x:Name="true0" Text="" FontSize="20"  Grid.Row="0" Grid.Column="3" />
    <Label x:Name="true1" Text="" FontSize="20"  Grid.Row="1" Grid.Column="3" />
</Grid>

由于

2 个答案:

答案 0 :(得分:1)

您的问题似乎是在空对象上使用ToLower()

将您的Ifs更改为以下内容:

if (string.Equals (WordsList2.ElementAtOrDefault (0), word20.Text, StringComparison.InvariantCultureIgnoreCase))
{ 
}

if (string.Equals (WordsList2.ElementAtOrDefault (1), word21.Text, StringComparison.InvariantCultureIgnoreCase))
{ 
}

使用string.Equals是比较字符串的更好方法,因为您可以定义不同的验证方式。

使用StringComparison.InvariantCultureIgnoreCase,您无需将其显式转换为小写。

使用ElementAtOrDefault可以防止和IndexOutOfBounds异常,因为如果指定的索引没有项目,它将返回默认值,在您的情况下,strings默认值为NULL。要使用此功能,您需要添加using System.Linq

答案 1 :(得分:0)

我不确定你是什么意思&#34;不被接受&#34;但null组件的ToLower()属性的默认值为sonarqube { properties { property "sonar.projectName", "Android 9" property "sonar.projectKey", "org.sonarqube:Android9" property "sonar.host.url", "http://localhost:9000" property "sonar.username", "admin" property "sonar.password", "password" property "sonar.projectVersion", "5" property "sonar.java.source", "7" property "sonar.android.lint.report", "build/outputs/lint-results.xml" property "sonar.sourceEncoding","UTF-8" property "sonar.sources","src/main/java" property "sonar.exclusions","build/**,**/*.png" property "sonar.test.exclusions", "" property "sonar.binaries", "build/classes" property "sonar.jacoco.reportPath","build/jacoco/testDevDebugUnitTest.exec" property "sonar.tests", "src/androidTest/java" //property "sonar.import_unknown_files", true //property "sonar.android.lint.report", "./build/outputs/lint-results.xml" property "sonar.jacoco.itReportPath", "build/outputs/code-coverage/connected/flavors/NORMAL/*.ec" }} 。在调用{{1}}之前确保它有值。