Reputation: 41
I want to use Messaging Center for looping through a form to save as a JSON. I have 2 Accordion views on my MainPage.xaml and inside the view are labels/checkboxes/entry.
when I press the save button in my accordion view it performs this code which saves as a json:
public class InspectionSchemeChecks
{
public string InspectionCategory { get; set; }
public List<InsScheme> InsScheme { get; set; }
}
public class InsScheme
{
public string InspectionName { get; set; }
public string Yes { get; set; }
public string No { get; set; }
public string InspectionNotes { get; set; }
}
public void SaveButton_Clicked(object sender, EventArgs e)
{
MessagingCenter.Subscribe<MainPage>(this, "Hi", (JsonSend) => {
// do something whenever the "Hi" message is sent
var SchemeChecks = new InspectionSchemeChecks();
var InspectionList = new List<InsScheme>();
SchemeChecks.InspectionCategory = HeaderLabel.Text;
InspectionList.Add(new InsScheme()
{
InspectionName = TempLabel.Text,
Yes = TempYCheckBox.Checked.ToString(),
No = TempNCheckBox.Checked.ToString(),
InspectionNotes = TempNotes.Text
});
InspectionList.Add(new InsScheme()
{
InspectionName = OilLevelLabel.Text,
Yes = OilLevelYCheckBox.Checked.ToString(),
No = OilLevelNCheckBox.Checked.ToString(),
InspectionNotes = OilLevelNotes.Text
});
InspectionList.Add(new InsScheme()
{
InspectionName = RefrigerantLevelLabel.Text,
Yes = RefrigerantLevelYCheckBox.Checked.ToString(),
No = RefrigerantLevelNCheckBox.Checked.ToString(),
InspectionNotes = RefrigerantLevelNotes.Text
});
InspectionList.Add(new InsScheme()
{
InspectionName = VICLabel.Text,
Yes = VICYCheckBox.Checked.ToString(),
No = VICNCheckBox.Checked.ToString(),
InspectionNotes = VICNotes.Text
});
InspectionList.Add(new InsScheme()
{
InspectionName = CheckEvaporatorLabel.Text,
Yes = CheckEvaporatorYCheckBox.Checked.ToString(),
No = CheckEvaporatorNCheckBox.Checked.ToString(),
InspectionNotes = CheckEvaporatorNotes.Text
});
InspectionList.Add(new InsScheme()
{
InspectionName = SPLabel.Text,
Yes = SPYCheckBox.Checked.ToString(),
No = SPNCheckBox.Checked.ToString(),
InspectionNotes = SPNotes.Text
});
InspectionList.Add(new InsScheme()
{
InspectionName = DPLabel.Text,
Yes = DPYCheckBox.Checked.ToString(),
No = DPNCheckBox.Checked.ToString(),
InspectionNotes = DPNotes.Text
});
SchemeChecks.InsScheme = InspectionList;
var json = JsonConvert.SerializeObject(SchemeChecks, Newtonsoft.Json.Formatting.Indented);
});
}
in my MainPage.xaml.cs I have a submit button that has this line of code to call the message MessagingCenter.Send(this, "Hi");
When I click that I want to combine the JSON together could I use the messagingCentre for that if so could I have guidance
Upvotes: 0
Views: 113
Reputation: 1583
Something looks weird in your MessagingCenter implementation, you're subscribing every time the Save button is clicked, which is wrong. You usually only subscribe once and unsubscribe when you're not interested in receiving messages anymore.
Also, I assume you're converting to json because you taught we can only pass string as message? If so, this is not the case, we can pass any object as message.
I'm not sure I understand your implementation, I've created a simple implementation that shows how to pass a InspectionSchemeChecks
object between two classes.
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
protected override void OnAppearing()
{
base.OnAppearing();
MessagingCenter.Subscribe<SomeOtherClass, InspectionSchemeChecks>(this, "InspectionSchemeChecks", OnNewMessage);
}
protected override void OnDisappearing()
{
MessagingCenter.Unsubscribe<SomeOtherClass, InspectionSchemeChecks>(this, "InspectionSchemeChecks");
base.OnDisappearing();
}
private void OnNewMessage(SomeOtherClass sender, InspectionSchemeChecks schemeChecks)
{
// Do what you want
}
}
public class SomeOtherClass
{
public void SaveButton_Clicked(object sender, EventArgs e)
{
var SchemeChecks = new InspectionSchemeChecks();
var InspectionList = new List<InsScheme>();
//...
SchemeChecks.InsScheme = InspectionList;
MessagingCenter.Send<SomeOtherClass, InspectionSchemeChecks>(this, "InspectionSchemeChecks", SchemeChecks);
}
}
You can easily modify this example to send a string instead of InspectionSchemeChecks
object as argument if you prefer to send the json as message.
Upvotes: 1