Stacklayout'u viewmodel'e bağlama çalışmıyor

0

Soru

Viewmodel'imde stacklayout'uma bağlı bir özelliğe sahip olmak istiyorum. Bunu stacklyout'umu Viewmodel'e bağlayarak denedim.

Bir düğmeye tıkladığımda, bu düzen görünmez hale getirilmelidir.

Bunu aşağıdaki kodla yaptığımda, programım bir nesnenin örneğine ayarlanmamış bir NulReferenceObject: Object Referansı ile çöküyor. Bahsettiğim StackLayout, aşağıdaki koddaki ilk koddur.

<FlexLayout>
    <StackLayout BindableLayout.ItemTemplate="{Binding CreateQuizPageQuizNameSL}"> // This StackLayout should be bind to the ViewModel
        <Label Text="Create New Quiz" />
        <StackLayout >
            <Entry Text="{Binding QuizNameInput}" Placeholder="Enter quiz name"/>
        </StackLayout>
    </StackLayout>
    <Button Command="{Binding SubmitCreateQuizCommand}" Text="Create my quiz now!"></Button>
</FlexLayout>

Görünüm Modeli

internal class CreateQuizPageViewModel
{
    // Quiz Name Input
    public String QuizNameInput { get; set; }

    // Command submit creating a quiz
    public Command SubmitCreateQuizCommand { get; set; }

    public StackLayout CreateQuizPageQuizNameSL { get; set; } = new StackLayout();

    public CreateQuizPageViewModel()
    {
        // Declaring a new command, giving the OnSubmitCreateNewQuizClick to the delegate
        SubmitCreateQuizCommand = new Command(OnSubmitCreateNewQuizClick);
    }

    // When a user submit the creation of new quiz
    public void OnSubmitCreateNewQuizClick()
    {
        CreateQuizPageQuizNameSL.IsVisible = false;
    }
}
binding stacklayout xamarin.forms
2021-11-22 16:48:44
1

En iyi cevabı

0

Kullanarak iki mizanpajı nasıl değiştireceğiniz aşağıda açıklanmıştır IsVisible bağlayıcı.

ÖNCE ekle Nuget Xamarin.CommunityToolkit Xamarin Formları projenize. ("MyProjectName "olan," olmadan.iOS "veya".Android " sonunda.)

TwoLayoutPage.xamlname:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
                 xmlns:local="clr-namespace:TestBugs"
                 x:Class="TestBugs.TwoLayoutPage">
    <ContentPage.BindingContext>
        <local:TwoLayoutViewModel/>
    </ContentPage.BindingContext>
    <ContentPage.Resources>
        <ResourceDictionary>
            <xct:InvertedBoolConverter x:Key="InvertedBoolConverter" />
        </ResourceDictionary>
    </ContentPage.Resources>
    <ContentPage.Content>
        <StackLayout>
            <StackLayout 
                    IsVisible="{Binding UseSecondLayout, Converter={StaticResource InvertedBoolConverter}}"
                    VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand">
                <Label Text="First Layout" FontSize="20" />
                <Button Text="To Second" Command="{Binding SwitchToSecondLayoutCommand}" />
            </StackLayout>
            <StackLayout IsVisible="{Binding UseSecondLayout}"
                    VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand">
                <Label Text="Second Layout!" FontSize="32" />
            </StackLayout>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

TwoLayoutViewModel.cs:

using Xamarin.Forms;

namespace TestBugs
{
    public class TwoLayoutViewModel : BindableObject
    {
        private bool _usesecondLayout = false;
        public bool UseSecondLayout {
            get => _usesecondLayout;
            set {
                _usesecondLayout = value;
                OnPropertyChanged();
            }
        }


        public TwoLayoutViewModel()
        {
            SwitchToSecondLayoutCommand = new Command(SwitchToSecondLayout);
        }


        public Command SwitchToSecondLayoutCommand { get; set; }


        private void SwitchToSecondLayout()
        {
            UseSecondLayout = true;
        }
    }
}
2021-11-22 20:26:37

Diğer dillerde

Bu sayfa diğer dillerde

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................