任何MvxPreferenceFragment示例代码?

时间:2016-05-17 07:29:23

标签: android xamarin mvvmcross preferences

似乎无法让我的设置在Android上运行。是否有示例显示如何使用MvxPreferenceFragment?我大多错过了一种展示Fragment的方法,就像MvxPreferenceActivity一样。

可悲的是,Android的示例不使用它。

https://github.com/MvvmCross/MvvmCross-AndroidSupport/blob/master/Samples/Example.Droid/Resources/layout/fragment_settings.axml

我确实尝试过“正常”活动(MvxCachingFragmentCompatActivity),但应用程序崩溃了。

我的片段代码:

[MvxFragment(typeof(SettingsViewModel), Resource.Id.flContent, true)]
[Register("client.android.fragments.SettingsFragment")]
public class SettingsFragment : MvxPreferenceFragment<SettingsSyncViewModel>
{
    public override void OnCreatePreferences(Bundle p0, string p1)
    {
        // Load the preferences from an XML resource
        AddPreferencesFromResource(Resource.Xml.preferences);
    }
}

的preferences.xml:

<?xml version="1.0" encoding="utf-8" ?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
  <PreferenceCategory
        android:title="Category Title"
        android:key="pref_key_storage_settings">
    <CheckBoxPreference
        android:key="pref_sync"
        android:title="CheckboxStuff"
        android:summary="Checkbox..."
        android:defaultValue="true" />
    <Preference
                android:key="pref_key_limit"
                android:summary="Stuff ..."
                android:title="Hello World" />
  </PreferenceCategory>
</PreferenceScreen>

查看模型为空。只是想让它显示出来。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

我认为这是我们应该在演示者中解决的问题。我目前所做的是在Android项目中打开它:

//ViewModel is loaded here to support popup fragments
private PreviewViewModel previewViewModel;
public PreviewViewModel ViewModel => previewViewModel = previewViewModel ?? Mvx.IocConstruct<PreviewViewModel>();


public void ShowDetail()
{
   var activity = Mvx.Resolve<IMvxAndroidCurrentTopActivity>().Activity as MvxCachingFragmentCompatActivity;
   if (activity != null)
   {
     var dialog = new PreviewDialogFragment() { DataContext = ViewModel };
     dialog.Show(activity.SupportFragmentManager, "preview");
   }
}