Support content effect (highlight, lift and hover) for mouse and trackpad on iPadOS 13.4 - xamarin.forms

I have implemented UIButton as custom renderer in Xamarin.Forms project. Is there a sample project to demonstrate how to implement content effects like highlight using UIPointerInteraction class? Specifically, is there an equivalent for this sample code in Xamarin? Integrating Pointer Interactions into Your iPad App

After doing some research and I did not find a sample project about UIPointerInteraction.
While I think you can use it in the latest Xamarin.iOS 13.6.0 as those classes has been added to Xamarin.iOS in this version:
new-type-uikituipointerregion
new-type-uikituipointerregionrequest
I wrote a simple example about how to initialize UIPointerInteraction :
public override void ViewDidLoad()
{
base.ViewDidLoad();
// Perform any additional setup after loading the view
pointerDelegate myDelegate = new pointerDelegate();
UIPointerInteraction interPointer = new UIPointerInteraction(myDelegate);
}
public class pointerDelegate : UIPointerInteractionDelegate {
public override void WillEnterRegion(UIPointerInteraction interaction, UIPointerRegion region, IUIPointerInteractionAnimating animator)
{
base.WillEnterRegion(interaction, region, animator);
}
public override void WillExitRegion(UIPointerInteraction interaction, UIPointerRegion region, IUIPointerInteractionAnimating animator)
{
base.WillExitRegion(interaction, region, animator);
}
}
Feel free to ask me if you have any difficulty when transferring swift/OC code from native iOS to C# in Xamarin.iOS.

Related

Is it possible to use Xamarin Forms Controls in a Uno project?

I am currently developing a cross-platform (Windows and Mac) app using Uno and WinUI 3. I need to implement a WebView in my project, but I'm having some trouble.
On Windows I can use the new WebView2 from WinUI without any problem, but being not supported by Uno I can't use it on Mac. On Mac I think I can't even use the WebView1 because it is not supported anymore in WinUI 3 (and so in Uno.WinUI, or am I wrong?).
So I thought to use the Xamarin.Forms WebView on Mac, but I don't know how to implement it. I thought to add a new project to the solution with just the WebView and call it from the Uno.Mac project, but it doesn't work.
May you give me any suggestions?
It is possible to use Uno Platform controls in Xamarin.Forms app (see docs). The other way may also be possible as there was a way to embed a Xamarin.Forms control in a native app, and Uno allows embedding native controls (see docs).
However, a more efficient solution would be to use the built-in WebView control that Uno Platform provides (unless it is missing some functionality you require - in which case please raise an issue on GitHub :-) ).
Alternatively, you could also directly embed the native macOS WKWebView (using the steps linked to above) and work with it directly.
Xamarin.Forms controls cannot be used individually in an Uno app, in the same way those cannot be used in a Xamarin.iOS or Xamarin.Android "classic" app.
You can, however, use the the Xamarin.Forms embedding technique described in this article. The base of the technique is to create a ContentPage, then add it as a native control.
Uno Platform supports adding native controls directly in the Visual Tree by assigning the native view to the Content property of a ContentControl using code-behind.
You can easily add Xamarin content into uno and vice versa.
Just try this :
public partial class XamarinContent : ContentControl
{
View _element;
public View Element
{
get => _element;
set
{
_element = value;
#if __ANDROID__
var renderer = RendererFactory.GetRenderer(value);
this.Content = renderer.View;
this.SizeChanged += (s, a) => {
_element.Layout(new Rect(0, 0, a.NewSize.Width, a.NewSize.Height));
};
#else
var converter = new ViewToRendererConverter();
var frameworkElement = converter.Convert(value, null, null, null);
this.Content = frameworkElement;
// this.SetValue(ContentControl.ContentProperty, frameworkElement) ;
#endif
}
}
}
and to convert from uno into Xamarin forms
[ContentProperty("Element")]
public class UnoContent : Xamarin.Forms.TemplatedView
{
public static readonly BindableProperty ContentProperty
= BindableProperty.Create("Element", typeof(Windows.UI.Xaml.FrameworkElement), typeof(View), null);
public Windows.UI.Xaml.FrameworkElement Element
{
get { return (Windows.UI.Xaml.FrameworkElement)GetValue(ContentProperty); }
set { SetValue(ContentProperty, value); base.ControlTemplate = new Xamarin.Forms.ControlTemplate(() => value.ToView()); }
}
}

Xamarin Forms: WebView loading time is very high

I am using WebView for loading websites in my project. It is loading websites but very slow. It takes around 10 to 15 seconds to load this site.
I try a solution from this thread. I have added android:hardwareAccelerated="true" on the manifest under application level, but no luck.
For iOS and Windows the solution is below: But I don't know how to create the custom render.
In iOS, try to use WKWebView instead of UIWebView.
For the UWP, use Windows.UI.Xaml.Controls.WebViewcontrol instead of using the built-in Xamarin WebView control.
Can I get the sample custom renderer for iOS and Windows-like above?
It is loading websites but very slow
The speed of loading website will up to lots of causes . For example, it will consume a lot of time if the web contains many remote css/js file . And it will also up to network performance .
The link that you provided loads slow even if on browser on my side . It contains lots of remote css if we check the source code.
For Android , We can create an custom renderer webview to accelerate it.
Set Render Priority to hight.
Enable the dom storeage.
When Loading the html, we can disable the image showing, when
loading finished, load the image by
Control.Settings.BlockNetworkImage.
Enable the Cache, if you load it at the nexttime, you can load it
quickly.
[assembly: ExportRenderer(typeof(Xamarin.Forms.WebView), typeof(MyWebviewRender))]
namespace MyWebviewDemo.Droid
{
public class MyWebviewRender : WebViewRenderer
{
public MyWebviewRender(Context context) : base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.WebView> e)
{
base.OnElementChanged(e);
Control.Settings.JavaScriptEnabled = true;
Control.Settings.SetAppCacheEnabled(true);
Control.Settings.CacheMode = Android.Webkit.CacheModes.Normal;
Control.Settings.SetRenderPriority(RenderPriority.High);
Control.Settings.DomStorageEnabled = true;
Control.Settings.BlockNetworkImage = true;
Control.SetWebChromeClient(new MyWebChromeClient());
}
}
internal class MyWebChromeClient : WebChromeClient
{
public override void OnProgressChanged(Android.Webkit.WebView view, int newProgress)
{
base.OnProgressChanged(view, newProgress);
if (newProgress == 100)
{
// webview load success
// // loadDialog.dismiss();
view.Settings.BlockNetworkImage=false;
}
else
{
// webview loading
// loadDialog.show();
}
}
}
}
In iOS, try to use WKWebView instead of UIWebView. For the UWP, use Windows.UI.Xaml.Controls.WebViewcontrol instead of using the built-in Xamarin WebView control.
In your case it will only have less improve even if using above solution . You could add an ActivityIndicator during the loading . If you can access the website , it would be better to download the css file to local and loading them in ContentPage .

How to execute a platform (Android) specific method within a ButtonOnClick() using a Xamarin.Forms custom renderer.

Introduction:
I am starting with code from:
https://github.com/xamarin/xamarin-forms-samples/tree/master/CustomRenderers/Entry/Android
to study custom renderers. I am doing this because there is code that only executes on the android platform. Lets call this "androidMethod()" and belongs in the Android codebase (not the shared library). I have noticed that most of the examples I have found have utilized customRenderers for making platform specific UI modifications (like the example in the link) but I intend to make no changes to the UI, rather I am trying to place a platform specific method in a Xamarin.Forms ButtonOnClick() method as the code below indicates.
The code below is similar to the code you will find in the MyEntryRenderer.cs from the link but you will see that it was modified to apply to a button instead of an entry.
MyButtonRenderer.cs:
using Xamarin.Forms.Platform.Android;
using Xamarin.Forms;
using CustomRenderer;
using CustomRenderer.Android;
using Android.Content;
[assembly: ExportRenderer(typeof(MyButton), typeof(MyButtonRenderer))]
namespace CustomRenderer.Android
{
class MyButtonRenderer : ButtonRenderer
{
private Button androidButton;
public MyButtonRenderer(Context context) : base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
{
base.OnElementChanged(e);
if (Control != null)
{
//I want to be able to do something like this:
ButtonOnClick(androidMethod());
}
}
}
}
How do I get androidMethod(); to execute in this context. The samples I find are limited so please try to limit your response to something that would be compatible with the example. Thankyou!
if you want to execute a platform specific method, I would use DepenencyService instead of a Custom Renderer

Manually Open custom inspector for serializable object

I have a window editor that holds nodes. I would like to open a custom inspector when one of these nodes is selected. The node class is a custom serializable class. Is this possible?.
It seems that custom inspectors can be created manually through the Editor.CreateEditor method but can't see how to let it appear docked like an usual inspector in the unity inspector window.
I would like to achieve the same behaviour that we have when we select a gameobject in sceneview that inmediately show properties for the object (Components, etc...) in the unity inspector.
Cheers
As I'm not sure what you're asking, here are multiple different solutions;
Selection
If you just want your nodes to become the focus of the hierarchy, then in your custom window's OnGUI method, use the code below;
[CustomEditor(typeof(NodeManager))]
public class NodeManager : EditorWindow
{
private static NodeManager window;
private Node[] m_nodes;
[MenuItem("Custom Windows/Node Inspector")]
public static void Init()
{
if(window == null)
window = GetWindow<NodeManager>("Node Manager", true, typeof(SceneView));
}
public void OnGUI()
{
m_nodes = GameObject.FindObjectsOfType<Node>();
foreach(Node node in m_nodes)
{
GUILayout.BeginHorizontal();
{
GUILayout.Label(node.name);
if (GUILayout.Button("Select"))
Selection.objects = new Object[] { node.gameObject };
}
GUILayout.EndHorizontal();
}
}
}
This adds a Button for each object in your custom window view, that will then select that object in the hierarchy.
Auto-Docking
I originally found the second response to this question, which goes into the details of parameters of the GetWindow method, and with this you can clearly see how to dock the window (I've converted it from JS to C# below).
(I looked fairly extensively in UnityEditor and UnityEditorInternal namespaces but couldn't find the Hierarchy or the Inspector).
GetWindow<T>(string title, bool focus, params System.Type[] desiredDockNextTo)
Which we can write for this example as;
EditorWindow.GetWindow<NodeInspector>("Node Test", true, typeof(SceneView));
This example docks the windows next to the SceneView window. This functionality can be combined with a custom inspector's OnInspectorGUI method, to automatically launch the custom node window, if it's not already open.
[CustomEditor(typeof(Node))]
public class NodeInspector : Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
NodeManager.Init();
}
}
Sorry if this isn't what you are looking for, if it's not then please give more details and I will amend my answer to better suit the question.
Hope this helped.
Has a possibility, you can make a custom ScriptableObject and Custom Editor for It, and when open the node inspector, just find a instance of the scriptable object and use Selection.selectedObject = scriptableObject, and in the custom editor, make a static field called 'editor' to draw inside.
It will work.

Unity 4.6 hide / unhide button using C# script

I've been working on a project in Unity 4.6 where it is necessary to hide and unhide buttons using C# script. Does anyone have an example on how to do this?
When a gameObject or component is set to inactive using SetActive(false) you cannot use any of the "Find" or "GetComponent" type functions. The only way you can reactivate the gameobject or component is to have a reference to it.
In your project could you store the keys in a list or array on startup? you could then iterate through the collection and reactive the disabled keys as needed.
If you want to show/hide a GUI object, and don't want to disable it just
Add CanvasGroup to the object
Set its alpha to 1 or 0 for show/hide CanvasGroup: GetComponent<CanvasGroup>().alpha = 1;
using UnityEngine;
using UnityEngine.UI;
public class Showandhide : MonoBehaviour {
public GameObject button;//assign the button object here
private bool isShowing;
void Update()
{
if (Input.GetKeyDown("escape"))
{
isShowing = !isShowing;
button.SetActive(isShowing);
}
}
}

Resources