[英]WinUI3 TabView XAML does not update the data binding in the view
Currently working on a simple UWP App using C#, WinUI3 and XAML.目前正在使用 C#、WinUI3 和 XAML 开发一个简单的 UWP 应用程序。 Very much like the new changes Microsoft did here.
非常像微软在这里所做的新变化。 However, I got stuck on some strange bindings problem that I can not make sense of.
但是,我遇到了一些我无法理解的奇怪绑定问题。 I am using as simple
ObservableCollection<ProfileTab> ProfileTabs
which should get initialized with a single tab and show in a muxs.TabView
.我使用的是简单的
ObservableCollection<ProfileTab> ProfileTabs
,它应该使用单个选项卡进行初始化并显示在muxs.TabView
。 I just straight followed the example in the XAML Controls Galary ( https://github.com/microsoft/Xaml-Controls-Gallery/blob/f95e0a2003be53228030dcf72032fcb47b96060f/XamlControlsGallery/ControlPages/TabViewPage.xaml.cs & https://github.com/microsoft/Xaml-Controls-Gallery/blob/f95e0a2003be53228030dcf72032fcb47b96060f/XamlControlsGallery/ControlPages/TabViewPage.xaml )我只是直接按照 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 Code 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# Code 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));
}
}
Now the default tab is initialized, I thought great!现在默认选项卡已初始化,我觉得很棒! But whenever add or remove is clicked nothing happened.
但是无论何时单击添加或删除都没有发生。 I started the Debugger, the events get triggered and the
ObservableCollection
seems indeed changing - adding and removing the displayed Tab.我启动了调试器,事件被触发,
ObservableCollection
似乎确实发生了变化 - 添加和删除显示的选项卡。 Now the problem is the view itself does not change - just the single default tab.现在的问题是视图本身不会改变 - 只是单个默认选项卡。
Anyone can point me to the bug or workaround?任何人都可以指出我的错误或解决方法吗? Thanks!
谢谢!
I have tested above code, it works well in my side, and I have make simple sample here please check that.我已经测试了上面的代码,它在我身边运行良好,我在这里做了简单的示例,请检查一下。
And please note the IconSource
of TabView
is under Microsoft.UI.Xaml.Controls
namespace, please don't use Windows.UI.Xaml.Controls
IconSource
to replace, or it will type exception.并且请注意
IconSource
的TabView
在Microsoft.UI.Xaml.Controls
命名空间下,请不要使用Windows.UI.Xaml.Controls
IconSource
替换,否则会键入异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.