Xamarin Android: Native image scaling, images are pixelated

If you use Android’s native image scaling, from ldpi, mdpi, hdpi, to xxxhdpi, and have all the image resource in the right directory of your Xamarin.Android project folder, and find the pictures displaying pixelated: Make sure, you are correctly set the Build action for each images to AndroidResource. When adding an existing item to the resources folder from Windows version of VisualStudio’s browse dialog, the Build action will be set to BundleResource, and won’t appear in the Resources.designer.cs file, so it will be unavailable to use from code. If the picture is displaying, but it is pixelated, you may check the higher DPI version image files, have the correct buid action

This content has 3 years. Some of the information in this post may be out of date or no longer work. Please, read this page keeping its age in your mind.

Android: Application auto backup ki/be kapcsolása (Xamarin is)

Android 6.0 óta (API verzió v23) bevezették az Androidba, hogy az alkalmazás fájljairól biztonsági mentést készít az operációs rendszer a felhasználó Google drive-jára. A backupok mérete maximum 25MB lehet, és az Android a JobScheduler API-jával készülnek 24 órás időintervallumban. További információk itt: https://developer.android.com/guide/topics/data/autobackup

Az érintett fájlok:

  • Shared preferences files
  • Files in the directory returned by getFilesDir()
  • Files in the directory returned by getDatabasePath(String)
  • Files in directories created with getDir(String, int)
  • Files on external storage in the directory returned by getExternalFilesDir(String)

Mivel a Xamarin Essentials a SharedPreferencesbe teszi a Preferences statikus osztály által tárolt adatokat, ezért azok is mentésre kerülnek.

A be/ki kapcsoláshoz az AndroidManifest.xml-ben a következő propertyre van szükség az application tagben:

android:allowBackup="true"
This content has 3 years. Some of the information in this post may be out of date or no longer work. Please, read this page keeping its age in your mind.

Xamarin Forms / Android: Backspace detektálása az Entrykben

Ahhoz, hogy a szoftveres / hardveres billentyűzeten ütött vissza gomb érzékelését detektáljuk a billentyűzeten, szükségünk lesz egy CustomRenderer-re, ahhoz pedig egy Entry száramaztatáshoz a közös kódban:

public class CustomEntry: Entry
{
    public delegate void BackspaceEventHandler(object sender, EventArgs e);

    public event BackspaceEventHandler OnBackspace;

    public CustomEntry() { }

    public void OnBackspacePressed() 
    {
        if (OnBackspace != null)
        {
            OnBackspace(null, null);
        }
    }
}

Két CustomRenderer megoldás is van Androidon. Az egyik, amelyik magát a Renderert egy InputFilter implementációvá teszi, a másik, amely egy csak egy metódust overrideol. A különbség köztük, hogy a DispatchKeyEvent override az üres entry esetén is továbbítja az eventet, míg az inputfiilter csak akkor érzékeli a visszatörlés gombnyomást, ha volt már szöveg benne.

DispatchKeyEvent override megoldás

public class CustomEntryRenderer: EntryRenderer
 {
    public override bool DispatchKeyEvent(KeyEvent e)
    {
        if (e.Action == KeyEventActions.Down)
        {
            if (e.KeyCode == Keycode.Del)
            {
                if (string.IsNullOrWhiteSpace(Control.Text))
                {
                    var entry = (PasswordBox)Element;
                    entry.OnBackspacePressed();
                }
            }
        }
        return base.DispatchKeyEvent(e);
    }

    protected override void 
    OnElementChanged(Xamarin.Forms.Platform.Android.ElementChangedEventArgs<Entry> e)
    {
        base.OnElementChanged(e);
    }
}

InputFilter implementáció

[assembly: ExportRenderer(typeof(CustomEntry), typeof(CustomEntryRenderer))]
namespace App.Droid.Renderers
{
    public class CustomEntryRenderer: EntryRenderer, Android.Text.IInputFilter
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged(e);

            if (Control == null) 
            {
                return;
            }

            Control.SetFilters(new IInputFilter[] { this });

        }

        ICharSequence IInputFilter.FilterFormatted(ICharSequence source, int start, int end, ISpanned dest, int dstart, int dend)
        {
            if (string.IsNullOrWhiteSpace(source.ToString()))
            {
                var entry = (CustomEntry)Element;
                entry.OnBackspacePressed();
            }
            return source;
        }

    }
}
This content has 4 years. Some of the information in this post may be out of date or no longer work. Please, read this page keeping its age in your mind.

Xamarin Forms Maps: Nagyításra szánt gombok eltűntetése

Androidon default megjelennek a Xamarin.Forms.Maps.Map használata esetén a nagyításra szánt + és – gombok, illetve a felhasználó aktuális pozíciójára mozgató gomb.
Ezeknek eltűntetéséhez egy CustomMapRenderer-t kell létrehozni, és az Androidos rendererben be kell állítani a térképen, hogy ne jelenlítse meg ezeket a plusz gombokat.

Xamarin Forms kód:

public class CustomMap : Map
    {
        public CustomMap(MapSpan region) : base(region)
        {
        }
    }

Xamarin.Android kód:

[assembly: ExportRenderer(typeof(CustomMap), typeof(CustomMapRenderer))]

namespace SampleApp.Droid.Renderers
{
    public class CustomMapRenderer : MapRenderer
    {
		protected override void OnMapReady(GoogleMap map)
		{
			base.OnMapReady(map);
			// Nagyításhoz gombok 
			map.UiSettings.ZoomControlsEnabled = false;
			// Saját helyzet gombok
			map.UiSettings.MyLocationButtonEnabled = false;
		}
    }
}
This content has 4 years. Some of the information in this post may be out of date or no longer work. Please, read this page keeping its age in your mind.

Xamarin Forms: Nem fordul az újonnan létrehozott solution androidos projektje

Prológus:

A cégnél elsők között lehettem, akik a Xamarin fejlesztés rejtelmeibe áshatták bele magukat. Hogy megelőzzek más fejlesztőknek akár órás fejfájásokat, írásba öntöm tapasztalataimat.

Tézis:
Az újonnan létrehozott Xamarin Forms Solution Androidos projektjének futtatásánál a hasonló hibaüzenetekkel találkozhatunk:

Hibaüzenetek a friss projektnél

A Solution NuGet Csomagjait vizsgáljuk meg először.

Vegyük szemügyre a Xamarin.Forms nevű csomagot, egész pontosan annak a Dependencies résznél található csomagok verzió igényeit.

Xamarin Forms package Dependencies

Láthatjuk, hogy (például) a Xamarin.Android.Support.Design csomag verziószámának meg kell egyezni a 23.3.0 verzióval. Gyanakodhatunk arra, hogy eltérő csomag verziók vannak telepítve. A Xamarin.Android.Support.Design csomag nevére kattintva pedig meggyőződhetünk erről.

A nem megfelelő csomag van telepítve

Frissítsük a NuGet package-t a helyes verzióra. Ellenőrizzük az összes csomagot, majd futtassuk az Androidos projektet.

 

(Archive post from 2017.08.12)

This content has 4 years. Some of the information in this post may be out of date or no longer work. Please, read this page keeping its age in your mind.