Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 创建多个片段以匹配ViewModel中对象的数量_C#_Mvvm_Xamarin - Fatal编程技术网

C# 创建多个片段以匹配ViewModel中对象的数量

C# 创建多个片段以匹配ViewModel中对象的数量,c#,mvvm,xamarin,C#,Mvvm,Xamarin,我在viewmodel中有五个对象,如下所示,我希望有相同数量的片段。目前,我已经硬编码了五个片段 是否有一种方法可以从viewmodel中获取对象的数量,并将其传递给视图,使其模块化,而不是硬编码。我使用的是mvvm模式 视图模型类 public RecyclerViewModel() { Items = new ObservableCollection<ListItem> { new ListItem { Title = "A" }, n

我在viewmodel中有五个对象,如下所示,我希望有相同数量的片段。目前,我已经硬编码了五个片段

是否有一种方法可以从viewmodel中获取对象的数量,并将其传递给视图,使其模块化,而不是硬编码。我使用的是mvvm模式

视图模型类

public RecyclerViewModel()
 {
    Items = new ObservableCollection<ListItem> {
        new ListItem { Title = "A" },
        new ListItem { Title = "B" },
        new ListItem { Title = "C" },
        new ListItem { Title = "D" },
        new ListItem { Title = "E" }
    };
 }
var viewPager = view.FindViewById<ViewPager>(Resource.Id.viewpager);
if (viewPager != null)
 {
    var fragments = new List<MvxFragmentPagerAdapter.FragmentInfo>
    {
      // hard coded
      new MvxFragmentPagerAdapter.FragmentInfo("RecyclerView 1", typeof (RecyclerViewFragment),typeof (RecyclerViewModel)),
      new MvxFragmentPagerAdapter.FragmentInfo("RecyclerView 2", typeof (RecyclerViewFragment),typeof (RecyclerViewModel)),
      new MvxFragmentPagerAdapter.FragmentInfo("RecyclerView 3", typeof (RecyclerViewFragment),typeof (RecyclerViewModel)),
      new MvxFragmentPagerAdapter.FragmentInfo("RecyclerView 4", typeof (RecyclerViewFragment),typeof (RecyclerViewModel)),
      new MvxFragmentPagerAdapter.FragmentInfo("RecyclerView 5", typeof (RecyclerViewFragment), typeof (RecyclerViewModel))
     };
   viewPager.Adapter = new MvxFragmentPagerAdapter(Activity, ChildFragmentManager, fragments);
 }
公共回收服务模型() { Items=新的ObservableCollection{ 新列表项{Title=“A”}, 新列表项{Title=“B”}, 新列表项{Title=“C”}, 新列表项{Title=“D”}, 新列表项{Title=“E”} }; } 查看类

public RecyclerViewModel()
 {
    Items = new ObservableCollection<ListItem> {
        new ListItem { Title = "A" },
        new ListItem { Title = "B" },
        new ListItem { Title = "C" },
        new ListItem { Title = "D" },
        new ListItem { Title = "E" }
    };
 }
var viewPager = view.FindViewById<ViewPager>(Resource.Id.viewpager);
if (viewPager != null)
 {
    var fragments = new List<MvxFragmentPagerAdapter.FragmentInfo>
    {
      // hard coded
      new MvxFragmentPagerAdapter.FragmentInfo("RecyclerView 1", typeof (RecyclerViewFragment),typeof (RecyclerViewModel)),
      new MvxFragmentPagerAdapter.FragmentInfo("RecyclerView 2", typeof (RecyclerViewFragment),typeof (RecyclerViewModel)),
      new MvxFragmentPagerAdapter.FragmentInfo("RecyclerView 3", typeof (RecyclerViewFragment),typeof (RecyclerViewModel)),
      new MvxFragmentPagerAdapter.FragmentInfo("RecyclerView 4", typeof (RecyclerViewFragment),typeof (RecyclerViewModel)),
      new MvxFragmentPagerAdapter.FragmentInfo("RecyclerView 5", typeof (RecyclerViewFragment), typeof (RecyclerViewModel))
     };
   viewPager.Adapter = new MvxFragmentPagerAdapter(Activity, ChildFragmentManager, fragments);
 }
var viewPager=view.findviewbyd(Resource.Id.viewPager);
if(viewPager!=null)
{
var fragments=新列表
{
//硬编码
新的MvxFragmentPagerAdapter.FragmentInfo(“RecyclerView 1”,typeof(RecyclerServicewFragment),typeof(RecyclerServicewModel)),
新的MvxFragmentPagerAdapter.FragmentInfo(“RecyclerView 2”,typeof(RecyclerServicewFragment),typeof(RecyclerServicewModel)),
新的MvxFragmentPagerAdapter.FragmentInfo(“RecyclerView 3”,typeof(RecyclerServicewFragment),typeof(RecyclerServicewModel)),
新的MvxFragmentPagerAdapter.FragmentInfo(“RecyclerView 4”,typeof(RecyclerServicewFragment),typeof(RecyclerServicewModel)),
新的MvxFragmentPagerAdapter.FragmentInfo(“RecyclerView 5”,typeof(RecyclerServicewFragment),typeof(RecyclerServicewModel))
};
Adapter=新的MvxFragmentPagerAdapter(活动、子片段管理器、片段);
}
因此,在构造函数中:

Items = new ObservableCollection<ListItem> {
                new ListItem { StaticClass.a[StaticClass.index < StaticClass.a.Length ? StaticClass.Index : StaticClass.a.Length -1] }
            };
StaticClass.Index++;
Items=新的ObservableCollection{
新的ListItem{StaticClass.a[StaticClass.index
以及:

var片段=新列表();
对于(int i=0;i
就像我之前说的,比赛条件可能会导致问题,但要测试一下:)

因此,在构造函数中:

Items = new ObservableCollection<ListItem> {
                new ListItem { StaticClass.a[StaticClass.index < StaticClass.a.Length ? StaticClass.Index : StaticClass.a.Length -1] }
            };
StaticClass.Index++;
Items=新的ObservableCollection{
新的ListItem{StaticClass.a[StaticClass.index
以及:

var片段=新列表();
对于(int i=0;i

正如我之前所说,比赛条件可能会导致问题,但请测试它:)

我对选项卡做了类似的操作,选项卡数量动态变化,下面是代码转储:

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:keepScreenOn="true">
<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <include
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        layout="@layout/merge_header" />    
    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</LinearLayout>

视图:

[活动(ParentActivity=typeof(HomeView),Theme=“@style/Theme.App”,ConfigurationChanges=Android.Content.PM.ConfigChanges.Orientation | Android.Content.PM.ConfigChanges.ScreenSize)]
公共类计划视图:BaseTabActivity
{
创建时受保护的覆盖无效(捆绑包)
{
base.OnCreate(bundle);
SetContentView(资源.布局.页面规划);
ViewModel.BuildInterface();
CreateTabs();
}
void CreateTabs()
{
TabHost.TabSpec;
spec=TabHost.NewTabSpec(ViewModel.TextSource.GetText(LocalizationConstants.Planning_Stats));
spec.SetIndicator(ViewModel.TextSource.GetText(LocalizationConstants.Planning_Stats));
var intent=new intent(this.CreateIntentFor(ViewModel.PlanningStats));
spec.SetContent(intent.AddFlags(ActivityFlags.ClearTop));
TabHost.AddTab(spec);
var currentGameweek=ViewModel.CurrentGW;
foreach(ViewModel.planningGW中的var planningGW)
{
spec=TabHost.NewTabSpec(string.Format(“{0}{1}”,ViewModel.SharedTextSource.GetText(LocalizationConstants.Shared_GW),planningGW.Gameweek));
spec.SetIndicator(string.Format(“{0}{1}”、ViewModel.SharedTextSource.GetText(LocalizationConstants.Shared_GW)、planningGW.Gameweek));
intent=新的intent(this.CreateIntentFor(planningGW));
spec.SetContent(intent.AddFlags(ActivityFlags.ClearTop));
TabHost.AddTab(spec);
}
}
}
要生成界面的viewmodel:

PlanningStatsViewModel _planningStats;
    public PlanningStatsViewModel PlanningStats
    {
        get { return _planningStats; }
        set { _planningStats = value; RaisePropertyChanged(() => PlanningStats); }
    }

    ObservableCollection<PlanningGWViewModel> _planningGW;
    public ObservableCollection<PlanningGWViewModel> PlanningGW
    {
        get { return _planningGW; }
        set { _planningGW = value; RaisePropertyChanged(() => PlanningGW); }
    }

public void BuildInterface()
    {
        PlanningStats = new PlanningStatsViewModel(_dataService, _loadingService, _messenger, _messageService);
        PlanningGW = new ObservableCollection<PlanningGWViewModel>();

        var nonPlayedGameweeks = Service.GetCacheNonPlayedGameweeksAsync().Result;
        var query = nonPlayedGameweeks.ToList();

        if (query.Any())
        {
            CurrentGW = query.Min(f => f.GameWeek);

            var grouped = query.ToList().GroupBy(f => f.GameWeek, f => f, (key, g) => new { Gameweek = key, Fixtures = g.ToList() });

            foreach (var grp in grouped.Take(SettingsPreferences.SelectedPlanUpcoming))
                if (grp.Gameweek <= BusinessConstants.NumGamesSeason)
                    PlanningGW.Add(new PlanningGWViewModel(grp.Gameweek, _dataService, _loadingService, _messenger, _messageService));
        }
    }
PlanningStatsViewModel\u planningStats;
公共规划状态视图模型规划状态
{
获取{return\u planningStats;}
设置{u planningStats=value;RaisePropertyChanged(()=>planningStats);}
}
可观测采集计划GW;
公共可观测收集规划GW
{
获取{return\u planningGW;}
设置{u planningGW=value;RaisePropertyChanged(()=>planningGW);}
}
公共void BuildInterface()
{
PlanningStats=新的PlanningStatsViewModel(\u数据服务、\u加载服务、\u信使、\u消息服务);
PlanningGW=新的可观测集合();
var nonPlayedGameweeks=Service.GetCacheNonPlayedGameweeksAsync().Result;
var query=nonPlayedGameweeks.ToList();
if(query.Any())
{
CurrentGW=query.Min(f=>f.GameWeek);
var grouped=query.ToList().GroupBy(f=>f.GameWeek,f=>f,(key,g)=>new{GameWeek=key,Fixtures=g.ToList()});
foreach(group.Take中的var grp(setingsReferences.selectedPlanUncoming))
if(grp.Gameweek PlanningGWItems);}
}
公共异步任务重建列表(bool显示加载)
{
IsLoading=显示加载;
ObservableCollection<PlanningGWItemsViewModelWrapper> _planningGWItems;
    public ObservableCollection<PlanningGWItemsViewModelWrapper> PlanningGWItems
    {
        get { return _planningGWItems; }
        set { _planningGWItems = value; RaisePropertyChanged(() => PlanningGWItems); }
    }

public async Task RebuildLists(bool displayLoading)
    {
        IsLoading = displayLoading;

        Standings = await Service.GetCacheStandingsAsync().ConfigureAwait(false);

        var nonPlayedGameweeks = await Service.GetCacheNonPlayedGameweeksAsync().ConfigureAwait(false);

        PlanningGWItems = await BuildList(Standings, nonPlayedGameweeks.Where(f => f.GameWeek == _gameweek).OrderBy(f => f.Date).ToList()).ConfigureAwait(false);

        IsLoading = false;
    }