Есть общий проект (.NET Standard
). В нём лежит файл User.json
с информацией о пользователях.
Нужно в этом проекте создать статический класс, к которому мы будем обращаться из платформозависимых проектов и получать информацию из файла.
Как получить из него данные в этом же .NET Standard
проекте?
Вот скриншот структуры проекта и расположения файла в нём:
Код Класса MainActivity:
namespace emailsender_standard.Droid
{
[Activity(Label = "emailsender_standard", MainLauncher = true, Icon = "@mipmap/icon")]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
Button sendBtn = FindViewById<Button>(Resource.Id.SendBtn);
sendBtn.Click += Button_Click;
}
private void Button_Click(object sender, EventArgs e)
{
EditText email = FindViewById<EditText>(Resource.Id.email);
EditText subject = FindViewById<EditText>(Resource.Id.subject);
EditText bodyText = FindViewById<EditText>(Resource.Id.bodyText);
if (Validation.IsValid(email.Text, subject.Text, bodyText.Text))
{
//work with file in Andoid Project
StreamReader strm = new StreamReader(Assets.Open("AndroidUsers.json"));
string response = strm.ReadToEnd();
var users = JsonConvert.DeserializeObject<List<User>>(response);
//TODO: work with file in NETStandard project
}
else
{
ShowAlert("Validation error", "Fill in all gaps and check e-mail");
}
}
void ShowAlert(string title, string message)
{
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
AlertDialog alert = dialog.Create();
alert.SetTitle(title);
alert.SetMessage(message);
alert.SetButton("OK", (c, ev) =>
{
// Ok button click task
});
alert.Show();
}
}
}