ValueConverters in plataform projects - c#

I want create a classic BooleanToVisibilityConverter for android, I follow this https://github.com/slodge/NPlus1DaysOfMvvmCross/tree/master/N-04-ValueConverters, but in this sample all the converters are in Core project. I create my converter in Droid project and doesn't work.
My ValueConverter code:
public class BoolToVisibilityValueConverter : MvxValueConverter<bool, ViewStates>
{
protected override ViewStates Convert(bool value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == true)
{
return ViewStates.Visible;
}
else
{
return ViewStates.Gone;
}
}
protected override bool ConvertBack(ViewStates value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == ViewStates.Gone)
{
return false;
}
else if (value == ViewStates.Invisible)
{
return false;
}
else
{
return true;
}
}
}
And my layout where I use this converter.
<?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:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
local:MvxBind="Text Message" />
<ProgressBar
android:layout_width="100dp"
android:layout_height="100dp"
android:indeterminate="true"
android:layout_gravity="bottom|center_horizontal"
local:MvxBind="Visibility ProgressBarVisibility,Converter=BoolToVisibility" />
</LinearLayout>
Thanks!
Edit:
Debug trace.
09-26 16:11:34.300 I/mono-stdout(18362): Parameter name: value
09-26 16:11:34.300 I/mono-stdout(18362): at (wrapper managed-to-native) System.Enum:ToObject (System.Type,object)
[0:] MvxBind:Error: 99.25 Problem seen during binding execution for to Visibility - problem ArgumentException: The value passed in must be an enum base or an underlying type for an enum, such as an Int32.
Parameter name: value
at (wrapper managed-to-native) System.Enum:ToObject (System.Type,object)
at Cirrious.MvvmCross.Binding.ExtensionMethods.MvxTypeExtensions.MakeSafeValue (System.Type propertyType, System.Object value) [0x00000] in :0
at Cirrious.MvvmCross.Binding.Bindings.Target.MvxPropertyInfoTargetBinding.MakeSafeValue (System.Object value) [0x00000] in :0
at Cirrious.MvvmCross.Binding.Bindings.Target.MvxPropertyInfoTargetBinding.SetValue (System.Object value) [0x00000] in :0
at Cirrious.MvvmCross.Binding.Bindings.MvxFullBinding.UpdateTargetFromSource (Boolean isAvailable, System.Object value) [0x00000] in :0
09-26 16:11:34.300 I/mono-stdout(18362): at Cirrious.MvvmCross.Binding.ExtensionMethods.MvxTypeExtensions.MakeSafeValue (System.Type propertyType, System.Object value) [0x00000] in :0
09-26 16:11:34.300 I/mono-stdout(18362): at Cirrious.MvvmCross.Binding.Bindings.Target.MvxPropertyInfoTargetBinding.MakeSafeValue (System.Object value) [0x00000] in :0
09-26 16:11:34.300 I/mono-stdout(18362): at Cirrious.MvvmCross.Binding.Bindings.Target.MvxPropertyInfoTargetBinding.SetValue (System.Object value) [0x00000] in :0
09-26 16:11:34.300 I/mono-stdout(18362): at Cirrious.MvvmCross.Binding.Bindings.MvxFullBinding.UpdateTargetFromSource (Boolean isAvailable, System.Object value) [0x00000] in :0
El programa 'Mono' terminó con código 0 (0x0).

You will need to register that ValueConverter in your Setup.cs file, by overriding ValueConverterAssemblies.
protected override List<Assembly> ValueConverterAssemblies
{
get
{
var toReturn = base.ValueConverterAssemblies;
toReturn.Add(typeof(BoolToVisibilityValueConverter).Assembly);
return toReturn;
}
}
You can read more about it here: https://github.com/slodge/MvvmCross/wiki/Value-Converters#referencing-value-converters-in-touch-and-droid

Related

Observable Collection crashes while updating values

Not able to replace entire observable collection. As it throws exception.
How do I replace entire observable collection item? The suggested answers from other threads didn't work. Tried the following. Any suggestion would help on this.
Xaml
<Picker
x:Name=“Picker1”
Style="{StaticResource PickerStyle}"
ItemsSource="{Binding CodeNumberList}"
SelectedItem="{Binding CodeNumberText}”>
<Picker.Behaviors>
<xct:EventToCommandBehavior
EventName="SelectedIndexChanged"
Command="{Binding ItemChangedCommand}"
CommandParameter="{Binding Source={x:Reference Picker1},
Path=SelectedItem}"/>
</Picker.Behaviors>
</Picker>
<Picker
x:Name=“Picker2”
Style="{StaticResource PickerStyle}"
ItemsSource="{Binding PhoneNumberList}"
SelectedItem="{Binding PhoneNumberText}”>
</Picker>
ViewModel
private ObservableCollection<string> _phumberList;
public ObservableCollection<string> PhNumberList
{
get
{
return _phumberList;
}
set
{
_phumberList = value;
RaisePropertyChanged(nameof(this.PhNumberList));
}
}
private void ItemChangedCommandHandler(string obj)
{
PopulatePhoneList();
}
private void PopulatePhoneList()
{
// tried clearing PhNumberList.Clear() and then added items, that didn't work too
//Invalid cast exception thrown in below line
PhNumberList = new ObservableCollection<string>();
foreach (var item in CurrentUser?.InformationDetailItems)
{
PhNumberList.Add (item.PhoneNumber);
}
}
public On NavigatedTo(){
// populate default pickerList
PopulatePhoneList();
}
Exception :
//Invalid cast exception thrown in below line
PhNumberList = new ObservableCollection();
Stack Trace
at (wrapper castclass) System.Object.__castclass_with_cache(object,intptr,intptr)
at Prism.Commands.DelegateCommand`1[T].CanExecute (System.Object parameter) [0x00000] in d:\a\1\s\Source\Prism\Commands\DelegateCommand{T}.cs:113
at Prism.Commands.DelegateCommandBase.System.Windows.Input.ICommand.CanExecute (System.Object parameter) [0x00000] in d:\a\1\s\Source\Prism\Commands\DelegateCommandBase.cs:67
at Xamarin.CommunityToolkit.Behaviors.EventToCommandBehavior.OnTriggerHandled (System.Object sender, System.Object eventArgs) [0x00039] in <823f4019dc404dea9e9331c7114e4863>:0
at Xamarin.Forms.Picker.OnSelectedIndexChanged (System.Object bindable, System.Object oldValue, System.Object newValue) [0x00012] in D:\a\1\s\Xamarin.Forms.Core\Picker.cs:283
at Xamarin.Forms.BindableObject.SetValueActual (Xamarin.Forms.BindableProperty property, Xamarin.Forms.BindableObject+BindablePropertyContext context, System.Object value, System.Boolean currentlyApplying, Xamarin.Forms.Internals.SetValueFlags attributes, System.Boolean silent) [0x00120] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:512
at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty property, System.Object value, Xamarin.Forms.Internals.SetValueFlags attributes, Xamarin.Forms.BindableObject+SetValuePrivateFlags privateAttributes) [0x00173] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:446
at Xamarin.Forms.BindableObject.SetValue (Xamarin.Forms.BindableProperty property, System.Object value, System.Boolean fromStyle, System.Boolean checkAccess) [0x0004d] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:374
at Xamarin.Forms.BindableObject.SetValue (Xamarin.Forms.BindableProperty property, System.Object value) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:349
at Xamarin.Forms.Picker.set_SelectedIndex (System.Int32 value) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\Picker.cs:114
at Xamarin.Forms.Picker.OnItemsCollectionChanged (System.Object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) [0x00007] in D:\a\1\s\Xamarin.Forms.Core\Picker.cs:205
at (wrapper delegate-invoke) <Module>.invoke_void_object_NotifyCollectionChangedEventArgs(object,System.Collections.Specialized.NotifyCollectionChangedEventArgs)
at System.Collections.ObjectModel.ObservableCollection`1[T].OnCollectionChanged (System.Collections.Specialized.NotifyCollectionChangedEventArgs e) [0x00018] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/external/corefx/src/System.ObjectModel/src/System/Collections/ObjectModel/ObservableCollection.cs:263
at System.Collections.ObjectModel.ObservableCollection`1[T].OnCollectionReset () [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/external/corefx/src/System.ObjectModel/src/System/Collections/ObjectModel/ObservableCollection.cs:362
at System.Collections.ObjectModel.ObservableCollection`1[T].ClearItems () [0x00018] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/external/corefx/src/System.ObjectModel/src/System/Collections/ObjectModel/ObservableCollection.cs:166
at System.Collections.ObjectModel.Collection`1[T].Clear () [0x00014] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/external/corefx/src/Common/src/CoreLib/System/Collections/ObjectModel/Collection.cs:81
at Xamarin.Forms.Internals.LockableObservableListWrapper.InternalClear () [0x00000] in D:\a\1\s\Xamarin.Forms.Core\LockableObservableListWrapper.cs:71
at Xamarin.Forms.Picker.ResetItems () [0x00009] in D:\a\1\s\Xamarin.Forms.Core\Picker.cs:273
at Xamarin.Forms.Picker.OnItemsSourceChanged (System.Collections.IList oldValue, System.Collections.IList newValue) [0x0004c] in D:\a\1\s\Xamarin.Forms.Core\Picker.cs:231
at Xamarin.Forms.Picker.OnItemsSourceChanged (Xamarin.Forms.BindableObject bindable, System.Object oldValue, System.Object newValue) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\Picker.cs:213
at Xamarin.Forms.BindableObject.SetValueActual (Xamarin.Forms.BindableProperty property, Xamarin.Forms.BindableObject+BindablePropertyContext context, System.Object value, System.Boolean currentlyApplying, Xamarin.Forms.Internals.SetValueFlags attributes, System.Boolean silent) [0x00120] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:512
at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty property, System.Object value, Xamarin.Forms.Internals.SetValueFlags attributes, Xamarin.Forms.BindableObject+SetValuePrivateFlags privateAttributes) [0x00173] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:446
at Xamarin.Forms.BindingExpression.ApplyCore (System.Object sourceObject, Xamarin.Forms.BindableObject target, Xamarin.Forms.BindableProperty property, System.Boolean fromTarget) [0x00226] in D:\a\1\s\Xamarin.Forms.Core\BindingExpression.cs:160
at Xamarin.Forms.BindingExpression.Apply (System.Boolean fromTarget) [0x0003e] in D:\a\1\s\Xamarin.Forms.Core\BindingExpression.cs:56
at Xamarin.Forms.BindingExpression+BindingExpressionPart.<PropertyChanged>b__49_0 () [0x00000] in D:\a\1\s\Xamarin.Forms.Core\BindingExpression.cs:762
at Xamarin.Forms.BindingExpression+BindingExpressionPart.PropertyChanged (System.Object sender, System.ComponentModel.PropertyChangedEventArgs args) [0x000cb] in D:\a\1\s\Xamarin.Forms.Core\BindingExpression.cs:773
at Xamarin.Forms.BindingExpression+WeakPropertyChangedProxy.OnPropertyChanged (System.Object sender, System.ComponentModel.PropertyChangedEventArgs e) [0x00012] in D:\a\1\s\Xamarin.Forms.Core\BindingExpression.cs:666
at (wrapper delegate-invoke) <Module>.invoke_void_object_PropertyChangedEventArgs(object,System.ComponentModel.PropertyChangedEventArgs)
at Prism.Mvvm.BindableBase.OnPropertyChanged (System.ComponentModel.PropertyChangedEventArgs args) [0x00000] in d:\a\1\s\Source\Prism\Mvvm\BindableBase.cs:99
at Prism.Mvvm.BindableBase.OnPropertyChanged (System.String propertyName) [0x00000] in d:\a\1\s\Source\Prism\Mvvm\BindableBase.cs:90
at Prism.Mvvm.BindableBase.RaisePropertyChanged (System.String propertyName) [0x00000] in d:\a\1\s\Source\Prism\Mvvm\BindableBase.cs:76
-->at ProjectMobile.ViewModels.DarFormPageViewModel.set_EquipmentNumberList (System.Collections.ObjectModel.ObservableCollection`1[T] value) [0x00008] in /Users/vpo1/Project-Mobile/MobileApp/ProjectMobile.ViewModels/PhFormPageViewModel:104 <--
at ProjectMobile.ViewModels.DarFormPageViewModel.PopulateEquipmentList (System.Boolean IsFromNavigatedTo) [0x00013] in /Users/vpo1/Project-Mobile/MobileApp/ProjectMobile.ViewModels/PhFormPageViewModel:940
at ProjectMobile.ViewModels.DarFormPageViewModel.DeliveryItemChangedCommandHandler (System.String obj) [0x00008] in /Users/vpo1/Project-Mobile/MobileApp/ProjectMobile.ViewModels/PhFormPageViewModel:61
at Prism.Commands.DelegateCommand`1[T].Execute (T parameter) [0x00000] in d:\a\1\s\Source\Prism\Commands\DelegateCommand{T}.cs:82
at Prism.Commands.DelegateCommand`1[T].Execute (System.Object parameter) [0x00000] in d:\a\1\s\Source\Prism\Commands\DelegateCommand{T}.cs:103
at Prism.Commands.DelegateCommandBase.System.Windows.Input.ICommand.Execute (System.Object parameter) [0x00000] in d:\a\1\s\Source\Prism\Commands\DelegateCommandBase.cs:62
at Xamarin.CommunityToolkit.Behaviors.EventToCommandBehavior.OnTriggerHandled (System.Object sender, System.Object eventArgs) [0x00042] in <823f4019dc404dea9e9331c7114e4863>:0
at Xamarin.Forms.Picker.OnSelectedIndexChanged (System.Object bindable, System.Object oldValue, System.Object newValue) [0x00012] in D:\a\1\s\Xamarin.Forms.Core\Picker.cs:283
at Xamarin.Forms.BindableObject.SetValueActual (Xamarin.Forms.BindableProperty property, Xamarin.Forms.BindableObject+BindablePropertyContext context, System.Object value, System.Boolean currentlyApplying, Xamarin.Forms.Internals.SetValueFlags attributes, System.Boolean silent) [0x00120] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:512
at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty property, System.Object value, Xamarin.Forms.Internals.SetValueFlags attributes, Xamarin.Forms.BindableObject+SetValuePrivateFlags privateAttributes) [0x00173] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:446
at Xamarin.Forms.BindableObject.SetValue (Xamarin.Forms.BindableProperty property, System.Object value, System.Boolean fromStyle, System.Boolean checkAccess) [0x0004d] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:374
at Xamarin.Forms.BindableObject.SetValue (Xamarin.Forms.BindableProperty property, System.Object value) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:349
at Xamarin.Forms.Picker.set_SelectedIndex (System.Int32 value) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\Picker.cs:114
at Xamarin.Forms.Picker.UpdateSelectedIndex (System.Object selectedItem) [0x00008] in D:\a\1\s\Xamarin.Forms.Core\Picker.cs:296
at Xamarin.Forms.Picker.OnSelectedItemChanged (Xamarin.Forms.BindableObject bindable, System.Object oldValue, System.Object newValue) [0x00006] in D:\a\1\s\Xamarin.Forms.Core\Picker.cs:289
at Xamarin.Forms.BindableObject.SetValueActual (Xamarin.Forms.BindableProperty property, Xamarin.Forms.BindableObject+BindablePropertyContext context, System.Object value, System.Boolean currentlyApplying, Xamarin.Forms.Internals.SetValueFlags attributes, System.Boolean silent) [0x00120] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:512
at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty property, System.Object value, Xamarin.Forms.Internals.SetValueFlags attributes, Xamarin.Forms.BindableObject+SetValuePrivateFlags privateAttributes) [0x00173] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:446
at Xamarin.Forms.BindingExpression.ApplyCore (System.Object sourceObject, Xamarin.Forms.BindableObject target, Xamarin.Forms.BindableProperty property, System.Boolean fromTarget) [0x00226] in D:\a\1\s\Xamarin.Forms.Core\BindingExpression.cs:160
at Xamarin.Forms.BindingExpression.Apply (System.Boolean fromTarget) [0x0003e] in D:\a\1\s\Xamarin.Forms.Core\BindingExpression.cs:56
at Xamarin.Forms.BindingExpression+BindingExpressionPart.<PropertyChanged>b__49_0 () [0x00000] in D:\a\1\s\Xamarin.Forms.Core\BindingExpression.cs:762
at Xamarin.Forms.BindingExpression+BindingExpressionPart.PropertyChanged (System.Object sender, System.ComponentModel.PropertyChangedEventArgs args) [0x000cb] in D:\a\1\s\Xamarin.Forms.Core\BindingExpression.cs:773
at Xamarin.Forms.BindingExpression+WeakPropertyChangedProxy.OnPropertyChanged (System.Object sender, System.ComponentModel.PropertyChangedEventArgs e) [0x00012] in D:\a\1\s\Xamarin.Forms.Core\BindingExpression.cs:666
at (wrapper delegate-invoke) <Module>.invoke_void_object_PropertyChangedEventArgs(object,System.ComponentModel.PropertyChangedEventArgs)
at Prism.Mvvm.BindableBase.OnPropertyChanged (System.ComponentModel.PropertyChangedEventArgs args) [0x00000] in d:\a\1\s\Source\Prism\Mvvm\BindableBase.cs:99
at Prism.Mvvm.BindableBase.OnPropertyChanged (System.String propertyName) [0x00000] in d:\a\1\s\Source\Prism\Mvvm\BindableBase.cs:90
at Prism.Mvvm.BindableBase.RaisePropertyChanged (System.String propertyName) [0x00000] in d:\a\1\s\Source\Prism\Mvvm\BindableBase.cs:76
at ProjectMobile.ViewModels.DarFormPageViewModel.set_BolNumberText (System.String value) [0x00008] in /Users/vpo1/Project-Mobile/MobileApp/ProjectMobile.ViewModels/PhFormPageViewModel:118
at ProjectMobile.ViewModels.DarFormPageViewModel.OnNavigatedTo (Prism.Navigation.INavigationParameters parameters) [0x00170] in /Users/vpo1/Project-Mobile/MobileApp/ProjectMobile.ViewModels/PhFormPageViewModel:863
Removing this part of the code should resolve the problem:
<xct:EventToCommandBehavior
EventName="SelectedIndexChanged"
Command="{Binding ItemChangedCommand}"
CommandParameter="{Binding Source={x:Reference Picker1},
Path=SelectedItem}"/>
This can be done in many other ways, like using the CodeNumberText property to handle it. As it is likely a bug in that open source component you can submit the bug to their GitHub or fix it yourself.

MVVMCross ValueConverter Bool To MvxColor/Color

i was trying to implement the ValueConverter for MVVMCross, but aparently it is not working as i would expect. So at the moment i just got another Property which does the conversion (MvxColor to TextColor) for me and bind it to my Layout.
But ofc i would rather use the value converters, so maybe you have an idea what iam doing wrong:
The binding error iam currently getting:
06-09 16:29:29.820 I/MvxBind (16312): 61,64 Problem seen during binding execution for binding TextColor for Changed - problem InvalidCastException: Specified cast is not valid.
06-09 16:29:29.820 I/MvxBind (16312): at MvvmCross.Plugins.Color.Droid.BindingTargets.MvxTextViewTextColorBinding.SetValueImpl (System.Object target, System.Object value) [0x0000a] in <filename unknown>:0
06-09 16:29:29.820 I/MvxBind (16312): at MvvmCross.Binding.Bindings.Target.MvxConvertingTargetBinding.SetValue (System.Object value) [0x00088] in <filename unknown>:0
06-09 16:29:29.820 I/MvxBind (16312): at MvvmCross.Binding.Bindings.MvxFullBinding.UpdateTargetFromSource (System.Object value) [0x00024] in <filename unknown>:0
MvxBind:Error: 61,64 Problem seen during binding execution for binding TextColor for Changed - problem InvalidCastException: Specified cast is not valid.
06-09 16:29:29.820 I/mono-stdout(16312): MvxBind:Error: 61,64 Problem seen during binding execution for binding TextColor for Changed - problem InvalidCastException: Specified cast is not valid.
06-09 16:29:29.820 I/mono-stdout(16312): at MvvmCross.Plugins.Color.Droid.BindingTargets.MvxTextViewTextColorBinding.SetValueImpl (System.Object target, System.Object value) [0x0000a] in <filename unknown>:0
at MvvmCross.Plugins.Color.Droid.BindingTargets.MvxTextViewTextColorBinding.SetValueImpl (System.Object target, System.Object value) [0x0000a] in <filename unknown>:0
at MvvmCross.Binding.Bindings.Target.MvxConvertingTargetBinding.SetValue (System.Object value) [0x00088] in <filename unknown>:0
06-09 16:29:29.830 I/mono-stdout(16312): at MvvmCross.Binding.Bindings.Target.MvxConvertingTargetBinding.SetValue (System.Object value) [0x00088] in <filename unknown>:0
at MvvmCross.Binding.Bindings.MvxFullBinding.UpdateTargetFromSource (System.Object value) [0x00024] in <filename unknown>:0
06-09 16:29:29.830 I/mono-stdout(16312): at MvvmCross.Binding.Bindings.MvxFullBinding.UpdateTargetFromSource (System.Object value) [0x00024] in <filename unknown>:0
[0:] MvxBind:Error: 61,64 Problem seen during binding execution for binding TextColor for Changed - problem InvalidCastException: Specified cast is not valid.
at MvvmCross.Plugins.Color.Droid.BindingTargets.MvxTextViewTextColorBinding.SetValueImpl (System.Object target, System.Object value) [0x0000a] in <filename unknown>:0
at MvvmCross.Binding.Bindings.Target.MvxConvertingTargetBinding.SetValue (System.Object value) [0x00088] in <filename unknown>:0
at MvvmCross.Binding.Bindings.MvxFullBinding.UpdateTargetFromSource (System.Object value) [0x00024] in <filename unknown>:0
My Layout-Snippet:
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:inputType="numberDecimal|numberSigned"
android:textColor="#color/schaefflerlightgreen"
local:MvxBind="TextColor Changed,Converter=MvxBoolToColorValueConverter" />
The two converter classes i tried out:
public class MvxBoolToMvxColorValueConverter: MvxColorValueConverter<bool>
{
protected override MvxColor Convert(bool value, object parameter, CultureInfo culture)
{
if (value)
{
// FF0000
return new MvxColor(255, 0, 0);
}
//227D41
return new MvxColor(34, 125, 65);
}
}
public class MvxBoolToColorValueConverter : MvxValueConverter<bool, Color>
{
protected override Color Convert(bool value, Type targetType, object parameter, CultureInfo culture)
{
if (value)
{
// FF0000
return new Color(255, 0, 0);
}
//227D41
return new Color(34, 125, 65);
}
}
The thing is the binding is generally working with a MvxColor-Property, just the conversion seems to be an issue. So probably i just missed something.
So thanks for your help.
Edith: And the property...
public bool Changed { get { return m_sValue != m_sSyncValue; } }
Strip off the Mvx-prefix and the ValueConverter-suffix from the converter name in your binding:
Change
local:MvxBind="TextColor Changed,Converter=MvxBoolToColorValueConverter" />
to
local:MvxBind="TextColor Changed,Converter=BoolToColor" />
This behaviour is described in the documentation for ValueConverters:
This sweep locates all instanciable classes which implement IMvxValueConverter
within your assemblies creates an instance of each one registers the
instance with the name stripped of any Mvx prefix and any
ValueConverter or Converter postfix. So, for example, the following
class names will all be registered with the same ValueConverter name
of "Foo":
Foo, FooValueConverter, FooConverter, MvxFooValueConverter,
MvxFooConverter

System.Reflection.TargetInvocationException thrown when trying to instantiate new ViewController

I have a storyboard. Because I load a view in code (depending on conditions) I haven't used the segue anymore. Now I want to load another view but I'm getting the following error:
System.Reflection.TargetInvocationException was thrown
Exception has been thrown by the target of an invocation.
ListButton.TouchUpInside += (object sender, EventArgs e) => {
MyListController myListController = this.Storyboard.InstantiateViewController ("MyListController") as MyListController;
myListController.EdgesForExtendedLayout = UIRectEdge.None;
if (myListController != null) {
this.NavigationController.PushViewController (myListController, true);
}
};
When the button (ListButton) is pressed the view should be loaded onto the navigation stack. The error occurs with InstantiateViewController.
How can I solve that?
Edit:
I went into MyListController and if I comment the following out the view gets loaded:
public MyListController (IntPtr handle) : base (handle)
{
TableView.RegisterClassForCellReuse (typeof(UITableViewCell), listCellId);
TableView.Source = new MyListDataSource (this);
MyList = new List<string> ();
}
Log:
System.Reflection.TargetInvocationException: Exception has been thrown
by the target of an invocation. ---> System.Exception: Object
reference not set to an instance of an object at
TestApp.MyListController.ViewDidLoad () [0x0004c] in
/Users/xxx/Projects/TestApp/TestApp/TestApp/MyListController.cs:78
at at (wrapper managed-to-native)
MonoTouch.ObjCRuntime.Messaging:IntPtr_objc_msgSendSuper
(intptr,intptr) at
MonoTouch.UIKit.UITableViewController.get_TableView () [0x00000] in
:0 at TestApp.MyListController..ctor (IntPtr
handle) [0x00009] in
/Users/xxx/Projects/TestApp/TestApp/TestApp/MyListController.cs:17
at at (wrapper managed-to-native)
System.Reflection.MonoCMethod:InternalInvoke
(System.Reflection.MonoCMethod,object,object[],System.Exception&) at
System.Reflection.MonoCMethod.InternalInvoke (System.Object obj,
System.Object[] parameters) [0x00002] in
/Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:537
--- End of inner exception stack trace --- at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj,
System.Object[] parameters) [0x00016] in
/Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:543
at System.Reflection.MonoCMethod.DoInvoke (System.Object obj,
BindingFlags invokeAttr, System.Reflection.Binder binder,
System.Object[] parameters, System.Globalization.CultureInfo culture)
[0x00095] in
/Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:528
at System.Reflection.MonoCMethod.Invoke (BindingFlags invokeAttr,
System.Reflection.Binder binder, System.Object[] parameters,
System.Globalization.CultureInfo culture) [0x00000] in
/Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:556
at System.Reflection.ConstructorInfo.Invoke (System.Object[]
parameters) [0x00000] in
/Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Reflection/ConstructorInfo.cs:62
at MonoTouch.ObjCRuntime.Runtime.ConstructNSObject[NSObject] (IntPtr
ptr, System.Type type, MissingCtorResolution missingCtorResolution)
[0x00000] in :0 at
MonoTouch.ObjCRuntime.Runtime.ConstructNSObject (IntPtr ptr, IntPtr
klass, MissingCtorResolution missingCtorResolution) [0x00000] in
:0 at MonoTouch.ObjCRuntime.Runtime.GetNSObject
(IntPtr ptr, MissingCtorResolution missingCtorResolution) [0x00000] in
:0 at MonoTouch.ObjCRuntime.Runtime.GetNSObject
(IntPtr ptr) [0x00000] in :0 at
MonoTouch.UIKit.UIStoryboard.InstantiateViewController (System.String
identifier) [0x00000] in :0 at
TestApp.StartScreenViewController.m__1 (System.Object
sender, System.EventArgs e) [0x0000c] in
/Users/xxx/Projects/TestApp/TestApp/TestApp/StartScreenViewController.cs:57
at MonoTouch.UIKit.UIControlEventProxy.Activated () [0x00000] in
:0 at at (wrapper managed-to-native)
MonoTouch.UIKit.UIApplication:UIApplicationMain
(int,string[],intptr,intptr) at MonoTouch.UIKit.UIApplication.Main
(System.String[] args, System.String principalClassName, System.String
delegateClassName) [0x00000] in :0 at
TestApp.Application.Main (System.String[] args) [0x00008] in
/Users/xxx/Projects/TestApp/TestApp/TestApp/Main.cs:17
Try:
Hmm, now I added a new UITableViewController to the storyboard, changed some classes to public, changed variable names to starting with lower case. Also the old UITableviewController had a problem with an added SearchController. Now I made everything new and the problem only relied in the viewDidLoad but that was not the original problem. It was one of these steps (perhaps the SearchController problem).
Problematic Code:
this.NavigationController.ToolbarHidden = false;
It seems that he can't find the NavigationController ... How do I addd the navigation controller to the new created view?

Newtonsoft on Mono 4.0

I am trying to execute a compiled application on Ubuntu and get this error:
Can't find custom attr constructor image: /root/Newtonsoft.Json.dll mtoken: 0x0a000077
Missing method System.Reflection.MethodInfo::CreateDelegate(Type) in assembly /usr/lib/mono/4.0/mscorlib.dll, referenced in assembly /root/Newtonsoft.Json.dll
Method not found: 'System.Reflection.MethodInfo.CreateDelegate'. at Newtonsoft.Json.Serialization.DefaultContractResolver.GetDefaultCreator (System.Type createdType) [0x00000] in <filename unknown>:0
at Newtonsoft.Json.Serialization.DefaultContractResolver.InitializeContract (Newtonsoft.Json.Serialization.JsonContract contract) [0x00000] in <filename unknown>:0
at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateLinqContract (System.Type objectType) [0x00000] in <filename unknown>:0
at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateContract (System.Type objectType) [0x00000] in <filename unknown>:0
at Newtonsoft.Json.Serialization.DefaultContractResolver.ResolveContract (System.Type type) [0x00000] in <filename unknown>:0
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.GetContractSafe (System.Object value) [0x00000] in <filename unknown>:0
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize (Newtonsoft.Json.JsonWriter jsonWriter, System.Object value, System.Type objectType) [0x00000] in <filename unknown>:0
at Newtonsoft.Json.JsonSerializer.SerializeInternal (Newtonsoft.Json.JsonWriter jsonWriter, System.Object value, System.Type objectType) [0x00000] in <filename unknown>:0
at Newtonsoft.Json.JsonSerializer.Serialize (Newtonsoft.Json.JsonWriter jsonWriter, System.Object value, System.Type objectType) [0x00000] in <filename unknown>:0
at Newtonsoft.Json.JsonConvert.SerializeObjectInternal (System.Object value, System.Type type, Newtonsoft.Json.JsonSerializer jsonSerializer) [0x00000] in <filename unknown>:0
at Newtonsoft.Json.JsonConvert.SerializeObject (System.Object value, System.Type type, Newtonsoft.Json.JsonSerializerSettings settings) [0x00000] in <filename unknown>:0
at Newtonsoft.Json.JsonConvert.SerializeObject (System.Object value) [0x00000] in <filename unknown>:0
at Server.Misc.BalanceEconomy.InvokeMethod (System.String a_sMethod, System.Object[] a_params) [0x00000] in <filename unknown>:0
at Server.Misc.BalanceEconomy.RefreshBalances () [0x00000] in <filename unknown>:0
at Server.Misc.BalanceEconomy.OnTick () [0x00000] in <filename unknown>:0
How do I fix this?

Monotouch with Newtonsoft not working

I am very new to Monotouch/iOS development. I'm using
==========
MonoDevelop 2.8.8.4
Installation UUID: 0d3db625-7df9-4282-9aa6-177c25a46d07
Runtime:
Mono 2.10.9 (tarball Tue Mar 20 15:31:37 EDT 2012)
GTK 2.24.10
GTK# (2.12.0.0)
Mono for Android not installed
Apple Developer Tools:
Xcode 4.2.1 (834)
Build 4D502
Monotouch: 5.2.10 (Evaluation)
==========
I am trying to serialize object to json string using Newtonsoft 3.5 all.
Very simple code but not working.. Can anybody can please help me ...
partial void Action_Clicked (MonoTouch.Foundation.NSObject sender)
{
myDTO test = new myDTO();
string teststring = ObjToJSON(test);
}
public string ObjToJSON(myDTO oObject) //Old function name is ObjToJSON_WithWrapper
{
string sJSON = "";
sJSON = Newtonsoft.Json.JsonConvert.SerializeObject(oObject);
return sJSON;
}
public myDTO JSONToObj(String JSONString) //Old function name is JSONToObjNew
{
myDTO deseri = JsonConvert.DeserializeObject<myDTO>(JSONString);
return deseri;
}
public class myDTO
{
public myDTO()
{
}
public string StringObject {get; set;}
}
Above is my simple test code but it's giving me error when it's run Newtonsoft.Json.JsonConvert.SerializeObject(oObject);
*** ERROR***
{System.TypeLoadException: A type load exception has occurred.
at Newtonsoft.Json.Utilities.ThreadSafeStore`2[System.Type,System.Type].AddValue System.Type key) [0x00000] in <filename unknown>:0
at Newtonsoft.Json.Utilities.ThreadSafeStore`2[System.Type,System.Type].Get (System.Type key) [0x00000] in <filename unknown>:0
at Newtonsoft.Json.Serialization.JsonTypeReflector.GetAssociatedMetadataType (System.Type type) [0x00000] in <filename unknown>:0
at Newtonsoft.Json.Serialization.JsonTypeReflector.GetAttribute[JsonContainerAttribute] (System.Type type) [0x00000] in <filename unknown>:0
at Newtonsoft.Json.Serialization.JsonTypeReflector.GetAttribute[JsonContainerAttribute] (ICustomAttributeProvider attributeProvider) [0x00000] in <filename unknown>:0
at Newtonsoft.Json.Utilities.ThreadSafeStore`2[System.Reflection.ICustomAttributeProvider,Newtonsoft.Json.JsonContainerAttribute].AddValue (ICustomAttributeProvider key) [0x00000] in <filename unknown>:0
at Newtonsoft.Json.Utilities.ThreadSafeStore`2[System.Reflection.ICustomAttributeProvider,Newtonsoft.Json.JsonContainerAttribute].Get (ICustomAttributeProvider key) [0x00000] in <filename unknown>:0
at Newtonsoft.Json.Serialization.CachedAttributeGetter`1[Newtonsoft.Json.JsonContainerAttribute].GetAttribute (ICustomAttributeProvider type) [0x00000] in <filename unknown>:0
at Newtonsoft.Json.Serialization.JsonTypeReflector.GetJsonContainerAttribute (System.Type type) [0x00000] in <filename unknown>:0
at Newtonsoft.Json.Serialization.JsonTypeReflector.GetJsonObjectAttribute (System.Type type) [0x00000] in <filename unknown>:0
at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateContract (System.Type objectType) [0x00000] in <filename unknown>:0
at Newtonsoft.Json.Serialization.DefaultContractResolver.ResolveContract (System.Type type) [0x00000] in <filename unknown>:0
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.GetContractSafe (System.Object value) [0x00000] in <filename unknown>:0
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize (Newtonsoft.Json.JsonWriter jsonWriter, System.Object value) [0x00000] in <filename unknown>:0
at Newtonsoft.Json.JsonSerializer.SerializeInternal (Newtonsoft.Json.JsonWriter jsonWriter, System.Object value) [0x00000] in <filename unknown>:0
at Newtonsoft.Json.JsonSerializer.Serialize (Newtonsoft.Json.JsonWriter jsonWriter, System.Object value) [0x00000] in <filename unknown>:0
at Newtonsoft.Json.JsonConvert.SerializeObject (System.Object value, Formatting formatting, Newtonsoft.Json.JsonSerializerSettings settings) [0x00000] in <filename unknown>:0
at Newtonsoft.Json.JsonConvert.SerializeObject (System.Object value) [0x00000] in <filename unknown>:0
at JSONTest1.JSONTest1ViewController.ObjToJSON (JSONTest1.myDTO oObject) [0x00006] in /Users/developer/Projects/JSONTest1/JSONTest1/JSONTest1ViewController.cs:61
at JSONTest1.JSONTest1ViewController.Action_Clicked (MonoTouch.Foundation.NSObject sender) [0x00006] in /Users/developer/Projects/JSONTest1/JSONTest1/JSONTest1ViewController.cs:52 at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00042] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:29
at JSONTest1.Application.Main (System.String[] args) [0x00000] in /Users/developer/Projects/JSONTest1/JSONTest1/Main.cs:17 }
You need this version of Newtonsoft for MonoTouch:
https://github.com/ayoung/Newtonsoft.Json

Categories

Resources