현재 Android의 공유 환경 설정 저장소에 문자열 목록을 저장하려고합니다. 공유 환경 설정을 사용하므로 Android 자체의 MultiSelectListPreference를 사용할 수 있습니다. 값을 저장하는 것은 문제가 아니지만 어쩌면 그 원인 일 수 있습니다. 값을 읽으려고하면 문제 자체가 시작됩니다. 바로 그 순간에 Android는 Xamarin 코드에 ArrayList를 제공하고 Xamarin은 이것을 C# List로 변환해야합니다. 그리고 다음 InvalidCastException가 발생합니다.Xamarin MultiSelectListPreference InvalidCastException
[MonoDroid] UNHANDLED EXCEPTION:
[MonoDroid] System.InvalidCastException: Cannot cast from source type to destination type.
[MonoDroid] at (wrapper castclass) object.__castclass_with_cache (object,intptr,intptr) <0x00068>
[MonoDroid] at Android.Runtime.JavaSet`1<string>.FromJniHandle (intptr,Android.Runtime.JniHandleOwnership) <0x0008f>
[MonoDroid] at Android.Content.ISharedPreferencesInvoker.GetStringSet (string,System.Collections.Generic.ICollection`1<string>) <0x0015b>
[MonoDroid] at canvastix.MultiSelectListPreference.OnDialogClosed (bool) <0x0009f>
[MonoDroid] at Android.Preferences.DialogPreference.n_OnDialogClosed_Z (intptr,intptr,bool) <0x0003f>
[MonoDroid] at (wrapper dynamic-method) object.4bc48339-d8f3-443a-a584-0088f55fe375 (intptr,intptr,bool) <0x00043>
[mono]
[mono] Unhandled Exception:
[mono] System.InvalidCastException: Cannot cast from source type to destination type.
[mono] at (wrapper castclass) object:__castclass_with_cache (object,intptr,intptr)
[mono] at Android.Runtime.JavaSet`1[System.String].FromJniHandle (IntPtr handle, JniHandleOwnership transfer) [0x00000] in <filename unknown>:0
[mono] at Android.Content.ISharedPreferencesInvoker.GetStringSet (System.String key, ICollection`1 defValues) [0x00000] in <filename unknown>:0
[mono] at canvastix.MultiSelectListPreference.OnDialogClosed (Boolean positiveResult) [0x00000] in <filename unknown>:0
[mono] at Android.Preferences.DialogPreference.n_OnDialogClosed_Z (IntPtr jnienv, IntPtr native__this, Boolean positiveResult) [0x00000] in <filename unknown>:0
[mono] at (wrapper dynamic-method) object:4bc48339-d8f3-443a-a584-0088f55fe375 (intptr,intptr,bool)
[mono-rt] [ERROR] FATAL UNHANDLED EXCEPTION: System.InvalidCastException: Cannot cast from source type to destination type.
[mono-rt] at (wrapper castclass) object:__castclass_with_cache (object,intptr,intptr)
[mono-rt] at Android.Runtime.JavaSet`1[System.String].FromJniHandle (IntPtr handle, JniHandleOwnership transfer) [0x00000] in <filename unknown>:0
[mono-rt] at Android.Content.ISharedPreferencesInvoker.GetStringSet (System.String key, ICollection`1 defValues) [0x00000] in <filename unknown>:0
[mono-rt] at canvastix.MultiSelectListPreference.OnDialogClosed (Boolean positiveResult) [0x00000] in <filename unknown>:0
[mono-rt] at Android.Preferences.DialogPreference.n_OnDialogClosed_Z (IntPtr jnienv, IntPtr native__this, Boolean positiveResult) [0x00000] in <filename unknown>:0
[mono-rt] at (wrapper dynamic-method) object:4bc48339-d8f3-443a-a584-0088f55fe375 (intptr,intptr,bool)
다음 코드를 사용하여 활동 값을 읽습니다. 특별한 아무것도 :
Button button = FindViewById<Button> (Resource.Id.button);
button.Click += delegate {
//Read value
ISharedPreferences Prefs = PreferenceManager.GetDefaultSharedPreferences (this);
ICollection<String> list = Prefs.GetStringSet("list", new List<String>());
Toast.MakeText(this, "Total items: " + list.Count, ToastLength.Short).Show();
};
나는 조각에 MultiSelectListPreference을 보여주기 위해 다음 코드를 사용하지 :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment
class="XamarinBugReadStringsetPreference.MainFragment"
android:id="@+id/fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Read and Show" />
</LinearLayout>
그리고 마지막으로 :
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<MultiSelectListPreference
android:key="list"
android:entries="@array/Entries"
android:entryValues="@array/Values" />
</PreferenceScreen>
이는 활동의 레이아웃입니다 , 목록의 값은 다음과 같습니다.
<?xml version="1.0" encoding="UTF-8" ?>
<resources>
<string-array name="Entries">
<item>1</item>
<item>2</item>
</string-array>
<string-array name="Values">
<item>Item 1</item>
<item>Item 2</item>
</string-array>
</resources>
부담없이 사용해보세요. 매니페스트에서 minSdkVersion을 15로 설정하고 targetSdkVersion을 19로 설정했습니다.
Xamarin의 버그 일 것입니다. 그래서 만약 당신이 해결 방법을 찾았 으면 좋을 것입니다.
String-Collection을 공유 환경 설정에 저장할 수 없으며 String, int, boolean과 같은 기본 유형 만 저장할 수 있습니다. –
콜렉션을 StringBuilder와 같은 일부 문자열로 변환 해보십시오. builder = new StringBuilder(); (String s : arr) { builder.append (s); } return builder.toString(); –
Xamarin은 C# 문자열 목록을 Java 문자열 목록에 저장할 수 있어야합니다. 문제는 MultiSelectListPreference가 저장 및 검색 메서드를 처리하므로 거의 모든 단일 메서드를이 작업을 위해 덮어 써야한다는 것입니다. 확장 된 클래스를 깨뜨릴 수있는 Xamarin의 향후 업데이트에 대해서는 언급하지 않습니다. 차라리 표준 MultiSelectListPreference를 사용하려고합니다. – Wezelkrozum