问题描述
在过去的几周里,我一直致力于使用 MVMCross 框架开发跨平台应用程序 (IOS/Android/WP7).今天我遇到了一个我真的不知道如何解决的问题,所以希望你能把我推向正确的方向.
For the past couple of weeks I've been working on developing a cross platform app (IOS/Android/WP7) using the MVVMCross framework. Today I ran into a problem I don't really know how to solve, so hopefully you can push me in the right direction.
在 IOS 中,我使用以下结构导航到另一个页面(以下代码位于 ViewModel 中):
In the IOS I have the following construction for navigating to another page (the code below is located in a ViewModel):
KeyValuePair<string,string> kvpAct1 = new KeyValuePair<string, string>("short", ".countertest5");
public IMvxCommand BeckhoffActuator1
{
get
{
return new MvxRelayCommand<Type>((type) => this.RequestNavigate<Beckhoff.BeckhoffActuatorViewModel>(kvpAct1));
}
}
当这个 IMvxCommand 被触发(按下按钮)时,下一个视图被加载,在这种情况下是 BeckhoffActuatorViewModel.在 BeckhoffActuatorView 的代码中,我使用了上面的键值对:
When this IMvxCommand is fired (button pressed) the next View is loaded, in this case the BeckhoffActuatorViewModel. In the code of the BeckhoffActuatorView I use the keyvaluepair from above:
public class BeckhoffActuatorView : MvxTouchDialogViewController<BeckhoffActuatorViewModel>
{
ICollection<string> icol;
public BeckhoffActuatorView(MvxShowViewModelRequest request) : base(request, UITableViewStyle.Grouped, null, true)
{
icol = request.ParameterValues.Values;
}
public override void ViewDidLoad()
{
//Code
}
}
此结构在 IOS 中运行良好,但我想在我的 android 应用程序中使用相同的结构.
This construction is working fine in IOS, but I would like to use the same construction in my android App.
ViewModel 中的代码没有改变,因为那是 MVVM 的全部思想.但是 Android 的 BackhoffActuatorView 的代码是不同的:
The code in the ViewModel hasn't changed since that's the whole idea of MVVM. But the code of the BackhoffActuatorView is different for Android:
public class BeckhoffActuatorView : MvxBindingActivityView<BeckhoffSensorViewModel>
{
public ICollection<string> icol;
public BeckhoffActuatorView()
{
Debug.WriteLine("Standard");
}
public BeckhoffActuatorView(MvxShowViewModelRequest request)
{
Debug.WriteLine("Custom");
icol = request.ParameterValues.Values;
}
protected override void OnViewModelSet()
{
SetContentView(Resource.Layout.BeckhoffActuatorView);
}
}
上面的代码不起作用,MvxBindingActivityView 似乎没有实现类似于我在 IOS 中使用的 ViewController 的东西.代码只出现在标准构造函数中,当我完全忽略它时,它不会编译/运行.
The code above isn't working, the MvxBindingActivityView doesn't seem to implement something similar to the ViewController I use in IOS. The code only come in the standard constructor, and when I leave that one out completely it won't compile/run.
有谁知道我可以访问我通过 RequestNavigate 发送的键值对?谢谢!
Does anyone know know I can access the keyvaluepair I send with the RequestNavigate? Thank you!
推荐答案
MVVMCross 非常基于约定 - 它的工作原理是尽可能在 ViewModel 之间传递消息.
MVVMCross is very convention based - and it works on the idea of passing messages between ViewModels wherever possible.
如果您使用以下方法导航到 ViewModel:
If you navigate to a ViewModel using:
KeyValuePair<string,string> kvpAct1 = new KeyValuePair<string, string>("short", ".countertest5");
public IMvxCommand BeckhoffActuator1
{
get
{
return new MvxRelayCommand<Type>((type) => this.RequestNavigate<Beckhoff.BeckhoffActuatorViewModel>(kvpAct1));
}
}
那么您应该能够使用构造函数在 BeckhoffActuatorViewModel 中获取它:
then you should be able to pick that up in the BeckhoffActuatorViewModel using the constructor:
public class BeckhoffActuatorViewModel : MvxViewModel
{
public BeckhoffActuatorViewModel(string short)
{
ShortValue = short;
}
private string _shortValue;
public string ShortValue
{
get
{
return _shortValue;
}
set
{
_shortValue = value;
FirePropertyChanged("ShortValue");
}
}
}
然后你的视图可以访问 ViewModel.ShortValue
(对于 iOS,这可以在 base.ViewDidLoad() 之后完成,对于 OnCreate() 之后的 Android 和 OnNavigatedTo 之后的 WP7)
And your views can then access ViewModel.ShortValue
(for iOS this can be done after base.ViewDidLoad(), for Android after OnCreate() and for WP7 after OnNavigatedTo)
有关此示例,请查看 TwitterSearch 示例:
For an example of this, take a look at the TwitterSearch example:
这有一个 HomeViewModel 调用导航使用:
This has a HomeViewModel which calls navigate using:
private void DoSearch()
{
RequestNavigate<TwitterViewModel>(new { searchTerm = SearchText });
}
和一个使用构造函数接收 searchTerm 的 TwitterViewModel:
and a TwitterViewModel which receives the searchTerm using the constructor:
public TwitterViewModel(string searchTerm)
{
StartSearch(searchTerm);
}
请注意,这里只允许 string
s、int
s、double
s 和 bool
s当前传递的构造函数参数 - 这是由于 Xaml Urls 和 Android Intent 的序列化要求.如果您想使用自己的自定义序列化对象尝试导航,请参阅 http://slodge.blogspot.co.uk/2013/01/navigating-between-viewmodels-by-more.html.
Please note that only string
s, int
s, double
s and bool
s are allowed in this constructor parameter passing at present - this is due to serialisation requirements for Xaml Urls and for Android Intents. If you want to experiment with navigation using your own custom serialised objects, then please see http://slodge.blogspot.co.uk/2013/01/navigating-between-viewmodels-by-more.html.
另外,请注意,如果您想使用匿名对象导航 (RequestNavigate(new { searchTerm = SearchText });
),那么您需要确保 InternalsVisibleTo
属性已设置 - 参见 https://github.com/slodge/MvvmCrossTwitterSearch/blob/master/TwitterSearch.Core/Properties/AssemblyInfo.cs:
Also, note that if you want to use the anonymous object navigation (RequestNavigate<TwitterViewModel>(new { searchTerm = SearchText });
) then you will need to make sure that an InternalsVisibleTo
attribute is set - see https://github.com/slodge/MvvmCrossTwitterSearch/blob/master/TwitterSearch.Core/Properties/AssemblyInfo.cs:
[assembly: InternalsVisibleTo("Cirrious.MvvmCross")]
此外……不适合胆小的人……这不是好的 mvvm 代码"……但是如果您真的想要/需要访问 Android 活动中的 MvxShowViewModelRequest 数据,那么您可以提取它来自传入的 Intent - 有一个包含请求的 Extras 字符串(请参阅 CreateViewModelFromIntent 中的反序列化Crillous.MvvmCross/Android/Views/MvxAndroidViewsContainer.cs" rel="nofollow noreferrer">https://github.com/slodge/MvvmCross/blob/master/Cirrious/Cirrious.MvvmCross/Android/Views/MvxAndroidViewsContainer.cs一>)
Further... not for the faint-hearted... and this isn't "good mvvm code"... but if you really want/need to access the MvxShowViewModelRequest data inside an Android activity, then you can extract it from the incoming Intent - there's an Extras string containing the request (see the deserialisation in CreateViewModelFromIntent
in https://github.com/slodge/MvvmCross/blob/master/Cirrious/Cirrious.MvvmCross/Android/Views/MvxAndroidViewsContainer.cs)
这篇关于将变量从 ViewModel 传递到另一个 View (MVVMCross)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!