I need to create custom control for CustomCollectionViewRenderer in Xamarin Forms as I need to show collection of images like Pinterest app in Xamarin forms. But OnElementChanged is not there to override CollectionViewRenderer. Am I doing something wrong here?
I'm using
Xamarin.Forms NuGet Package version as 4.0.2.70
Visual Studio Community 2019 for Mac Version 8.3.4 (build 8)
NuGet Version: 5.3.0.6192
Xamarin.iOS Version: 13.4.0.2 (Visual Studio Community)
Xamarin.Android Version: 10.0.3.0 (Visual Studio Community)
[assembly: ExportRenderer(typeof(CollectionView), typeof(CustomCollectionViewRenderer))]
namespace MyProj.Droid
{
public class CustomCollectionViewRenderer : CollectionViewRenderer
{
public CustomCollectionViewRenderer(Context context) : base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<ItemsView> elementChangedEvent)
{
base.OnElementChanged(elementChangedEvent);
if (elementChangedEvent.NewElement != null)
{
StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.Vertical);
SetLayoutManager(manager);
}
}
}
}
I just Did the same in my VS19 for MAC with the following configuration and everything worked
using Android.Content;
using Android.Support.V7.Widget;
using Test.Droid.PlatformRenderer;
using System.ComponentModel;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
[assembly: ExportRenderer(typeof(CollectionView), typeof(CustomCollectionViewRenderer))]
namespace Test.Droid.PlatformRenderer
{
public class CustomCollectionViewRenderer : CollectionViewRenderer
{
public CustomCollectionViewRenderer(Context context) : base(context)
{
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs changedProperty)
{
base.OnElementPropertyChanged(sender, changedProperty);
}
protected override void OnElementChanged(ElementChangedEventArgs<ItemsView> elementChangedEvent)
{
base.OnElementChanged(elementChangedEvent);
if (elementChangedEvent.NewElement != null)
{
StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.Vertical);
SetLayoutManager(manager);
}
}
}
}
VS config:
Xamarin.Forms NuGet Package version 4.5.0.356
Visual Studio Professional 2019 for Mac Version 8.3.4 (build 8)
NuGet Version: 5.3.0.6192
Xamarin.iOS Version: 13.4.0.2
Xamarin.Android Version: 10.0.3.0
So my guess is you need the latest XF!
Related
few months ago I had a custom Xamarin.Android renderer for a webview based on the sample code in
https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/custom-renderer/hybridwebview
Javascript code was perfectly invoking my C# code however recently after the latest updates, the WebView control is no longer able to invoke the C# action,
(to be more precise, if I am targeting Android 9.0 (API level 28) or higher)
using API level 27 still works fine
after more investigation, I figured out that the compiler gives a warning on [JavascriptInterface] is being obsolete!
https://learn.microsoft.com/en-us/dotnet/api/android.webkit.javascriptinterface?view=xamarin-android-sdk-9
and they advised to use the (IJavascriptInterface) instead
here is the code to be reviewed
[JavascriptInterface]
[Export ("invokeAction")]
public void InvokeAction (string data)
{
HybridWebViewRenderer hybridRenderer;
if (hybridWebViewRenderer != null && hybridWebViewRenderer.TryGetTarget (out hybridRenderer))
{
hybridRenderer.Element.InvokeAction (data);
}
}
Does anyone know how to implement this properly to fix that and get Javascript to invoke my C# code again.
it still works in my Xamarin.Android project with [JavascriptInterface]
this is part of my sample :
var webview = FindViewById<WebView>(Resource.Id.webView1);
WebSettings settings = webview.Settings;
settings.JavaScriptEnabled = true;
// load the javascript interface method to call the foreground method
webView.AddJavascriptInterface(new MyJSInterface(this), "CSharp");
webview.SetWebViewClient(new WebViewClient());
MyJSInterface class :
class MyJSInterface : Java.Lang.Object
{
Context context;
public MyJSInterface (Context context)
{
this.context = context;
}
[JavascriptInterface]
[Export]
public void ShowToast (string msg)
{
Toast.MakeText(context, msg, ToastLength.Short).Show();
}
}
and in html :
<button type="button" onClick="CSharp.ShowToast ('Call C#')">Call C#</button>
you could refer to https://stackoverflow.com/a/54069075/10768653
I have a big question,
The next code is in my test app,
namespace displayalertetst
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
protected override void OnAppearing()
{
base.OnAppearing();
a.Text = "asi";
DisplayAlert("hola", "A", "as");
}
}
}
The problem is, in Andriod, de Alert does not show, but in ios works fine ... why?
I already tried
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
protected override void OnAppearing()
{
base.OnAppearing();
a.Text = "asi";
Device.BeginInvokeOnMainThread(() =>
{
DisplayAlert("hola", "A", "as");
});
}
}
And got the same behavior.
I am using visual studio for mac 8.1.4
and i am using xamarin forms 4.1
Thanks!
You can update Visual Studio 2019 for Mac to the latest version 8.3.5 to have a try again , it is released on October 28, 2019 .
And now Xamarin Forms latest version is 4.3 , you need to keep them with all the latest .
I'm using MvvmCross and Xamarin Support libraries in my project and, after I updated MvvmCross from version 4.2.0 to 4.4.0 and Xamarin Support Packages from version 23.3.0 to 25.1.1, I can't execute the project. The app always crash in SplashScreen with error: Default constructor not found for type MvvmCross.Platform.IoC.MvxPropertyInjector. The error occurs in debug mode and release mode.
My SplashScreen:
[Activity(Icon = "#drawable/ic_launcher",
Theme = "#style/InflorTheme.Splash",
NoHistory = true,
MainLauncher = true,
ScreenOrientation = ScreenOrientation.Portrait)]
public class SplashScreen : MvxSplashScreenActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
}
}
Add the following to your LinkerPleaseInclude file:
public void Include(MvvmCross.Platform.IoC.MvxPropertyInjector injector)
{
injector = new MvvmCross.Platform.IoC.MvxPropertyInjector();
}
You might need other hints for the linker not to not throw away code that is only invoked through Reflection.
I need to display the app's build version in my settings page.
So how can i get the versionCode and versionName from AndroidManifest.xml file programmatically.
Or
Is there any way to get the build version programmatically in xamarin.forms.
For version name:
Application.Context.ApplicationContext.PackageManager.GetPackageInfo(Application.Context.ApplicationContext.PackageName, 0).VersionName
For version code:
Application.Context.ApplicationContext.PackageManager.GetPackageInfo(Application.Context.ApplicationContext.PackageName, 0).VersionCode
With a Dependency service:
For iOS:
using Foundation;
[assembly: Xamarin.Forms.Dependency(typeof(Screen_iOS))]
namespace XXX.iOS
{
public class Screen_iOS : IScreen
{
public string Version
{
get
{
NSObject ver = NSBundle.MainBundle.InfoDictionary["CFBundleShortVersionString"];
return ver.ToString();
}
}
}
}
For Android:
using Xamarin.Forms;
[assembly: Dependency(typeof(ScreenAndroid))]
namespace XXX.Droid
{
class ScreenAndroid : Java.Lang.Object, IScreen
{
public string Version
{
get
{
var context = Forms.Context;
return context.PackageManager.GetPackageInfo(context.PackageName, 0).VersionName;
}
}
}
}
And the interface:
interface IScreen
{
string Version { get; }
}
In Xamarin.Essentials you can use the following:
Version version = AppInfo.Version;
From there you can get the build version.
If you really just want the build number you can also use the shortcut:
string buildNumber = AppInfo.BuildString;
In Xamarin.Android you could always try:
public double GetAppVersion()
{
var info = AppContext.PackageManager.GetPackageInfo(AppContext.PackageName, 0);
return info.VersionCode;
}
and then you will need to use dependency injection to call this method from your Xamarin.Forms project.
Error: The type or namespace name 'Selenium' could not be found (are you missing a using directive or an assembly reference?)
CODE Generated by the IDE
using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using NUnit.Framework;
using Selenium;
namespace SeleniumTests
{
[TestFixture]
public class Untitled
{
private ISelenium selenium;
private StringBuilder verificationErrors;
[SetUp]
public void SetupTest()
{
selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.ehow.com/");
selenium.Start();
verificationErrors = new StringBuilder();
}
[TearDown]
public void TeardownTest()
{
try
{
selenium.Stop();
}
catch (Exception)
{
// Ignore errors if unable to close the browser
}
Assert.AreEqual("", verificationErrors.ToString());
}
[Test]
public void TheUntitledTest()
{
selenium.Open("/tips_7490061.html");
selenium.Click("//a[contains(text(),'How to Conserve Water Usage')]");
selenium.WaitForPageToLoad("30000");
}
}
}
Have you added the Selenium DLL to the c# project?
I have a basic tutorial available here that should help you through getting started.