簡體   English   中英

WinUI3 TabView XAML 不更新視圖中的數據綁定

[英]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似乎確實發生了變化 - 添加和刪除顯示的選項卡。 現在的問題是視圖本身不會改變 - 只是單個默認選項卡。

任何人都可以指出我的錯誤或解決方法嗎? 謝謝!

我已經測試了上面的代碼,它在我身邊運行良好,我在這里做了簡單的示例請檢查一下。

並且請注意IconSourceTabViewMicrosoft.UI.Xaml.Controls命名空間下,請不要使用Windows.UI.Xaml.Controls IconSource替換,否則會鍵入異常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM