Let's start by creating a new folder in the FileStorage.Portable
project, adding a new file called IMethods.cs
, and implementing the following:
public interface IMethods { #region Methods void Exit(); void DisplayEntryAlert(TaskCompletionSource<string> tcs, string message); #endregion }
For all native projects, add a new folder called Extras
. Let's start with the iOS project. add a new file called IOSMethods.cs
, and implement the following:
public class IOSMethods : IMethods { #region Public Methods public void Exit() { UIApplication.SharedApplication.PerformSelector(new ObjCRuntime.Selector("terminateWithSuccess"), null, 0f); } public void DisplayEntryAlert(TaskCompletionSource<string> tcs, string message) { UIAlertView alert = new UIAlertView(); alert.Title = "Title"; alert.AddButton("OK"); alert.AddButton("Cancel"); alert.Message = message; alert.AlertViewStyle = UIAlertViewStyle.PlainTextInput; alert.Clicked += (object s, UIButtonEventArgs ev) => { if (ev.ButtonIndex == 0) { tcs.SetResult(alert.GetTextField(0).Text); } else { tcs.SetResult(null); } }; alert.Show(); } #endregion }
We should recognize the Exit
function from previous chapters. The DisplayEntryAlert
function creates a PlainTextInputUIAlertView
. This alert will ask for text input via a textbox and we can retrieve this text value using the GetTextField
function. The alert will also display a Yes
and No
button, so when the user enters text and presses Yes
, a new file will be created with the text input set as the filename.
Now let's replicate the same procedure for Android. Add a new file called DroidMethods.cs
and implement the following:
public class DroidMethods : IMethods { #region Public Methods public void Exit() { Android.OS.Process.KillProcess(Android.OS.Process.MyPid()); } public void DisplayEntryAlert(TaskCompletionSource<string> tcs, string message) { var context = Forms.Context; LayoutInflater factory = LayoutInflater.From(context); var view = factory.Inflate(Resource.Layout.EntryAlertView, null); var editText = view.FindViewById<EditText>(Resource.Id.textEntry); new AlertDialog.Builder(context) .SetTitle("Chat") .SetMessage(message) .SetPositiveButton("Ok", (sender, e) => { tcs.SetResult(editText.Text); }) .SetNegativeButton("Cancel", (sender, e) => { tcs.SetResult(null); }) .SetView(view) .Show(); } #endregion }
This time for Android, we are using the AlertDialog.Builder
framework. We use the Forms.Context
property to retrieve the current context, which we use to create a new AlertDialog.Builder
. We have to use the SetView
function in this framework to assign a custom view for text input. This custom view is created using a new XML layout.
Add a new file called EntryAlertView.xml
to the Resources | layout folder and implement the following:
<?xml version="1.0" encoding="utf-8"?> <EditText xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/textEntry" android:layout_height="wrap_content" android:layout_width="250px" android:layout_centerHorizontal="true" android:singleLine="true" />
All we have is an EditText
object to retrieve the filename from the user in the alert dialog. Using FindViewById
in the DroidMethods
class, we can reference this EditText
item to retrieve the text value entered by the user.
That's everything. Our next step is a customized ContentPage
to handle the Alert
events from each view-model.
18.218.78.102