C# TableLayout中的片段不绑定到viewmodel
我已经为此挣扎了两天了,我只是被卡住了。由于某种原因,片段的绑定根本不会生效。页面显示正确,选项卡工作正常。我可以从选项卡1滑动到选项卡2,反之亦然。TextView应该显示viewmodel中的一些文本。调试时,两个片段ViewModel的构造函数都会执行,但LoginNotificationViewModel(LoginDescription和LastLoginRequestReceivedOn)上的属性永远不会触发 我有一个视图,其中包含一个表格布局,如下所示:C# TableLayout中的片段不绑定到viewmodel,c#,android,mvvmcross,android-tablayout,C#,Android,Mvvmcross,Android Tablayout,我已经为此挣扎了两天了,我只是被卡住了。由于某种原因,片段的绑定根本不会生效。页面显示正确,选项卡工作正常。我可以从选项卡1滑动到选项卡2,反之亦然。TextView应该显示viewmodel中的一些文本。调试时,两个片段ViewModel的构造函数都会执行,但LoginNotificationViewModel(LoginDescription和LastLoginRequestReceivedOn)上的属性永远不会触发 我有一个视图,其中包含一个表格布局,如下所示: <?xml vers
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<include
layout="@layout/header" />
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/main_content"
android:layout_below="@id/header">
<android.support.design.widget.TabLayout
android:id="@+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
local:tabMode="fixed" />
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"
android:background="@android:color/white" />
</LinearLayout>
</RelativeLayout>
其中一个片段涉及:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="30dp"
local:MvxBind="Text LoginDescription" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="30dp"
local:MvxBind="Text LastLoginRequestReceivedOn"/>
</LinearLayout>
您需要使用
bindingfillate()
而不是默认的Android充气器,因为它不知道如何处理MvxBind属性
使用MvvmCross.Binding.Droid.BindingContext;
...
创建视图上的公共覆盖视图(布局、充气机、视图组容器、捆绑包保存状态)
{
忽略变量=base.OnCreateView(充气机、容器、savedInstanceState);
var view=this.BindingInflate(Resource.Layout.MyFragmentLayout,container,false);
返回视图;
}
啊,我以前在某个地方的评论中看到过这一点。我没有在这个想法上找到它,也没有意识到它甚至是一种扩展方法。当我添加代码段时,我得到了一个NullReferenceException,似乎我还需要base.OnCreateView,因为它现在可以工作了。非常感谢你
namespace Notifier.ViewModels
{
public class HomeNotificationViewModel : ViewModelBase
{
public override string SubTitle => "Notificatie";
public override void InitView()
{
}
public override void InitData()
{
// TODO: Init Data
}
public override void ApplicationSelected()
{
}
public void ResetPincode()
{
DeviceRegistrationHelper.UpdatePincode(null, () =>
{
DetermineAndStartHomeView();
});
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="30dp"
local:MvxBind="Text LoginDescription" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="30dp"
local:MvxBind="Text LastLoginRequestReceivedOn"/>
</LinearLayout>
using Android.OS;
using Android.Views;
using Notifier.ViewModels;
namespace Notifier.Android.Fragments
{
public class LoginNotificationFragment : BaseFragment<LoginNotificationViewModel>
{
protected override int LayoutResource => Resource.Layout.LoginNotificationFragment;
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return inflater.Inflate(Resource.Layout.LoginNotificationFragment, container, false);
}
}
}
using MvvmCross.Droid.Support.V4;
using Notifier.Android.Views;
using Notifier.ViewModels;
namespace Notifier.Android.Fragments
{
public abstract class BaseFragment<TViewModel> : MvxFragment<TViewModel>
where TViewModel : ViewModelBase
{
protected abstract int LayoutResource { get; }
}
}
using Notifier.Classes;
using System;
using System.Linq;
namespace Notifier.ViewModels
{
public class LoginNotificationViewModel : ViewModelBase
{
public override string SubTitle => "Inlog notificatie";
public String Status { get; set; }
public override void InitView()
{
}
public override void InitData()
{
// TODO: Init Data
}
public override void ApplicationSelected()
{
}
public string LoginDescription
{
get => Settings.HasNotification
? string.Format("Er is een inlog verzoek ontvangen voor {0}", ApplicationShortName(Settings.Notification.ApplicationId))
: "Geen openstaande inlogverzoeken";
}
/// <summary>
/// Short name for the application
/// </summary>
private string ApplicationShortName(int applicationId)
{
return Applications.Where(app => app.ApplicationID == applicationId).FirstOrDefault()?.ApplicationEntity.ApplicationShortName;
}
public string LastLoginRequestReceivedOn
{
get => string.Format("Laatste inlogverzoek: {0}",
Settings.HasLastNotificationReceivedOn
? string.Empty
: Settings.LastNotificationReceivedOn.ToString("DD-MM-YYYY HH:MM"));
}
}
}
namespace Notifier.ViewModels
{
/// <summary>
/// Base class for all view models.
/// </summary>
public abstract class ViewModelBase : MvxViewModel
{
private readonly IPlatformEntrance _platformEntrance;
private readonly IPlatformLog _platformLog;
public string Title => "nNotifier©";
public ViewModelBase()
{
_platformEntrance = Mvx.Resolve<IPlatformEntrance>();
_platformLog = Mvx.Resolve<IPlatformLog>();
InitData();
}
public abstract void InitData();
public abstract string SubTitle { get; }
...
}
}
using Android.Content;
using Android.Support.V4.App;
using MvvmCross.Droid.Support.V4;
namespace Notifier.Adapters
{
public class TabsFragmentPagerAdapter : MvxCachingFragmentStatePagerAdapter
{
FragmentInfo[] _fragments;
public TabsFragmentPagerAdapter(Context context, FragmentManager fragmentManager, FragmentInfo[] fragments)
: base(context, fragmentManager, fragments)
{
_fragments = fragments;
}
// public override int Count => _fragments.Length;
//public override global::Android.Support.V4.App.Fragment GetItem(int position,
// global::Android.Support.V4.App.Fragment.SavedState fragmentSavedState = null)
//{
// return _fragments[position];
//}
//public override ICharSequence GetPageTitleFormatted(int position)
//{
// return _titles[position];
//}
}
}