我在这里跟踪片段示例https://github.com/MvvmCross/MvvmCross-AndroidSupport。我试图通过在片段上设置IsCacheableFragment属性来创建不可缓存的片段。但是,当我这样做时,ViewModel永远不会被设置。
例如:
[MvxFragment(typeof(MainViewModel), Resource.Id.content_frame, true, IsCacheableFragment =false)]
[Register("example.droid.fragments.ExampleRecyclerViewFragment")]
public class ExampleRecyclerViewFragment : BaseFragment<ExampleRecyclerViewModel>
{...
将在OnCreate中抛出错误,因为ViewModel为null
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var view = base.OnCreateView(inflater, container, savedInstanceState);
var recyclerView = view.FindViewById<MvxRecyclerView>(Resource.Id.my_recycler_view);
if (recyclerView != null)
{
recyclerView.HasFixedSize = true;
var layoutManager = new LinearLayoutManager(Activity);
recyclerView.SetLayoutManager(layoutManager);
}
//ViewModel is null
_itemSelectedToken = ViewModel.WeakSubscribe(() => ViewModel.SelectedItem,
(sender, args) => {
if (ViewModel.SelectedItem != null)
Toast.MakeText(Activity,
$"Selected: {ViewModel.SelectedItem.Title}",
ToastLength.Short).Show();
});
....
我通过做类似于MvxCachingFragmentCompatActivity refresh cached fragment的事情解决了我的问题。
片段上的属性是否应该按照我的意图执行,还是用于其他内容?