使用MvvmCross ShowViewModel传递复杂的导航参数 [英] Passing complex navigation parameters with MvvmCross ShowViewModel
问题描述
即使使用此处指定的配置的MvxJsonNavigationSerializer,我的复杂类型也不会从Show传递给Init方法,
My complex type wouldn't pass from Show to Init method even with configured MvxJsonNavigationSerializer as specified here Custom types in Navigation parameters in v3
public class A
{
public string String1 {get;set;}
public string String2 {get;set;}
public B ComplexObject1 {get;set;}
}
public class B
{
public double Double1 {get;set;}
public double Double2 {get;set;}
}
当我将对象A的实例传递给ShowViewModel方法时,我使用String1& String2正确反序列化,但CopmlexObject1为空.
When I pass instance of object A to ShowViewModel method I receive this object with String1 & String2 deserialized correctly but CopmlexObject1 is null.
如何处理复杂对象MvvmCross序列化?
How to deal with complex object MvvmCross serialization?
推荐答案
我认为先前的答案中可能包含一些小怪兽-会记录为问题:/
I believe there may be some gremlins in that previous answer - will log as an issue :/
还有其他可能的途径仍然可以使用Json和框架的覆盖部分来实现这种类型的复杂可序列化对象导航,但是实际上我认为仅使用您自己的BaseViewModel进行序列化和反序列化可能会更好-例如使用序列化代码,例如:
There are other possible routes to achieve this type of complex serializable object navigation still using Json and overriding parts of the framework, but actually I think that it might be better to just use your own BaseViewModel's to do serialization and deserialization - e.g. use serialization code like:
public class BaseViewModel
: MvxViewModel
{
private const string ParameterName = "parameter";
protected void ShowViewModel<TViewModel>(object parameter)
where TViewModel : IMvxViewModel
{
var text = Mvx.Resolve<IMvxJsonConverter>().SerializeObject(parameter);
base.ShowViewModel<TViewModel>(new Dictionary<string, string>()
{
{ParameterName, text}
});
}
}
具有反序列化功能,例如:
with deserialization like:
public abstract class BaseViewModel<TInit>
: MvxViewModel
{
public void Init(string parameter)
{
var deserialized = Mvx.Resolve<IMvxJsonConverter>().DeserializeObject<TInit>(parameter);
RealInit(deserialized);
}
protected abstract void RealInit(TInit parameter);
}
然后是一个像这样的viewModel:
then a viewModel like this:
public class FirstViewModel
: BaseViewModel
{
public IMvxCommand Go
{
get
{
return new MvxCommand(() =>
{
var parameter = new A()
{
String1 = "Hello",
String2 = "World",
ComplexObject = new B()
{
Double1 = 42.0,
Double2 = -1
}
};
ShowViewModel<SecondViewModel>(parameter);
});
}
}
}
可以导航至以下内容:
public class SecondViewModel
: BaseViewModel<A>
{
public A A { get; set; }
protected override void RealInit(A parameter)
{
A = parameter;
}
}
这篇关于使用MvvmCross ShowViewModel传递复杂的导航参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!