如何在 Xamarin Forms 中使用 StreamReader 填写表单
How to fill out a form from StreamReader in Xamarin Forms
我正在 Xamarin Forms 中创建一个项目,允许用户输入他们的姓名、用户名、密码,并使用步进器告诉他们年龄,并使用开关来识别用户是否是学生。然后用户可以点击保存,信息将通过 StreamWriter 保存到一个文件中。然后用户将能够点击“加载”,他们所有保存的信息将自动填充输入框、年龄值和学生“真或假”开关。
我在 MainPage.xaml.cs
class 中遇到问题,无法加载保存的信息以填充字段。我已将 MainPage.xaml
中的控件名称绑定到 EntryFields
class 中的 属性 值。这是我第一次使用 Xamarin Forms,我被卡住了。请帮忙 :)
这是我在遇到问题之前保存的方法所拥有的:
{
List<string> data = new List<string>();
bool stepValue;
int ageValue;
//Read data from file
if (File.Exists(_fileName))
{
using (var reader = new StreamReader(_fileName))
{
string line;
while ((line = reader.ReadLine()) != null)
{
data.Add(line);
}
for(int i = 0; i < data.Count; i++)
{
if (data[4] == "true")
{
stepValue = true;
}
else if (data[4] == "false")
{
stepValue = false;
}
ageValue = Int32.Parse(data[3]);
//This is where I am stuck. I want to add each item to my list of EntryFields
//but I am not sure how since they're all different values.
}
}
}
else
{
DisplayAlert("Error", "Unable to load. Fill out required information and select SAVE.", "Go Back");
}
}
这是我的 EntryFields
class,看起来像这样:
{
public bool TrueOrFalse { get; set; }
public string Name { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public int Age { get; set; }
public EntryFields(string name, string username, string password, int age, string student)
{
Name = name;
Username = username;
Password = password;
Age = age;
if (student.ToLower() == "true")
{
TrueOrFalse = true;
}
else if(student.ToLower() == "false")
{
TrueOrFalse = false;
}
}
}
最后,所以我们都在看同一件事,这是我的 MainPage.xaml
class:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MorganHall_CE01.MainPage">
<StackLayout IsVisible="True">
<Frame BackgroundColor="#2196F3" Padding="24" CornerRadius="0">
<Label Text="Code Exercise 1" HorizontalTextAlignment="Center" TextColor="White" FontSize="36"/>
</Frame>
<Entry x:Name="nameEntry" Placeholder="Enter Name Here" HorizontalOptions="FillAndExpand" HorizontalTextAlignment="Center" Text="{Binding Path=Name}" TextColor="#514E4E" />
<Entry x:Name="usernameEntry" Placeholder="Enter Username Here" HorizontalOptions="FillAndExpand" HorizontalTextAlignment="Center" Text="{Binding Username}" TextColor="#514E4E" />
<Entry x:Name="passwordEntry" Placeholder="Enter Password Here" HorizontalOptions="FillAndExpand" HorizontalTextAlignment="Center" Text="{Binding Password}" IsPassword="True" TextColor="#514E4E" />
<StackLayout Orientation="Horizontal" Margin="10">
<Label x:Name="ageLabel" Text="Age: " HorizontalTextAlignment="Start" HorizontalOptions="FillAndExpand" FontAttributes="Bold" TextColor="#000000" />
<Stepper x:Name="ageStepper" Minimum="0" Maximum="100" Increment="1" Margin="50,0" Value="{Binding Age}" />
</StackLayout>
<StackLayout Orientation="Horizontal">
<Label x:Name="studentLabel" Text="Enrolled Student: " HorizontalTextAlignment="Start" HorizontalOptions="FillAndExpand" FontAttributes="Bold" TextColor="#000000" />
<Switch x:Name="studentSwitch" IsToggled="{Binding TrueOrFalse}" Margin="50,0" OnColor="#60E848" ThumbColor="#484453" />
</StackLayout>
<StackLayout Orientation="Horizontal">
<Button x:Name="loadData" Text="Load" HorizontalOptions="EndAndExpand" FontAttributes="Bold" BackgroundColor="#138A83" TextColor="#FDFAFA" />
<Button x:Name="saveData" Text="Save" HorizontalOptions="StartAndExpand" FontAttributes="Bold" BackgroundColor="#075D7F" TextColor="#FBF8F8" />
</StackLayout>
</StackLayout>
</ContentPage>
在 MainPage.xaml.cs
class 中,我有一个 EntryFields 列表,如下所示:
private List<EntryFields> entries = new List<EntryFields>();
请告诉我有更好更简单的方法来加载保存的信息。我都糊涂了。
提前致谢! :)
像这样的东西应该有用
var entries = new EntryFields();
// this will read all the file data into a string array
var data = File.ReadAllLines(_fileName);
// you will need to be sure the order matches the order you saved in
entries.Name = data[1];
entries.Username = data[2];
entries.Password = data[3];
entries.Age = int.Parse(data[4]);
// set the binding context of the page
this.BindingContext = entries;
我正在 Xamarin Forms 中创建一个项目,允许用户输入他们的姓名、用户名、密码,并使用步进器告诉他们年龄,并使用开关来识别用户是否是学生。然后用户可以点击保存,信息将通过 StreamWriter 保存到一个文件中。然后用户将能够点击“加载”,他们所有保存的信息将自动填充输入框、年龄值和学生“真或假”开关。
我在 MainPage.xaml.cs
class 中遇到问题,无法加载保存的信息以填充字段。我已将 MainPage.xaml
中的控件名称绑定到 EntryFields
class 中的 属性 值。这是我第一次使用 Xamarin Forms,我被卡住了。请帮忙 :)
这是我在遇到问题之前保存的方法所拥有的:
{
List<string> data = new List<string>();
bool stepValue;
int ageValue;
//Read data from file
if (File.Exists(_fileName))
{
using (var reader = new StreamReader(_fileName))
{
string line;
while ((line = reader.ReadLine()) != null)
{
data.Add(line);
}
for(int i = 0; i < data.Count; i++)
{
if (data[4] == "true")
{
stepValue = true;
}
else if (data[4] == "false")
{
stepValue = false;
}
ageValue = Int32.Parse(data[3]);
//This is where I am stuck. I want to add each item to my list of EntryFields
//but I am not sure how since they're all different values.
}
}
}
else
{
DisplayAlert("Error", "Unable to load. Fill out required information and select SAVE.", "Go Back");
}
}
这是我的 EntryFields
class,看起来像这样:
{
public bool TrueOrFalse { get; set; }
public string Name { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public int Age { get; set; }
public EntryFields(string name, string username, string password, int age, string student)
{
Name = name;
Username = username;
Password = password;
Age = age;
if (student.ToLower() == "true")
{
TrueOrFalse = true;
}
else if(student.ToLower() == "false")
{
TrueOrFalse = false;
}
}
}
最后,所以我们都在看同一件事,这是我的 MainPage.xaml
class:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MorganHall_CE01.MainPage">
<StackLayout IsVisible="True">
<Frame BackgroundColor="#2196F3" Padding="24" CornerRadius="0">
<Label Text="Code Exercise 1" HorizontalTextAlignment="Center" TextColor="White" FontSize="36"/>
</Frame>
<Entry x:Name="nameEntry" Placeholder="Enter Name Here" HorizontalOptions="FillAndExpand" HorizontalTextAlignment="Center" Text="{Binding Path=Name}" TextColor="#514E4E" />
<Entry x:Name="usernameEntry" Placeholder="Enter Username Here" HorizontalOptions="FillAndExpand" HorizontalTextAlignment="Center" Text="{Binding Username}" TextColor="#514E4E" />
<Entry x:Name="passwordEntry" Placeholder="Enter Password Here" HorizontalOptions="FillAndExpand" HorizontalTextAlignment="Center" Text="{Binding Password}" IsPassword="True" TextColor="#514E4E" />
<StackLayout Orientation="Horizontal" Margin="10">
<Label x:Name="ageLabel" Text="Age: " HorizontalTextAlignment="Start" HorizontalOptions="FillAndExpand" FontAttributes="Bold" TextColor="#000000" />
<Stepper x:Name="ageStepper" Minimum="0" Maximum="100" Increment="1" Margin="50,0" Value="{Binding Age}" />
</StackLayout>
<StackLayout Orientation="Horizontal">
<Label x:Name="studentLabel" Text="Enrolled Student: " HorizontalTextAlignment="Start" HorizontalOptions="FillAndExpand" FontAttributes="Bold" TextColor="#000000" />
<Switch x:Name="studentSwitch" IsToggled="{Binding TrueOrFalse}" Margin="50,0" OnColor="#60E848" ThumbColor="#484453" />
</StackLayout>
<StackLayout Orientation="Horizontal">
<Button x:Name="loadData" Text="Load" HorizontalOptions="EndAndExpand" FontAttributes="Bold" BackgroundColor="#138A83" TextColor="#FDFAFA" />
<Button x:Name="saveData" Text="Save" HorizontalOptions="StartAndExpand" FontAttributes="Bold" BackgroundColor="#075D7F" TextColor="#FBF8F8" />
</StackLayout>
</StackLayout>
</ContentPage>
在 MainPage.xaml.cs
class 中,我有一个 EntryFields 列表,如下所示:
private List<EntryFields> entries = new List<EntryFields>();
请告诉我有更好更简单的方法来加载保存的信息。我都糊涂了。 提前致谢! :)
像这样的东西应该有用
var entries = new EntryFields();
// this will read all the file data into a string array
var data = File.ReadAllLines(_fileName);
// you will need to be sure the order matches the order you saved in
entries.Name = data[1];
entries.Username = data[2];
entries.Password = data[3];
entries.Age = int.Parse(data[4]);
// set the binding context of the page
this.BindingContext = entries;