如何在.NET MAUI Shell和MVVM中使用不同的绑定源填充多个选项卡,但内容相同?

huangapple go评论75阅读模式
英文:

How can I populate multiple Tabs with the same page content but a different binding source in .Net MAUI Shell and MVVM

问题

我正在开发一个应用程序,该应用程序将显示每周每天的数据,从星期一到星期日。我希望每天都是一个单独的选项卡,以便您可以在不同天之间轻松切换。目前,我正在使用一个选项卡,其中包含7个不同的ShellContent页面,每个页面完全相同,与ViewModel中的不同可观察集合绑定。

这种方法确实可以工作,但我遇到了两个主要问题:
1)由于我创建了7个不同的内容页面,如果我更改其中一个的布局,那么我必须手动更改所有页面的布局,这既繁琐又容易出错。
2)每次打开一个选项卡时,都会创建ViewModel的新实例,这些实例需要在用户切换选项卡时加载,这对用户来说可能不太流畅。

我查看了TabbedPage,看起来这是我想要的东西,我可以添加一个DataTemplate并使用项源绑定选项卡,但是无法与Shell一起使用TabbedBar。

我应该如何重复使用相同的内容以及在用户切换选项卡之前加载所有页面的数据?我可以接受初始加载速度较慢,因为我可以在上面放置加载指示器。

目前,我是这样添加它们的:

<FlyoutItem Title="Weekly Rota" Icon="team.png">
        <Tab>
            <ShellContent Title="Monday" Shell.TabBarIsVisible="False" ContentTemplate="{DataTemplate startupPages:WeeklyRotaMonday}"/>
            <ShellContent Title="Tuesday" Shell.TabBarIsVisible="False" ContentTemplate="{DataTemplate startupPages:WeeklyRotaTuesday}" />
            <ShellContent Title="Wednesday" Shell.TabBarIsVisible="False" ContentTemplate="{DataTemplate startupPages:WeeklyRotaWednesday}" />
            <ShellContent Title="Thursday" Shell.TabBarIsVisible="False" ContentTemplate="{DataTemplate startupPages:WeeklyRotaThursday}" />
            <ShellContent Title="Friday" Shell.TabBarIsVisible="False" ContentTemplate="{DataTemplate startupPages:WeeklyRotaFriday}" />
            <ShellContent Title="Saturday" Shell.TabBarIsVisible="False" ContentTemplate="{DataTemplate startupPages:WeeklyRotaSaturday}" />
            <ShellContent Title="Sunday" Shell.TabBarIsVisible="False" ContentTemplate="{DataTemplate startupPages:WeeklyRotaSunday}"  />
        </Tab>
    </FlyoutItem>

ViewModel中的代码如下:

public ObservableCollection<DailyData> MondayList { get; set; } = new();
public ObservableCollection<DailyData> TuesdayList { get; } = new();
public ObservableCollection<DailyData> WednesdayList { get; } = new();

然后,通过页面的代码后台,将数据绑定到每个页面:

public WeeklyRotaFriday(WeeklyRotaViewModel viewModel)
{
    InitializeComponent();
    this.BindingContext = viewModel;
    viewModel.GetDailyDataCommand.Execute(DayOfWeek.Friday);
}

我理解这种方法远非理想,但我对应用程序开发非常陌生,并且找不到完全符合我尝试实现的示例。它确实可以运行,但应该有更好的方法。

英文:

I am developing an App that will display data for each day of the week , Monday to Sunday
I would like each day to be a separate tab so you can swipe through the days, Currently i am using a Tab that adds 7 different shellContent pages each one is exactly the same and is bound to a different observable collection in the viewmodel.
This does work but there are 2 main issues I am having. 1) as I have created 7 different content pages if i change the layout in one i have to then manually change the layout for all of them, which is tedious and error prone. 2) every time i open a tab it is creating a new instance of the viewmodel and these need to get loaded into the tab page on each swipe which is jarring for the user.

I looked at TabbedPage and this seems to be what i want, I can add one Datatemplate and bind the tabs using the item source however i cannont use TabbedBar with Shell.

How can i resuse the same content for each tab and ideally have all the pages loaded with their data before the user has to swipe. I am ok with the initial loading being a little slower as i can but a loading indicator on it.

currently I am adding them like this

&lt;FlyoutItem Title=&quot;Weekly Rota&quot; Icon=&quot;team.png&quot;&gt;
        &lt;Tab&gt;
            &lt;ShellContent Title=&quot;Monday&quot; Shell.TabBarIsVisible=&quot;False&quot; ContentTemplate=&quot;{DataTemplate startupPages:WeeklyRotaMonday}&quot;/&gt;
            &lt;ShellContent Title=&quot;Tuesday&quot; Shell.TabBarIsVisible=&quot;False&quot; ContentTemplate=&quot;{DataTemplate startupPages:WeeklyRotaTuesday}&quot; /&gt;
            &lt;ShellContent Title=&quot;Wednesday&quot; Shell.TabBarIsVisible=&quot;False&quot; ContentTemplate=&quot;{DataTemplate startupPages:WeeklyRotaWednesday}&quot; /&gt;
            &lt;ShellContent Title=&quot;Thursday&quot; Shell.TabBarIsVisible=&quot;False&quot; ContentTemplate=&quot;{DataTemplate startupPages:WeeklyRotaThursday}&quot; /&gt;
            &lt;ShellContent Title=&quot;Friday&quot; Shell.TabBarIsVisible=&quot;False&quot; ContentTemplate=&quot;{DataTemplate startupPages:WeeklyRotaFriday}&quot; /&gt;
            &lt;ShellContent Title=&quot;Saturday&quot; Shell.TabBarIsVisible=&quot;False&quot; ContentTemplate=&quot;{DataTemplate startupPages:WeeklyRotaSaturday}&quot; /&gt;
            &lt;ShellContent Title=&quot;Sunday&quot; Shell.TabBarIsVisible=&quot;False&quot; ContentTemplate=&quot;{DataTemplate startupPages:WeeklyRotaSunday}&quot;  /&gt;
        &lt;/Tab&gt;
    &lt;/FlyoutItem&gt;

the ViewModel has the following Code

        public ObservableCollection&lt;DailyData&gt; MondayList { get; set; } = new();
        public ObservableCollection&lt;DailyData&gt; TuesdayList { get; } = new();
        public ObservableCollection&lt;DailyData&gt; WedensdayList { get; } = new();

The data is then bound to each page via the codebehind of the page

    public WeeklyRotaFriday(WeeklyRotaViewModel viewModel)
    {
        InitializeComponent();
        this.BindingContext = viewModel;
        viewModel.GetDailyDataCommand.Execute(DayOfWeek.Friday);
    }

I understand this approach is far from ideal but I am very new to App Development and and have not been able to find any examples that do exactly what i am trying to do. It does function but there must be a better way to do it.

答案1

得分: 1

以下是您要翻译的内容:

You can use only one page and achieve to change content for different day by override OnParentChanging method.

Add Route to every ShellContent:

&lt;FlyoutItem&gt;
    &lt;Tab&gt;
        &lt;ShellContent Title=&quot;Monday&quot; Route=&quot;Monday&quot; Shell.TabBarIsVisible=&quot;False&quot; ContentTemplate=&quot;{DataTemplate local:MainPage}&quot;/&gt;
        &lt;ShellContent Title=&quot;Tuesday&quot; Route=&quot;Tuesday&quot; Shell.TabBarIsVisible=&quot;False&quot; ContentTemplate=&quot;{DataTemplate local:MainPage}&quot; /&gt;
        &lt;ShellContent Title=&quot;Wednesday&quot; Route=&quot;Wednesday&quot; Shell.TabBarIsVisible=&quot;False&quot; ContentTemplate=&quot;{DataTemplate local:MainPage}&quot; /&gt;
        &lt;ShellContent Title=&quot;Thursday&quot; Route=&quot;Thursday&quot; Shell.TabBarIsVisible=&quot;False&quot; ContentTemplate=&quot;{DataTemplate local:MainPage}&quot; /&gt;
        &lt;ShellContent Title=&quot;Friday&quot; Route=&quot;Friday&quot; Shell.TabBarIsVisible=&quot;False&quot; ContentTemplate=&quot;{DataTemplate local:MainPage}&quot; /&gt;
        &lt;ShellContent Title=&quot;Saturday&quot; Route=&quot;Saturday&quot; Shell.TabBarIsVisible=&quot;False&quot; ContentTemplate=&quot;{DataTemplate local:MainPage}&quot; /&gt;
        &lt;ShellContent Title=&quot;Sunday&quot; Route=&quot;Sunday&quot; Shell.TabBarIsVisible=&quot;False&quot; ContentTemplate=&quot;{DataTemplate local:MainPage}&quot; /&gt;
    &lt;/Tab&gt;
&lt;/FlyoutItem&gt;

Page.xaml:

&lt;ContentPage xmlns=&quot;http://schemas.microsoft.com/dotnet/2021/maui&quot;
             xmlns:x=&quot;http://schemas.microsoft.com/winfx/2009/xaml&quot; 
             x:Class=&quot;MauiApp2.MainPage&quot;&gt;
    &lt;ScrollView&gt;
        &lt;VerticalStackLayout&gt;
            &lt;Label Text=&quot;{Binding Selection}&quot;/&gt;
            &lt;CollectionView ItemsSource=&quot;{Binding WeekList}&quot;&gt;
                &lt;CollectionView.ItemTemplate&gt;
                    &lt;DataTemplate&gt;
                        &lt;Image Source=&quot;{Binding Name}&quot;/&gt;
                    &lt;/DataTemplate&gt;
                &lt;/CollectionView.ItemTemplate&gt;
            &lt;/CollectionView&gt;
        &lt;/VerticalStackLayout&gt;
    &lt;/ScrollView&gt;
&lt;/ContentPage&gt;

Override OnParentChanging in Page.xaml.cs:

public partial class MainPage : ContentPage
{
    VM VM = new VM();
    public MainPage()
    {
        InitializeComponent();
        BindingContext = VM;

    }
    protected override void OnParentChanging(ParentChangingEventArgs args)
    {
        base.OnParentChanging(args);
        var shellContent = args.NewParent as ShellContent;
        var route = shellContent.Route;
        switch (route)
        {
            case &quot;Monday&quot;:
                VM.Selection = &quot;Wednesday&quot;;
                break;
            case &quot;Tuesday&quot;:
                VM.Selection = &quot;Tuesday&quot;;
                VM.WeekList = VM.NameIdList1;
                break;
            case &quot;Wednesday&quot;:
                VM.Selection = &quot;Wednesday&quot;;
                VM.WeekList = VM.NameIdList2;
                break;
            case &quot;Thursday&quot;:
                VM.Selection = &quot;Thursday&quot;;
                break;
            case &quot;Friday&quot;:
                VM.Selection = &quot;Friday&quot;;
                break;
            case &quot;Saturday&quot;:
                VM.Selection = &quot;Saturday&quot;;
                break;
            case &quot;Sunday&quot;:
                VM.Selection = &quot;Sunday&quot;;
                break;
            default: break;
        }
    }
}

ViewModel:

public class VM : INotifyPropertyChanged
{
    public ObservableCollection&lt;NameId&gt; WeekList { get; set; } = new();
    public ObservableCollection&lt;NameId&gt; NameIdList1 { get; set; } 
    = new ObservableCollection&lt;NameId&gt; 
    { 
        new NameId { Id = &quot;Id A&quot;, Name = &quot;Name A&quot; }, 
        new NameId { Id = &quot;Id B&quot;, Name = &quot;Name B&quot; }, 
        new NameId { Id = &quot;Id C&quot;, Name = &quot;Name C&quot; } 
    };
    public ObservableCollection&lt;NameId&gt; NameIdList2 { get; set; } 
    = new ObservableCollection&lt;NameId&gt; 
    { 
        new NameId { Id = &quot;Id A&quot;, Name = &quot;Name 2&quot; }, 
        new NameId { Id = &quot;Id B&quot;, Name = &quot;Name 2&quot; }, 
        new NameId { Id = &quot;Id C&quot;, Name = &quot;Name 2&quot; } 
    };
    
    public string selection;
    public string Selection
    {
        get =&gt; selection;
        set
        {
            selection = value;
            OnPropertyChanged(nameof(Selection));
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}
英文:

You can use only one page and achieve to change content for different day by override OnParentChanging method.

Add Route to every ShellContent:

&lt;FlyoutItem&gt;
    &lt;Tab&gt;
        &lt;ShellContent Title=&quot;Monday&quot; Route=&quot;Monday&quot; Shell.TabBarIsVisible=&quot;False&quot; ContentTemplate=&quot;{DataTemplate local:MainPage}&quot;/&gt;
        &lt;ShellContent Title=&quot;Tuesday&quot; Route=&quot;Tuesday&quot; Shell.TabBarIsVisible=&quot;False&quot; ContentTemplate=&quot;{DataTemplate local:MainPage}&quot; /&gt;
        &lt;ShellContent Title=&quot;Wednesday&quot; Route=&quot;Wednesday&quot; Shell.TabBarIsVisible=&quot;False&quot; ContentTemplate=&quot;{DataTemplate local:MainPage}&quot; /&gt;
        &lt;ShellContent Title=&quot;Thursday&quot; Route=&quot;Thursday&quot; Shell.TabBarIsVisible=&quot;False&quot; ContentTemplate=&quot;{DataTemplate local:MainPage}&quot; /&gt;
        &lt;ShellContent Title=&quot;Friday&quot; Route=&quot;Friday&quot; Shell.TabBarIsVisible=&quot;False&quot; ContentTemplate=&quot;{DataTemplate local:MainPage}&quot; /&gt;
        &lt;ShellContent Title=&quot;Saturday&quot; Route=&quot;Saturday&quot; Shell.TabBarIsVisible=&quot;False&quot; ContentTemplate=&quot;{DataTemplate local:MainPage}&quot; /&gt;
        &lt;ShellContent Title=&quot;Sunday&quot; Route=&quot;Sunday&quot; Shell.TabBarIsVisible=&quot;False&quot; ContentTemplate=&quot;{DataTemplate local:MainPage}&quot; /&gt;
    &lt;/Tab&gt;
&lt;/FlyoutItem&gt;

Page.xaml:

&lt;ContentPage xmlns=&quot;http://schemas.microsoft.com/dotnet/2021/maui&quot;
             xmlns:x=&quot;http://schemas.microsoft.com/winfx/2009/xaml&quot; 
             x:Class=&quot;MauiApp2.MainPage&quot;&gt;
    &lt;ScrollView&gt;
        &lt;VerticalStackLayout&gt;
            &lt;Label Text=&quot;{Binding Selection}&quot;/&gt;
            &lt;CollectionView ItemsSource=&quot;{Binding WeekList}&quot;&gt;
                &lt;CollectionView.ItemTemplate&gt;
                    &lt;DataTemplate&gt;
                        &lt;Image Source=&quot;{Binding Name}&quot;/&gt;
                    &lt;/DataTemplate&gt;
                &lt;/CollectionView.ItemTemplate&gt;
            &lt;/CollectionView&gt;
        &lt;/VerticalStackLayout&gt;
    &lt;/ScrollView&gt;
&lt;/ContentPage&gt;

Override OnParentChanging in Page.xaml.cs:

public partial class MainPage : ContentPage
{
    VM VM = new VM();
    public MainPage()
    {
        InitializeComponent();
        BindingContext = VM;

    }
    protected override void
    OnParentChanging(ParentChangingEventArgs args)
    {
        base.OnParentChanging(args);
        var shellContent = args.NewParent as ShellContent;
        var route = shellContent.Route;
        switch (route)
        {
            case &quot;Monday&quot;:
                VM.Selection = &quot;Wednesday&quot;;
                break;
            case &quot;Tuesday&quot;:
                VM.Selection = &quot;Tuesday&quot;;
                VM.WeekList = VM.NameIdList1;
                break;
            case &quot;Wednesday&quot;:
                VM.Selection = &quot;Wednesday&quot;;
                VM.WeekList = VM.NameIdList2;
                break;
            case &quot;Thursday&quot;:
                VM.Selection = &quot;Thursday&quot;;
                break;
            case &quot;Friday&quot;:
                VM.Selection = &quot;Friday&quot;;
                break;
            case &quot;Saturday&quot;:
                VM.Selection = &quot;Saturday&quot;;
                break;
            case &quot;Sunday&quot;:
                VM.Selection = &quot;Sunday&quot;;
                break;
            default: break;
        }
    }
}

ViewModel:

public class VM : INotifyPropertyChanged
{
    public ObservableCollection&lt;NameId&gt; WeekList { get; set; } = new();
    public ObservableCollection&lt;NameId&gt; NameIdList1 { get; set; } 
    = new ObservableCollection&lt;NameId&gt; 
    { 
        new NameId { Id = &quot;Id A&quot;, Name = &quot;Name A&quot; }, 
        new NameId { Id = &quot;Id B&quot;, Name = &quot;Name B&quot; }, 
        new NameId { Id = &quot;Id C&quot;, Name = &quot;Name C&quot; } 
    };
    public ObservableCollection&lt;NameId&gt; NameIdList2 { get; set; } 
    = new ObservableCollection&lt;NameId&gt; 
    { 
        new NameId { Id = &quot;Id A&quot;, Name = &quot;Name 2&quot; }, 
        new NameId { Id = &quot;Id B&quot;, Name = &quot;Name 2&quot; }, 
        new NameId { Id = &quot;Id C&quot;, Name = &quot;Name 2&quot; } 
    };
    
    public string selection;
    public string Selection
    {
        get =&gt; selection;
        set
        {
            selection = value;
            OnPropertyChanged(nameof(Selection));
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

huangapple
  • 本文由 发表于 2023年3月9日 19:52:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75684237.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定