It is my xaml file:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:mojaapkamobilna="clr-namespace:MojaApkaMobilna" x:DataType="mojaapkamobilna:MainViewModel"
x:Class="MojaApkaMobilna.MainPage">
<StackLayout>
<Label x:Name="TopLabel" Text="{Binding TopText}"></Label>
<StackLayout Orientation="Horizontal">
<Label VerticalOptions="Center">Imie:</Label>
<Entry x:Name="Name" Placeholder="Wpisz swoje imię" WidthRequest="300" Text="{Binding Name}"></Entry>
</StackLayout>
<StackLayout Orientation="Horizontal">
<Label VerticalOptions="Center">Haslo:</Label>
<Entry x:Name="Password" Placeholder ="Wpisz swoje hasło" IsPassword="True" WidthRequest="300" Text="{Binding Password}"></Entry>
</StackLayout>
<Button Text="Kliknij we mnie..." Command = "{Binding OnClickCommand}"></Button>
<ScrollView>
<CollectionView ItemsSource="{Binding Persons}">
<CollectionView.ItemTemplate>
<DataTemplate>
<StackLayout>
<Label FontSize="20" Text="{Binding Name}" />
<Label FontSize="20" Text="{Binding Password}"></Label>
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</ScrollView>
<Button></Button>
</StackLayout>
And it's my xaml.cs file:
namespace MojaApkaMobilna
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
BindingContext = new MainViewModel();
}
}
}
And my view model class:
namespace MojaApkaMobilna
{
internal class MainViewModel : INotifyPropertyChanged
{
private string _name;
private string _password;
private string _topText;
public ObservableCollection<Person> Persons { get; } = new ObservableCollection<Person>();
public string TopText
{
get => _topText;
set
{
_topText = value;
OnPropertyChanged(nameof(TopText));
}
}
public string Name
{
get => _name;
set
{
_name = value;
OnPropertyChanged(nameof(Name));
OnClickCommand.ChangeCanExecute();
}
}
public string Password
{
get => _password;
set
{
_password = value;
OnPropertyChanged(nameof(Password));
OnClickCommand.ChangeCanExecute();
}
}
public event PropertyChangedEventHandler PropertyChanged;
public Command OnClickCommand { get; }
private void Button_Clicked()
{
TopText = "Wpisane imie: " Name ", Haslo: " Password;
try
{
Persons.Add(new Person(Name, Password));
foreach (var person in Persons)
{
Console.WriteLine(person.Name);
Console.WriteLine(person.Password);
}
Console.WriteLine(Persons.Count);
}
catch
{
TopText = "Nie powiódł się zapis nowego użytkownika";
}
}
public MainViewModel()
{
OnClickCommand = new Command(Button_Clicked, Validate);
}
/* Informowanie widoku o zmianach */
private void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private bool Validate() => !string.IsNullOrWhiteSpace(Name) && !string.IsNullOrWhiteSpace(Password);
}
}
And my problem is that my scroolview doesnt work. Interesting is that when program is running, when I delete that ItemsSource (xaml file) and write it again, it starts working. Why is it happening and how to repair it?
<CollectionView ItemsSource="{Binding Persons}">
I'm weak in Xamarin, so thank you for your understanding.
CodePudding user response:
I found solution. In AssemblyInfo file:
[assembly: XamlCompilation(XamlCompilationOptions.Compile)]
replace by:
[assembly: XamlCompilation(XamlCompilationOptions.Skip)]