How to get UIButton from a Xamarin.Froms.Button? - xamarin.forms

I'm using Xamarin.Forms mixed with some native Xamarin code. Now I'd like to get a UIButton instance from a Xamarin.Forms.Button. How can I do that?
In the Clicked event handler, I can see
void Share_Clicked(object sender, System.EventArgs e)
{
var button = sender as Xamarin.Forms.Button;
if(button.EffectControlProvider != null)
{
}
}
I can see EffectControlProvider.Controller is actually of UIButton type but I can't convert to it.
Is there any funciton like ToNative to get the UIButton?

Refer to Referring to Native Views from Code.
#if __IOS__
var wrapper = (Xamarin.Forms.Platform.iOS.NativeViewWrapper)contentViewButtonParent.Content;
var button = (UIKit.UIButton)wrapper.NativeView;
button.SetTitle("Scale and Rotate Text", UIKit.UIControlState.Normal);
button.SetTitleColor(UIKit.UIColor.Black, UIKit.UIControlState.Normal);
#endif
Notice:this code is only used in Shared Access Project.

Related

Adding children to StackLayout inside for loop causes this error "Layout cycle detected. Layout could not complete." in Xamarin Forms UWP

foreach (var equipment in EquipmentCollection)
{
var control = new EquipmentControl(equipment);
MyStackLayout.Children.Add(control);
}
I am getting this error only in UWP but not in Android. Even in UWP if the list size is small it is working fine.
I have tried few work arounds but still I couldn't able to make it work.
Since there is no UpdateLayout() method for xamarin-forms controls so I tried to use custom renderer for stacklayout and raised a child added event and called UpdateLayout() inside it but still got the crash.
[assembly: ExportRenderer(typeof(CustomStackLayout), typeof(CustomStackLayoutRenderer))]
namespace FO4.UWP.Controls
{
public class CustomStackLayoutRenderer : ViewRenderer<StackLayout, StackPanel>
{
StackPanel stkPanel;
protected override void OnElementChanged(ElementChangedEventArgs<StackLayout> e)
{
base.OnElementChanged(e);
if (e.NewElement != null)
{
CustomStackLayout stkLayout = (CustomStackLayout)e.NewElement;
stkLayout.ChildAdded += StkLayout_ChildAdded;
}
if (e.OldElement != null)
{
CustomStackLayout stkLayout = (CustomStackLayout)e.OldElement;
stkLayout.ChildAdded -= StkLayout_ChildAdded;
}
}
private void StkLayout_ChildAdded(object sender, ElementEventArgs e)
{
this.UpdateLayout();
}
}
}
I develop in Xamarin Forms but I don't target UWP.
Here some suggestions that will maybe help you:
For this kind of need, it is more frequent to use DataBinding through BindableLayout.ItemTemplate and Bindable.ItemsSource like described here: https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/layouts/bindable-layouts#populate-a-bindable-layout-with-data.
If you don't want to use these mechanism, maybe you can try to use the BatchBegin() and BatchCompleted() method before and after your Loop https://learn.microsoft.com/en-us/dotnet/api/xamarin.forms.visualelement.batchbegin?view=xamarin-forms
The use of a renderer shoud not be required in my opinion...

changing mouse cursor in chromiumwebbrowser DragEnter event in WPF

we are using chromiumwebbrowser in WPf application, added DragEnter event handler to the chromiumwebbrowser and call come to us.
wanted to change the cursor,
private void Browser_DragEnter(object sender, DragEventArgs e)
{
var currCursor = Browser.Cursor;
if (e.Effects == DragDropEffects.Move)
{
//Mouse.OverrideCursor = Cursors.Hand;
// Mouse.SetCursor(Cursors.Hand);
Debug.Write("DragDropEffects.Move\n");
}
else
Mouse.SetCursor(Cursors.Hand);
Mouse.OverrideCursor = Cursors.Pen;
Browser.Cursor = Cursors.Pen;
//Mouse.Capture(Browse);
e.Handled = true;
}
nothing works, could some help would solve this issue

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
});
}

Get e parameter from a button that hasn't been clicked

Is there a way that I could access the e event arguments for a button that has not been clicked?
I need to delete multiple entries in a gridview by clicking a button and having it simulate clicking the delete button for each selected entry, but I can't use performClick, so I'm trying to call the actual method that deletes each one. However, that method requires an "e As System.Web.UI.WebControls.GridViewCommandEventArgs" parameter and I can't figure out how to get that.
You won't be able to access the EventArgs parameter.
I'd suggest you design your code like this:
public class MyClass
{
private ListView listView;
protected void OnClick(EventArgs e)
{
performAction();
}
private void performAction()
{
listView.deleteSelectedItems();
}
}
Don't implement functionality you are going to need somewhere else in delegates. Instead call this functionality inside the delegates' body. This way you can reuse performAction() somewhere else ..
Your problem calling delete button can be resolved if you add one check box in each row of datagrid and on click of button Delete you can perform delete operation for the checked rows in following manner
Protected void btnDelete_Click(object sender, EventArgs e)
{
for(int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox chkDelete = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("chkSelect");
if(chkDelete != null)
{
if(chkDelete.Checked)
{
strID = GridView1.Rows[i].Cells[1].Text;
ids.Add(strID); //ids can colletion of any type
}
}
}
}
Now send ids to any function to perform delete.

Add drop down menu on button click - windows 8

I want to display a drop down menu when a user clicks a button. Something like comboBox but instead of the comboBox its a button. How can I do it??
I solved it using PopupMenu. Here is the code for other's reference.
public static Rect GetElementRect(FrameworkElement element)
{
GeneralTransform buttonTransform = element.TransformToVisual(null);
Point point = buttonTransform.TransformPoint(new Point());
return new Rect(point, new Size(element.ActualWidth, element.ActualHeight));
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
var menu = new PopupMenu();
menu.Commands.Add(new UICommand("Label", (command) =>
{
//do work
}));
// We don't want to obscure content, so pass in a rectangle representing the sender of the context menu event.
// We registered command callbacks; no need to handle the menu completion event
var chosenCommand = await menu.ShowForSelectionAsync(GetElementRect((FrameworkElement)sender));
if (chosenCommand == null) // The command is null if no command was invoked.
{
}
}
Milan,
You'll need to create a custom control or a user control that combines a button and a popup. You could also just implement this in-place with a button and popup. I suggest you look at Callisto's Menu control and start from there to implement your dropdown menu:
Callisto controls (includes a Menu)

Resources