Activate second screen after first screen has closed - caliburn.micro

I am learning Caliburn.Micro by creating this simple demo app. I want to display my LoginView first. Then, after the credentials have been entered and the LoginView has been closed/deactivated, I want to display my UserView. I use the TryClose() method in the LoginViewModel to close the screen. When I run the code below, e.WasClosed is true, the ActivateItem(new UserViewModel()) line is performed, but the LoginView is re-activated.
How do I get the LoginView to completely close/deactivate and the UserView to activate?
Here is the code:
class MainViewModel : Conductor<object>.Collection.OneActive
{
LoginViewModel lvm = new LoginViewModel();
public MainViewModel()
{
ActivateItem(lvm);
lvm.Deactivated += new EventHandler<DeactivationEventArgs>(lvm_Deactivated);
}
void lvm_Deactivated(object sender, DeactivationEventArgs e)
{
if (e.WasClosed)
{
ActivateItem(new UserViewModel());
}
}
}

1st deactive and close the loginview
this.DeactivateItem(this.lvm, true);
Then activate user view
ActivateItem(new UserViewModel());
final code
this.DeactivateItem(this.lvm, true);
ActivateItem(new UserViewModel());

Related

How to access a field in a business object and copy its info to another business object from a simple action button?

I have a custom simple action button called "Add to Cart". The button has to copy the values of the properties of a product (productID, productName, productPrice, etc.) and copy them to the properties of another business object called "Cart". How can I do it?
public class ActionInPopupController : ViewController
{
public ActionInPopupController()
{
SimpleAction actionInPopup = new SimpleAction(this,
"Add To Cart",
DevExpress.Persistent.Base.PredefinedCategory.PopupActions
);
actionInPopup.Execute += ActionInPopup_Execute;
}
void ActionInPopup_Execute(object sender, SimpleActionExecuteEventArgs e)
{
Products currentObject = View.CurrentObject as Products;
}
private void InitializeComponent()
{
//
// ActionInPopupController
//
this.TargetViewId = "Products_DetailView";
}
}
This is the code for the button and im guessing the code for that to work needs to go in ActionInPopup_Execute. I don't know how to though.

How could I change the Navigastion's page arrow in Xamarin Forms?

I'm creating an app using xamarin Forms (multiplatform), I'm using a Navigation page, but I want to change the arrow ("<-") to text ("back")
Do you know how could i do it?
Thanks
(I'm going to use it in an Android App, but I'm creating the app using Xamarin forms)
You could use custom renderer to remove the navigation icon and set it with text. But, when you do that, you need to capture the click of the text and simulate the back event.
Create the interface:
public class CustomNavigationPage : NavigationPage
{
public CustomNavigationPage(Page startupPage) : base(startupPage)
{
}
}
The implementation of Android:
[assembly: ExportRenderer(typeof(CustomNavigationPage),
typeof(NavigationPageRenderer_Droid))]
namespace NavigationPageDemo.Droid
{
public class NavigationPageRenderer_Droid : NavigationPageRenderer
{
public Android.Support.V7.Widget.Toolbar toolbar;
public Activity context;
public NavigationPageRenderer_Droid(Context context) : base(context)
{
}
protected override Task<bool> OnPushAsync(Page view, bool animated)
{
var retVal = base.OnPushAsync(view, animated);
context = (Activity)Forms.Context;
toolbar = context.FindViewById<Android.Support.V7.Widget.Toolbar>(Droid.Resource.Id.toolbar);
if (toolbar != null)
{
//if (toolbar.NavigationIcon != null)
//{
//toolbar.NavigationIcon = Android.Support.V7.Content.Res.AppCompatResources.GetDrawable(context, Resource.Drawable.back);
//toolbar.NavigationIcon = null;
toolbar.NavigationIcon = null;
toolbar.Title = "back";
toolbar.SetOnClickListener(new OnClick());
//}
}
return retVal;
}
protected override Task<bool> OnPopViewAsync(Page page, bool animated)
{
return base.OnPopViewAsync(page, animated);
}
}
public class OnClick : Java.Lang.Object, IOnClickListener
{
void IOnClickListener.OnClick(Android.Views.View v)
{
App.Current.MainPage.Navigation.PopAsync();
}
}
In the custom renderer, use the OnClickListener to capture the click on text.
when you are working with xamarin forms it is suggested make use of common components and make least use of custom renderer.
Now for your requirement you want to create custom navigation bar
so here is how you can do it.
Create BaseContent Page
Create a Control Template inside your base page your can follow this link
Inside your control template using a grid view place your label with text binding (Back),also your can place a label in center to show title of page again u can make use of template binding which u would come to know when u go through the link
Now inherit your main page with your basecontentpage page
add your control template inside your main page
turn off your navigation bar of your main page
and you are done, this would give u more power to add more things like image or toolbar in your navbar
also to dynamically handle your back button u can check the count from navigationstack if its 0 u can show Humburger Icon or if its more than 0 u can show your label using IsVisible True/False

How to disable button in MaterialAlertDialogBuilder

How can I disable button in MaterialAlertDialogBuilder?
I want to make similar functionality like in this screenshot:
enter image description here
I wrote the following code (dialog contains EditText where user should input his favorite food name).
final MaterialAlertDialogBuilder dialogEnterDishName = new MaterialAlertDialogBuilder(context);
//...
final EditText editTextEnterDishName = new EditText(context);
dialogEnterDishName.setView(editTextEnterDishName);
dialogEnterDishName.setPositiveButton(getString(R.string.dialog_enter_dish_name_positive_button), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
if (!editTextEnterDishName.getText().toString().equals(""))
//...
else {
//TODO Make posititve button disabled until the user enters any character
}
}
});
//...
dialogEnterDishName.show();
}
I already knew, that class AlertDialog (MaterialAlertDialogBuilder extends AlertDialog.Builder) have a method public Button getButton(int whichButton), but I can't use it in MaterialAlertDialogBuilder.
Please, help!
Make sure that you are calling getButton() function after you inflate your AlertDialog (through .show() call). If you are doing it other way around there is no button to get.
In order to enable button back you can use TextWatcher. More details here: Android TextWatcher.afterTextChanged vs TextWatcher.onTextChanged
val customLayout = LayoutInflater.from(context).inflate(R.layout.your_alert_dialog, null, false)
val dialog = MaterialAlertDialogBuilder(context)
.setTitle("Provide name")
.setView(customLayout)
.setNeutralButton("Cancel") { dialog, _ -> dialog.dismiss() }
.setPositiveButton("Confirm") { _, _ -> }
.create()
dialog.show()
dialog.getButton(AlertDialog.BUTTON_POSITIVE).isEnabled = false

How to use PKPaymentButton in Xamarin.Forms

I have a page in Xamarin.Forms in which I have to show PKPaymentButton by using the same PKPaymentButton class which is a child of UIButton in PassKit.
I have written a custom ButtonRenderer and trying to convert the button into PKPayment button.
I got so far that in custom renderer we can change the appearance of a button but can we use something like creating a new button instance in my case PKPaymentButton and replace it with Button.
UPDATE-
I have achieved this by-
public class ApplePayButtonRenderer : Xamarin.Forms.Platform.iOS.ButtonRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
{
base.OnElementChanged(e);
if (e.OldElement == null)
{
var button = new PKPaymentButton(PKPaymentButtonType.Buy, PKPaymentButtonStyle.Black);
SetNativeControl(button);
}
}
}
Now I am trying to get its click into Xamarin.Forms
You could use Messaging Center to send message when you click the payment Button.
in Custom Renderer
var button = new PKPaymentButton(PKPaymentButtonType.Buy, PKPaymentButtonStyle.Black);
button.TouchUpInside += Button_TouchUpInside;
SetNativeControl(button);
private void Button_TouchUpInside(object sender, EventArgs e)
{
MessagingCenter.Send<Object>(this,"PayButtonClick");
}
in Forms
Add the following code to the constructor of the ContentPage which contains the Payment Button
public xxxPage()
{
InitializeComponent();
MessagingCenter.Subscribe<Object>(this, "PayButtonClick",(args)=> {
//do some thing you want
});
}

How to keep button enabled after closing the form

I'm trying to make a system and I'm having trouble with buttons being disabled.
I have a function that makes the button on a another form enable the button on the main form but whenever I get back to the main form the button becomes disabled again.
How do I keep this permanent even after closing the program? Can I save it in a database to keep its function enabled even if its default is disabled?
Here's the picture of what it looks like:
Thanks for the help.
Take two buttons - "button1" and "button2" on "MainForm" Form.
Set property Enabled=false for "button1"
MainForm.cs
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
Form2 oFrm2 = new Form2();
oFrm2.evtFrm += new ShowFrm(oFrm2_evtFrm);
oFrm2.Show();
}
void oFrm2_evtFrm()
{
button1.Enabled = true;
}
}
Take one button - "button1" on "Form2" Form.
Form2.cs
public delegate void ShowFrm();
public partial class Form2 : Form
{
public event ShowFrm evtFrm;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (evtFrm != null)
{
evtFrm();
}
}
}
MainForm will display first.
Click on "button2" to display "Form2".
On "Form2", click on "button1" to make enable "button1" of "MainForm"
If you want to make "button1" enable permanent, you have to store value - "button1" is enable or disable.

Resources