-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMainFragment.cs
81 lines (66 loc) · 1.99 KB
/
MainFragment.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
using System;
using Android.OS;
using Android.Runtime;
using Android.Support.V4.App;
using Android.Views;
using Android.Widget;
using TipCalc;
using Xamarin.Forms;
using Button = Android.Widget.Button;
using TextChangedEventArgs = Android.Text.TextChangedEventArgs;
using View = Android.Views.View;
namespace FormsEmbeddingPreview.Droid
{
public class MainFragment : Fragment
{
private double _initialAmount;
private double _lastTipAmount;
private bool _subscribed;
private void SubscribeToTipMessages()
{
if (_subscribed)
{
return;
}
MessagingCenter.Subscribe<object, TipArgs>(Messages.Sender, Messages.Tip,
(obj, args) =>
{
_lastTipAmount = args.Tip;
});
_subscribed = true;
}
protected MainFragment(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
{
SubscribeToTipMessages();
}
public MainFragment()
{
SubscribeToTipMessages();
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var view = inflater.Inflate(Resource.Layout.MainFragment, container, false);
var tipCalcButton = view.FindViewById<Button>(Resource.Id.navigateToTipCalc);
tipCalcButton.Click += TipCalcButtonOnClick;
var initialAmount = view.FindViewById<EditText>(Resource.Id.initialAmount);
initialAmount.TextChanged += InitialAmountOnTextChanged;
var tipAmount = view.FindViewById<TextView>(Resource.Id.tipAmount);
tipAmount.Text = _lastTipAmount.ToString("C");
return view;
}
private void InitialAmountOnTextChanged(object sender, TextChangedEventArgs textChangedEventArgs)
{
double result;
if (double.TryParse(textChangedEventArgs.Text.ToString(), out result))
{
_initialAmount = result;
}
}
private void TipCalcButtonOnClick(object sender, EventArgs eventArgs)
{
((MainActivity)Activity).NavigateToTipCalc();
MessagingCenter.Send(Messages.Sender, Messages.InitialAmount,
new InitialAmountArgs(_initialAmount));
}
}
}