在標題中,我有一個問題,即更新彈出視圖模型中的屬性不會更新 UI。我使用來自 xamarin 社區工具包的彈出視窗。我正在使用執行此任務的命令:
async Task ShowPopup()
{
MessagingCenter.Send(AnimeGroupObservable, "AnimeGroups");
Shell.Current.ShowPopup(new MediaListGroupsPopup());
}
它發送帶有有效負載的訊息并顯示彈出視窗。這是彈出視圖模型:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Text;
using System.Windows.Input;
using OtakuApp.Models;
using Xamarin.Forms;
namespace OtakuApp.ViewModels
{
class MediaListGroupsPopupViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged([CallerMemberName] string name = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
public ObservableCollection<Group> _AnimeGroups = new ObservableCollection<Group>();
public ObservableCollection<Group> AnimeGroups
{
get => _AnimeGroups;
set
{
if (_AnimeGroups == value)
return;
_AnimeGroups = value;
OnPropertyChanged();
}
}
public String _label;
public String label
{
get => _label;
set
{
if (value == _label)
return;
_label = value;
OnPropertyChanged();
}
}
public MediaListGroupsPopupViewModel()
{
MessagingCenter.Subscribe<ObservableCollection<Group>>(this, "AnimeGroups", (AnimeGroupObservable) =>
{
Console.WriteLine(AnimeGroupObservable[0].Name);
label = AnimeGroupObservable[1].Name;
MessagingCenter.Unsubscribe<ObservableCollection<Group>>(this, "AnimeGroups");
});
}
}
}
我計劃有一個小的標簽集合視圖可供選擇。但是現在我正在努力更新一個標簽只是為了測驗目的,所以你可以想象我已經嘗試了集合視圖但它沒有用。在代碼中手動將 _label 設定為某些內容表明系結有效。只是因為某種原因沒有更新。
彈出 xaml 檔案:
<?xml version="1.0" encoding="utf-8" ?>
<xct:Popup
x:Class="OtakuApp.Popups.MediaListGroupsPopup"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
Size="300,300">
<StackLayout>
<Label Text="{Binding label}" />
</StackLayout>
</xct:Popup>
所以現在我有兩個問題:
- 標簽不更新。它系結到具有 INotifyPropertyChanged 的??屬性
- Weirdly this subscription happens only the second time (and after that too, just not the first time) I open up a popup. Is this because it's in the constructor? If yes, what's the correct way to deal with it?
Also a small question - I have unsubscribe at the end of subscription. When I didn't have it and I printed out AnimeGroupObservable[0].Name, the first time it was printed one time, the second time I open up the popup two times etc. Is the unsubscribe at the end the correct way to fix this?
uj5u.com熱心網友回復:
由于您將單個引數傳遞給單個頁面,因此使用建構式比(這很好,但對于這種情況來說太MessagingCenter過分了)要簡單得多
創建頁面時,在建構式中傳遞引數
Shell.Current.ShowPopup(new MediaListGroupsPopup(AnimeGroupObservable));
然后修改頁面建構式接受引數
public MediaListGroupsPopup(ObservableCollection<Group> groups)
{
// you did't show how you create your VM, but I assume it's something like this
this.BindingContext = new MediaListGroupsPopupViewModel(groups);
}
然后修改你的 VM 建構式
public MediaListGroupsPopupViewModel(ObservableCollection<Group> groups)
{
label = groups[1].Name;
}
如果你真的只使用一個string值,你可以只傳遞它而不是整個ObservableCollection
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/313990.html
標籤:xaml xamarin xamarin.forms mvvm xamarin-community-toolkit
