Image Button for Xamarin.MacOS - xamarin.forms

Is there an Image Button Renderer in Xamarin.MacOS?
I am using Xamarin.Forms and all my image buttons in the common code are not shown at all in Xamarin.MacOS. Is Image Button supported in Xamarin.MacOS?

Is there an Image Button Renderer in Xamarin.MacOS?
Unfortunately, after checking the source code of Xamarin.Forms.Platform.macOS, there is no ImageButtonRenderer there now. I guess the reason that there is no control is the similar with ImageButton. I'm not sure whehter designer will add this in the future. Whatever, now it not exists.
Is Image Button supported in Xamarin.MacOS?
From the above said, MacOS has no control like ImageButton of Forms. Therefore, if used ImageButton in Xamarin Froms, it will not show in Xamarin.MacOS.
However, there is a workaround. You could use Button in Forms, and add Image in ButtonRenderer in Xamarin.MacOS. Because NSButton has a Image property.
For example, the code of ButtonRenderer as follows:
[assembly: ExportRenderer (typeof(MyButton), typeof(MyButtonRenderer))]
namespace CustomRenderer.MacOS
{
public class MyButtonRenderer : ButtonRenderer
{
protected override void OnElementChanged (ElementChangedEventArgs<Button> e)
{
base.OnElementChanged (e);
if (Control != null) {
// do whatever you want to the UITextField here!
Control.Image= NSImage.ImageNamed("tree")
Control.ImageScaling = NSImageScale.AxesIndependently;
}
}
}
}
The image source (tree.png) is the same way with iOS to be added in Resource folder, and set Propertied : Build Action -> BundleResource.

Related

How to fix Menu for Nevigation pages in Master Detail page for all platform(android and uwp)

I have designed master detail page for my application.
Here I past screentshots of my application.
Homepage overview
I use data grid view to show my data.
Menu
but, I don't want to hide my Menu.I fix that in my application home screen for uwp app only.
So after fix that I want to show my data in datagridview for rest of the screen.
I want to unhide Menu.
Is that posiible using master detail page design.
How can I do it, Please any suggestion or code example.
advance thanks.
Customized screen what I want:-
CustomScreen
You can create a MasterDetailPageRenderer in UWP solution as follows:
[assembly: ExportRenderer(typeof(AppMasterDetailDemo.Views.MainPage), typeof(CustomMasterDetailRenderer))]
namespace AppMasterDetailDemo.UWP
{
public class CustomMasterDetailRenderer: MasterDetailPageRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<MasterDetailPage> e)
{
base.OnElementChanged(e);
if (Control != null)
{
Control.CollapsedPaneWidth = 0;
Control.CollapseStyle = Xamarin.Forms.PlatformConfiguration.WindowsSpecific.CollapseStyle.Partial;
Control.MasterToolbarVisibility = Windows.UI.Xaml.Visibility.Collapsed;
Control.DetailTitleVisibility = Windows.UI.Xaml.Visibility.Collapsed;
Control.MasterTitleVisibility = Windows.UI.Xaml.Visibility.Collapsed;
Control.ContentTogglePaneButtonVisibility =
Windows.UI.Xaml.Visibility.Collapsed;
}
}
}
}
Then in Xamarin Forms, set as follows can hide the Menu button.
NavigationPage.SetHasBackButton(this, false);
NavigationPage.SetHasNavigationBar(this, false);
Or modify the false to true, can show again.
The effect:

Interaction with parent control triggers RippleDrawable in Xamarin.Forns custom renderer

I have implemented a custom clickable label class in Xamarin.Forms along with a custom renderer, that adds a RippleDrawable as the controls Foreground. I am creating the RippleDrawable with the following code:
public static Drawable CreateRippleDrawable(Context context)
{
var typedValue = new TypedValue();
context.Theme.ResolveAttribute(Resource.Attribute.SelectableItemBackground, typedValue, true);
var rippleDrawable = context.Resources.GetDrawable(typedValue.ResourceId, context.Theme);
return rippleDrawable;
}
In my custom renderer I assign the drawable
this.Control.Foreground = DrawableHelper.CreateRippleDrawable(this.Context);
and update the ripple when the user touches the control
private void LinkLabelRenderer_Touch(object sender, TouchEventArgs e)
{
if (e.Event.Action == MotionEventActions.Down)
{
this.Pressed = true;
}
if (e.Event.Action == MotionEventActions.Cancel)
{
this.Pressed = false;
}
if (e.Event.Action == MotionEventActions.Up)
{
this.Ripple.SetHotspot(e.Event.GetX(), e.Event.GetY());
this.Pressed = false;
// raise the event of the Xamarin.Forms control
}
}
Now, whenever I click the control, the ripple will be shown, which is the expected behavior, but if I touch (tap or long-press) the parents of the control (e.g. the StackLayout, Grid or whatever layout contains the label, including their parent Layout, Page or View) the ripple animation will be triggered. Anyway, the event handler LinkLabelRenderer_Touch in not called in this case, only when the actual control is touched.
I can work around this behavior by adding an empty GestureRecognizer to the respective parent(s), but I really dislike this solution, because this is but a hack. And to make things worse it is a hack I'll always have to remember whenever I use the control.
How can I prevent the RippleDrawable being shown when the parent is touched?
Turned out I got things fundamentally wrong. Subscribing the Touch event is not the way to go. I had to make the control clickable and subscribe the Click event
this.Control.Clickable = true;
this.Click += LinkLabelRenderer_OnClick;
There is no need to handle all that RippleTouch stuff the way I did (via the Touch event) but could let android handle things for me.

Image tint Button Android Xamarin Forms

I try to create a CustomRenderer for Android inherit to ButtonRenderer with an Image. I want change color of Image programmatically like TintColor on iOS.
For example, my CustomRenderer iOS :
public class IconButtonRenderer : ButtonRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
{
base.OnElementChanged(e);
var button = e.NewElement;
}
public override void Draw(CoreGraphics.CGRect rect)
{
base.Draw(rect);
// Here I change color of Image in Red.
Control.ImageView.Image = Control.ImageView.Image.ImageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate);
Control.ImageView.TintColor = UIColor.Red;
}
}
On Android:
Control is an Android.Widget.Button and not have Image property but Xamarin.Forms.Button has property Image and use Android.Widget.Button for Android.
How I can access image with format Android for change color?
Image tint Button Android Xamarin Forms
Orignal Image
Solution 1 :
As #Mathias Kirkegaard said, in your ButtonRenderer you could use SetColorFilter to change color :
Control.Background.SetColorFilter(Android.Graphics.Color.Blue, PorterDuff.Mode.SrcIn);
It did work in native Android, but has some problem in Xamarin.Forms, it only changes the Button background color like this: effect. Hope someone could find a solution, and if I find a solution to solve this issue I will come back and update my answer.
Solution 2 :
You could use the plugin : Tinted Image and add a click event to implement the same feature.
Install the Plugin.CrossPlatformTintedImage nuget package
Initialize the renderer in your iOS, Android, and UWP projects as shown below:
Xamarin.Forms.Init();
TintedImageRenderer.Init();
In Xaml:
<ContentPage
...
xmlns:controls="clr-namespace:Plugin.CrossPlatformTintedImage.Abstractions;assembly=Plugin.CrossPlatformTintedImage.Abstractions"
...>
...
<controls:TintedImage x:Name="buttonImage" TintColor="Blue" Source="redDis.png"/>
...
</ContentPage>
In code, Adding a Tap Gesture Gesture Recognizer :
var tapGestureRecognizer = new TapGestureRecognizer();
tapGestureRecognizer.Tapped += (s, e) =>
{
// handle the tap
};
buttonImage.GestureRecognizers.Add(tapGestureRecognizer);
Effect after adding a click event on Tinted Image
Solution 3 :
If you need to use ImageButton, you could refer to this solution. I didn't test it but you could use the ImageButton from XLab.
XLab ImageButton Source code
XLab ImageButtonRenderer Source code
On a button you can use "SetColorFilter(color)"
You might be able to get the color using the ContextCompat:
var color = new Color(ContextCompat.GetColor(Context, Resource.Color.Green));
and then apply it:
yourButton.SetColorFilter(color)
Not sure whether you need to use an ImageButton rather than a simple Button.

Xamarin Forms iOS renderer Dimensions

I’m trying to create a “generic” renderer for iOS and I have trouble figuring out the dimensions of the renderer.
The class of the renderer in the PCL is very simple and the only “peculiar” is that inherits from a View since I want to have it as a generic renderer
public class ExampleView : View { }
In the XAML, it is also very typical
<renderers:ExampleView />
The definition of the renderer in the iOS project is the following
public class ExampleRenderer : ViewRenderer<ExampleView, UIView>
{
protected override void OnElementChanged(ElementChangedEventArgs<ExampleView> e)
{
base.OnElementChanged(e);
if (Control == null)
{
SetNativeControl(new UIView(new CGRect(0, 0, 0, 300)));
}
}
}
The interesting part here is that the class inherits from ViewRenderer<ExampleView, UIView>
It seems that although I specify that the width of the render to be 0, the actual renderer takes the entire width of the page.
In fact whatever value I put as width, will be disregarded, and the renderer will occupy the entire page.
This is not bad, it is unintentional and this is what bother me. (or Am I doing something wrong?)
So the question here is how can find in my custom render, the width that occupies?
It is not the renderer, but the native control that is taking up the space. The renderer is doing it's job i.e. keep the native control in sync with forms control - which includes layout parse requests.
To control the size constraints I would recommend using the properties WidthRequest or HeightRequest on forms Element to do that. It will automatically trickle down to the native Control.Frame through the renderer.
EDIT-1
If you want to update your native controls (or subviews) based on current bounds you can use OnElementPropertyChanged to track the changes. There can many reasons that can cause a size-update - like device-orientation change, parent layout, first layout pass etc.
public class ExampleRenderer : ViewRenderer<ExampleView, UIView>
{
protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == VisualElement.WidthProperty.PropertyName ||
e.PropertyName == VisualElement.HeightProperty.PropertyName)
{
if(Element != null && Element.Bounds.Height > 0 && Element.Bounds.Width > 0)
{
//update size on native control's subviews
// or use SetNeedsDisplay() to re-draw
//Note: force update to Control's size is not recommended (as it is recommended it be same as forms element)
//Instead add a subview that you can update for size here
}
}
}

Button in edit view in Unity

I'd like to know if there is a way to add a button in scene view when in edit mode in unity.
I'd like to choose an option from the gameobject menu and then deactivate it when the button on scene is clicked (before pressing run)
Is this possible please?
I think you are asking about Unity3D game engine. Well, by means of Editor scripts you can add a button in Editor mode. For example:
using UnityEngine;
using UnityEditor;
using System.Collections;
public class LevelChunksAnalyser : MonoBehaviour {
[MenuItem("Level Chunks/Update Metadata")]
public static void UpdateLevelChunksMetadata()
{
Debug.Log("Yo");
}
}

Resources