繁体   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