[英]WinUI3 TabView XAML does not update the data binding in the view
目前正在使用 C#、WinUI3 和 XAML 開發一個簡單的 UWP 應用程序。 非常像微軟在這里所做的新變化。 但是,我遇到了一些我無法理解的奇怪綁定問題。 我使用的是簡單的ObservableCollection<ProfileTab> ProfileTabs
,它應該使用單個選項卡進行初始化並顯示在muxs.TabView
。 我只是直接按照 XAML Controls Galary 中的示例( https://github.com/microsoft/Xaml-Controls-Gallery/blob/f95e0a2003be53228030dcf72032fcb47b96060f/XamlControlsGallery/ControlPages/TabViewPages & : //c. microsoft/Xaml-Controls-Gallery/blob/f95e0a2003be53228030dcf72032fcb47b96060f/XamlControlsGallery/ControlPages/TabViewPage.xaml )
XAML 代碼
<TabView Grid.Row="1" TabItemsSource="{x:Bind ProfileTabs, Mode=OneWay}" AddTabButtonClick="TabView_AddButtonClick" TabCloseRequested="TabView_TabCloseRequested">
<TabView.TabItemTemplate>
<DataTemplate x:DataType="local:ProfileTab">
<muxc:TabViewItem Header="{x:Bind TabHeader}" IconSource="{x:Bind TabIconSource}" Content="{x:Bind TabContent}" />
</DataTemplate>
</TabView.TabItemTemplate >
</TabView>
C# 代碼
public class ProfileTab
{
public string TabHeader { get; set; }
public IconSource TabIconSource { get; set; }
public object TabContent { get; set; }
}
public sealed partial class MainPage : Page
{
public ObservableCollection<ProfileTab> ProfileTabs { get; set; }
public MainPage()
{
this.InitializeComponent();
InitializeSampleProfile();
}
private void TabView_AddButtonClick(TabView sender, object args)
{
var profile = new SettingsProfile("");
ProfileTabs.Add(CreateNewTab(profile));
}
private void TabView_TabCloseRequested(TabView sender, TabViewTabCloseRequestedEventArgs args)
{
ProfileTabs.Remove(args.Item as ProfileTab);
}
private ProfileTab CreateNewTab(SettingsProfile profile)
{
var profileTab = new ProfileTab
{
TabHeader = $"{profile.PrettyName()}",
TabIconSource = new SymbolIconSource() { Symbol = Symbol.Document },
};
// The content of the tab is a frame that contains a page, pass the profile as parameter
Frame frame = new Frame();
frame.Navigate(typeof(ProfilePage), profile);
profileTab.TabContent = frame;
return profileTab;
}
private void InitializeSampleProfile()
{
ProfileTabs = new ObservableCollection<ProfileTab>();
// load sample data
ProfileTabs.Add(CreateNewTab(defaultProfile));
}
}
現在默認選項卡已初始化,我覺得很棒! 但是無論何時單擊添加或刪除都沒有發生。 我啟動了調試器,事件被觸發, ObservableCollection
似乎確實發生了變化 - 添加和刪除顯示的選項卡。 現在的問題是視圖本身不會改變 - 只是單個默認選項卡。
任何人都可以指出我的錯誤或解決方法嗎? 謝謝!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.