這是我的 xaml 檔案:
<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>
這是我的 xaml.cs 檔案:
namespace MojaApkaMobilna
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
BindingContext = new MainViewModel();
}
}
}
我的視圖模型類:
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);
}
}
我的問題是我的 scoolview 不起作用。有趣的是,當程式運行時,當我洗掉該 ItemsSource(xaml 檔案)并再次寫入時,它開始作業。為什么會發生以及如何修復它?
<CollectionView ItemsSource="{Binding Persons}">
我在 Xamarin 方面很弱,所以感謝您的理解。
uj5u.com熱心網友回復:
我找到了解決方案。在AssemblyInfo檔案中:
[assembly: XamlCompilation(XamlCompilationOptions.Compile)]
替換為:
[assembly: XamlCompilation(XamlCompilationOptions.Skip)]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/495572.html
