Customizing Xamarin.Forms layouts

sroes

I'm building an Android and iOS app using Xamarin Forms.

What I'm simply trying to do is set a background drawable on my Android app for my ListView items. The root view of my ListView items are StackLayout's:

var listView = new ListView
{
    ItemsSource = items,
    ItemTemplate = new DataTemplate(() =>
    {
        return new ViewCell
        {
            View = new StackLayout(...)
        };
    }
};

I know I can access the native element by using a custom renderer:

public class MyEntryRenderer : EntryRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
    {
        base.OnElementChanged(e);

        if (e.OldElement == null) {
            var nativeEditText = (EditText)Control;
            ...
        }
    }
}

But I'm not sure how this would work for a StackLayout (or any other layout for that matter).

I first extended StackLayout:

public class ListViewItem : StackLayout
{
}

And I read somewhere that layouts use the VisualElementRenderer, so I tried the following:

public class ListViewItemRenderer : VisualElementRenderer<StackLayout>
{
    protected override void OnElementChanged(ElementChangedEventArgs<StackLayout> e)
    {
        base.OnElementChanged(e);

        // any way to access the native element?
    }
}

But VisualElementRenderer does not seem to give me access to the native element.

So is there any way I can access the native elements of Layout elements? Or maybe there is a different way to simply set a background drawable on layouts within my Android app?

sroes

Even though I still don't know how to access the native element of a layout, the VisualElementRenderer has a method for setting the background drawable on Android (which was exactly what I needed). So I ended up with the following:

protected override void OnElementChanged(ElementChangedEventArgs<StackLayout> e)
{
    base.OnElementChanged(e);
    SetBackgroundDrawable(Resources.GetDrawable(Resource.Drawable.listViewItem));
}

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

xamarin forms - actionbar color

분류에서Dev

Xamarin Forms Switch XAML

분류에서Dev

Xamarin Forms Reverse PushModalAsync

분류에서Dev

Xamarin Forms 영역

분류에서Dev

Xamarin Forms DependencyService nullReferenceException

분류에서Dev

Xamarin Forms EventToCommandBehavior

분류에서Dev

Xamarin Forms TemplateSelector for View

분류에서Dev

Xamarin.Forms없이 Xamarin BeginInvokeOnMainThread

분류에서Dev

Xamarin.Forms Accessibilty VoiceReader

분류에서Dev

Use RadioButton in Xamarin.Forms

분류에서Dev

Xamarin Forms: ContentPage OnCreate event?

분류에서Dev

Xamarin.Forms 맵 핀

분류에서Dev

Xamarin.Forms (공유)

분류에서Dev

Customizing Registration form in Visual Studio 2013 using Asp.net Identity (Web Forms)

분류에서Dev

Xamarin Forms : Toasts.Forms.Plugin 오류

분류에서Dev

Onlaunched의 Xamarin.Forms.Forms.Init (e) FileNotFoundExeception

분류에서Dev

Xamarin Forms Remove space from Entry

분류에서Dev

Xamarin.Forms 선택기

분류에서Dev

Xamarin Forms Bind List <myObject> to Picker

분류에서Dev

Xamarin Forms Transalted 단추 클릭

분류에서Dev

Xamarin Forms iOS Renderer for Facebook Audience Network

분류에서Dev

How to use message pack in xamarin forms

분류에서Dev

Xamarin Forms Artnet 및 UDP 전송

분류에서Dev

Xamarin Forms, XAML의 동적 ScrollView

분류에서Dev

Xamarin.Forms-DatePicker 복제

분류에서Dev

How to access a field in a database table in Xamarin Forms

분류에서Dev

Xamarin Forms Maps Google 전용?

분류에서Dev

Xamarin.Forms에 뷰 삽입

분류에서Dev

NuGet package(s) that are missing in Xamarin Forms