現在我試圖實作一個collectionview和不同用戶的選擇。為了獲得選定用戶的數量,我在 obersavle 集合之外定義了一個名為 _nrofselectedUser 的變數。它與我定義的 SelectedUser ObservableList 的 count() 相同。ObservableCollection 作業正常,當洗掉其中的物件時,我得到了所有的東西并更新了 ui。但我無法獲得 nrofselectedUser 權限的資料系結。
這是初始化 VielModel 的主頁:
public partial class UserCollectionPage : ContentPage
{
private UserCollectionViewModel _uvm;
private UserDBHelper userDBHelper = new UserDBHelper();
private int _nrOfSelectedUser { get; set; }
public UserCollectionPage()
{
BindingContext = _uvm = new UserCollectionViewModel();
InitializeComponent();
}
這是ModelView,叫做UserCollectionViewModel:
命名空間 App1.ViewModels { public class UserCollectionViewModel : BaseViewModel { private ObservableCollection _user;
public ObservableCollection<object> _selectedUser { get; set; }
private SelectionMode _selectionMode = SelectionMode.None;
private int _nrOfSelectedUser { get; set; }
public User SelectedItem { get; set; }
public SelectionMode SelectionMode { get => _selectionMode; set => SetProperty(ref _selectionMode, value); }
public ObservableCollection<User> User { get => _user; set => _user = value; }
public ObservableCollection<object> SelectedUser { get => _selectedUser; set => _selectedUser = value; }
public UserDBHelper userDBHelper = new UserDBHelper();
public UserCollectionViewModel()
{
InitData();
ShareCommand = new Command(OnShare);
LongPressCommand = new Command<User>(OnLongPress);
ClearCommand = new Command(OnClear);
PressedCommand = new Command<User>(OnPressed);
RefreshCommand = new Command(ExecuteRefreshCommand);
}
private void InitData()
{
_selectedUser = new ObservableCollection<object>();
_user = userDBHelper.GetAllUserToCollection();
_nrOfSelectedUser = SelectedUser.Count();
}
public int NrofSelectedUser
{
get => _nrOfSelectedUser;
set
{
_nrOfSelectedUser = value;
OnPropertyChanged(nameof(NrofSelectedUser));
}
}
private void OnLongPress(User p)
{
try
{
// Use default vibration length
Vibration.Vibrate();
Console.WriteLine("LongPressed");
if (_selectionMode == SelectionMode.None)
{
SelectionMode = SelectionMode.Multiple;
SelectedUser.Add(p);
_nrOfSelectedUser ;
Console.Write("Add User");
}
}
catch (FeatureNotSupportedException ex)
{
Console.WriteLine("Feature not supported on device" ex);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
private async void OnPressed(User obj)
{
Console.WriteLine("User In Collection: " SelectedUser.Count());
if (_selectionMode != SelectionMode.None)
{
_nrOfSelectedUser ;
Console.WriteLine("Test OnPress");
if (_selectedUser.Count() == 0)
{
SelectionMode = SelectionMode.None;
}
else
{
Console.WriteLine("Test OnPress");
}
}
else
{
await App.Current.MainPage.DisplayToastAsync("Navigiere zu User Details");
}
}
這是 MainPage 的 XAML
<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"
x:Class="App1.View.UserCollectionPage"
xmlns:pg="clr-namespace:App1.ViewModels"
x:Name="UserCollectionView"
xmlns:converters="clr-namespace:App1.Methods;assembly=App1">
<ContentPage.Resources>
<ResourceDictionary>
<converters:IntToString x:Key="IntToString" />
</ResourceDictionary>
</ContentPage.Resources>
<NavigationPage.TitleView>
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.5*"/>
<ColumnDefinition Width="0.5*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Label Grid.Column="0"
MaxLines="1"
Text="Patienten"
FontSize="Medium"
HorizontalOptions="StartAndExpand"
VerticalOptions="Center"/>
<StackLayout Grid.Column="1"
HorizontalOptions="EndAndExpand"
Orientation="Horizontal">
<Label Text="Selected:"
VerticalOptions="Center"
FontSize="Medium"
HorizontalOptions="EndAndExpand"/>
<Label Text="{Binding Path=BindingContext.NrofSelectedUser, Source={x:Reference UserCollectionView},Converter={StaticResource IntToString}}" VerticalOptions="Center" FontSize="Medium" HorizontalOptions="EndAndExpand"/>
<Image
Grid.Column="2" Source="https://img.icons8.com/external-tulpahn-flat-tulpahn/64/fa314a/external-bin-mobile-user-interface-tulpahn-flat-tulpahn.png"
HorizontalOptions="EndAndExpand"/>
</StackLayout>
</Grid>
</NavigationPage.TitleView>
<StackLayout>
<CollectionView x:Name="UserCV" SelectionMode="{Binding SelectionMode}" SelectedItems="{Binding SelectedUser}" ItemsSource="{Binding User}"
As you can see in the NavigationPage.TitleView i want to set the nrofselectedUser, so that it changes, when the selectedUser changes. But it isnt working, and i tried different things:
1. <Label Text="{Binding NrofSelectedUser}" so just Binding without anything
2. <Label Text="{Binding NrofSelectedUser, Converter={StaticResource IntToString}" Binding and converting the Integer to a string variable
3. <Label Text="{Binding Path=BindingContext.NrofSelectedUser, Source={x:Reference UserCollectionView}}" Binding with parent referenz to the contentpage of the usercollectionView Xaml
So on every Attempt, the initialising of the nrofselectedUser is working on InitData(). So the number is at first 0, which is correct and also got displayed in the app.
但在那之后,當我在集合視圖中選擇用戶時,無論我嘗試什么,UI 都不會更新。
在除錯中,我可以看到,值會根據按下的變化而變化,但我也設定了 nrofselectUser = SelectedUser.Count()
所以我不知道為什么資料系結不起作用,
我認為這是要做的事情, 或 nrofselectUser = SelectedUser.Count() 沒有呼叫 baseView 中的 onpropertychanged 方法。但我不知道如何觸發它...
請幫忙
順便說一句,BaseViewModel
public abstract class BaseViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string name = null) =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
uj5u.com熱心網友回復:
你正在增加私有變數
_nrOfSelectedUser ;
相反,您需要增加public 屬性,這將導致PropertyChanged觸發
NrofSelectedUser ;
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/417498.html
標籤:
上一篇:從父函式獲取映射子組件的鍵
下一篇:從資料集創建高余額和低余額計算
