Scroll to Top of Editor Control - c#

I would like to scroll to the first character of the editor control. Currently when new data is imported into the control, the control scrolls to the bottom which isn't the behavior I want. I don't see a clear way to accomplish this.
This needs to be set at load only as the user needs the ability to type without the cursor jumping to the begging during typing.
The following works for me which I've modified from the answer below. This is set in the Custom Rendered for Android:
protected override void OnVisibilityChanged(global::Android.Views.View changedView, [GeneratedEnum] ViewStates visibility)
{
base.OnVisibilityChanged(changedView, visibility);
Control.SetSelection(0);
}

You can custom a CustomEditorRenderer in native solution to achieve that .
Android :
[assembly: ExportRenderer(typeof(Editor), typeof(CustomEditorRenderer))]
namespace AppEntryTest.Droid
{
class CustomEditorRenderer : EditorRenderer
{
public CustomEditorRenderer(Context context) : base(context)
{
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
//set position from 0
Control.SetSelection(0);
}
}
The effect :
iOS :
[assembly: ExportRenderer(typeof(Editor), typeof(CustomEditorRenderer))]
namespace AppEntryTest.iOS
{
class CustomEditorRenderer : EditorRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Editor> e)
{
base.OnElementChanged(e);
Control.BecomeFirstResponder();
NSRange range;
range.Location = 0;
range.Length = 0;
Control.SelectedRange = range;
}
}
The effect :
=============================Update=============================
First , create renderer class in each platform :
Uesd in Xaml as follow :
<Editor Text="{Binding EditorValue}" />
In addition , also can create a custom Editor in Forms :
public class MyEditor : Editor
{
}
Used in Xaml : <local:MyEditor Text="{Binding EditorValue}" />
In rederer class , assembly also need to modify to use the custom Editor .
[assembly: ExportRenderer(typeof(MyEditor), typeof(CustomEditorRenderer))]

Related

How can I overlap bottom navigation with content in xamarin shell tabbar

[assembly: ExportRenderer(typeof(AppShell), typeof(CustomShellRenderer))]
namespace App4.Droid
{
public class CustomShellRenderer : ShellRenderer
{
public CustomShellRenderer(Context context) : base(context)
{
}
protected override IShellBottomNavViewAppearanceTracker CreateBottomNavViewAppearanceTracker(ShellItem shellItem)
{
return new CustomShellBottomNavViewAppearanceTracker();
}
}
public class CustomShellBottomNavViewAppearanceTracker : IShellBottomNavViewAppearanceTracker
{
public void Dispose()
{
}
public void ResetAppearance(BottomNavigationView bottomView)
{
}
public void SetAppearance(BottomNavigationView bottomView, IShellAppearanceElement appearance)
{
(bottomView.Parent as LinearLayout)?.SetBackgroundColor(Color.Transparent.ToAndroid());
bottomView.SetBackgroundColor(Color.Transparent.ToAndroid());
bottomView.LabelVisibilityMode = LabelVisibilityMode.LabelVisibilityUnlabeled;
}
}
}
shell customrenderer in android
I want to overlap the content part and bottom navigation part
I know how to implement purely XAML-only without shell and CustomRender, but I don't want to. Because my project is highly dependent on Shell
ex)
https://github.com/naweed/MauiPlanets
above app is overlap bottom navigation and contentpage
in my case remove tabbar background and remain icons
Simply put, I would like to lower the height of the content page overlapping with the bottom navigation.
Full screen should be able to meet your need, if I understand correctly.
Add this to your MainActivity.cs:
protected override void OnCreate(Bundle savedInstanceState)
{
...
IWindowInsetsController wicController = Window.InsetsController;
Window.SetDecorFitsSystemWindows(false);
Window.SetFlags(WindowManagerFlags.Fullscreen, WindowManagerFlags.Fullscreen);
if (wicController != null)
{
wicController.Hide(WindowInsets.Type.NavigationBars());
}
}

Xamarin FOrms iOS: ImageRenderer CreateNativeControl error

I am trying to implement a custom ImageRenderer in iOS subclassing the native UIImageView, but I am having some problems with CreateNativeControl.
In the older Xamarin.Forms version (like 4.2) the custom native class that I initialized with protected override UIImageView CreateNativeControl() { return new NativeImage(); } looks like it never get called (the message I log in the constructor is not shown). The Custom Renderer is correctly initialized (the right message is logged).
In the latest stable version (like 4.4) in overriding of CreateNativeControl the return type it is said that has to be a FormsUIImageView, never heard of it, anyway I also tried to subclass that but same problem as before, it seems it never get called as the constructor message is not logged. The Custom Renderer is correctly initialized (the right message is logged).
Here the code I used:
public class IOSImageView : ImageRenderer
{
public IOSImageView()
{
}
protected override void OnElementChanged(ElementChangedEventArgs<Image> e)
{
base.OnElementChanged(e);
if(Control == null)
{
return;
}
Console.WriteLine("PIPPO created from Custom Renderer"); //this message is correctly logged
}
protected override UIImageView CreateNativeControl() //FormsUIImageView in XF 4.4
{
return new NativeImage();
}
}
public class NativeImage : UIImageView //FormsUIImageView in XF 4.4
{
public NativeImage() : base()
{
Console.WriteLine("PIPPO created from native IOS"); //this message is NOT logged
}
public override void TouchesBegan(NSSet touches, UIEvent evt)
{
base.TouchesBegan(touches, evt);
Console.WriteLine("PIPPO touched"); //this (of course because no NativeImage is shown and there is no image to touch) is NOT logged
}
}
FormsUIImageView is new after XF 4.4 which you can check Xamarin.Forms release notes
In your case , you seems want to set the Image Renderer as your custom ImageView, right?
You should invoked SetNativeControl()
protected override void OnElementChanged(ElementChangedEventArgs<Image> e)
{
base.OnElementChanged(e);
if(Control!=null)
{
SetNativeControl(new NativeImage());
}
}
public class NativeImage : FormsUIImageView
{
public NativeImage() : base()
{
this.UserInteractionEnabled = true;
}
public override void TouchesBegan(NSSet touches, UIEvent evt)
{
base.TouchesBegan(touches, evt);
Console.WriteLine("PIPPO touched"); //this (of course because no NativeImage is shown and there is no image to touch) is NOT logged
}
}

UserControl extending ScrollableControl - disable container functinality

I'm building custom control by extending ScrollableControl.
Problem is that my custom control acts as container - I can drag controls into it:
My question is how can I disable container functionality in class that extends ScrollableControl
Below are two test controls, one extends Control, second ScrollableControl
public class ControlBasedControl : Control
{
protected override Size DefaultSize
{
get { return new Size(100, 100); }
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.FillRectangle(Brushes.LightCoral, ClientRectangle);
}
}
public class ScrollableControlBasedControl : ScrollableControl
{
public ScrollableControlBasedControl()
{
AutoScrollMinSize = new Size(200, 200);
}
protected override Size DefaultSize
{
get { return new Size(100, 100); }
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.FillRectangle(Brushes.LawnGreen, ClientRectangle);
}
}
You get "acts-like-a-container" behavior at design time from the [Designer] attribute. Copy-pasting from the Reference Source:
[
ComVisible(true),
ClassInterface(ClassInterfaceType.AutoDispatch),
Designer("System.Windows.Forms.Design.ScrollableControlDesigner, " + AssemblyRef.SystemDesign)
]
public class ScrollableControl : Control, IArrangedElement {
// etc...
}
It is ScrollableControlDesigner that gets the job done. Doesn't do much by itself, but derived from ParentControlDesigner, the designer that permits a control to act as a parent for child controls and gives it container-like behavior at design time.
Fix is easy, you just have to use your own [Designer] attribute to select another designer. Add a reference to System.Design and make it look like this:
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms.Design; // Add reference to System.Design
[Designer(typeof(ControlDesigner))]
public class ScrollableControlBasedControl : ScrollableControl {
// etc...
}
There is probably more than one way to accomplish this, but here is what I would do...
First create a read-only version of ControlCollection
public class ReadOnlyControlCollection : Control.ControlCollection
{
public ReadOnlyControlCollection(Control owner)
: base(owner)
{
}
public override bool IsReadOnly
{
get { return true; }
}
public override void Add(Control control)
{
throw new ArgumentException("control");
}
}
Then make your ScrollableControlBasedControl create an instance of ReadOnlyControlCollection in stead of the default ControlCollection
public class ScrollableControlBasedControl : ScrollableControl
{
protected override Control.ControlCollection CreateControlsInstance()
{
return new ReadOnlyControlCollection(this);
}
// The rest of your class goes here...
}
I use Visual Studio 2010 and when I drop a control on an ScrollableControlBasedControl the control is magically moved back to where it came from, as if the action was cancelled.

Xaml - How to bind with classes & renderers

I'm having a small issue modding the code below. It works fine except that I have to set the typeface permanently in the nIcon class. I want to use it for different typefaces from my xaml file.
In the example code below, I want the typeface to change from the default "FontAwesome" to the "Ionicons" as stated in the xaml file.
Since Xaml does not accept parameters when creating the class, I have no idea how to set it.
Any ideas?
Xaml Code:
<controls:nIcon
Text="\nf011"
FontFamily="Ionicons"/>
Shared code:
public class nIcon : Label
{
public const string Typeface = "FontAwesome";
public nIcon()
{
\\ FontFamily = Typeface;
}
}
Android Renderer:
public class IcDroidRenderer : LabelRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
{
base.OnElementChanged(e);
if (e.OldElement == null)
{
Control.Typeface = Typeface.CreateFromAsset(Forms.Context.Assets, nIcon.Typeface + ".ttf");
}
}
}

Change settings on all Windows Forms forms

I have changed my app.config file to allow the user to change the color scheme of the program. I can figure out how to change the background color of the form they are on where they change these settings:
Color colBackColor = Properties.Settings.Default.basicBackground;
this.BackColor = colBackColor;
But how can I change all of my forms background color? It's like I still want to pass all my forms to a function. I already asked that question and someone told me to use the app.config file. Now that I have done that, am I using it wrong?
It's simply that you need a base form from which all your forms in your project have to inherit:
public class FormBase : Form {
protected override void OnLoad(EventArgs e){
Color colBackColor = Properties.Settings.Default.basicBackground;
BackColor = colBackColor;
}
}
//Then all other forms have to inherit from that FormBase instead of the standard Form
public class Form1 : FormBase {
//...
}
public class Form2 : FormBase {
//...
}
UPDATE
public interface INotifyChangeStyle {
void ChangeStyle();
}
public class FormBase : Form, INotifyChangeStyle {
protected override void OnLoad(EventArgs e){
ChangeStyle();
}
public void ChangeStyle(){
//Perform style changing here
Color colBackColor = Properties.Settings.Default.basicBackground;
BackColor = colBackColor;
//--------
foreach(var c in Controls.OfType<INotifyChangeStyle>()){
c.ChangeStyle();
}
}
}
public class MyButton : Button, INotifyChangeStyle {
public void ChangeStyle(){
//Perform style changing here
//....
//--------
foreach(var c in Controls.OfType<INotifyChangeStyle>()){
c.ChangeStyle();
}
}
}
//... the same for other control classes

Categories