我正在使用MVVMCROSS处理Xamarin中的应用程序,我遇到了以下问题:
在我的主视图中我有4个按钮,我的问题是,当我点击任何按钮时,我被定向到Root,而且我总是从RootViewModel中配置的第一个视图中掉落,如何配置我的主按钮以便每一个都将被引导到他们各自的观点。
- 查看
[MvxRootPresentation (WrapInNavigationController = false)]
public partial class MainView: BaseScrollViewController
{
public override void DidReceiveMemoryWarning ()
{
base.DidReceiveMemoryWarning ();
// Release any cached data, images, etc that are not in use.
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
if (ViewModel == null)
{
return;
}
// Title = "Welcome!";
/ *
* Binding
* /
var set = this.CreateBindingSet <MainView, Core.ViewModels.MainViewModel> ();
//set.Bind(stkPassword).For("Visibility").To(x => x.stkPasswordVisibility);
set.Bind (btnNewOrder) .To (vm => vm.BtnShow);
set.Bind (btnAvailableOrders) .To (vm => vm.BtnShow);
set.Bind (btnMyTasks) .To (vm => vm.BtnShow);
set.Bind (btnMyOrders) .To (vm => vm.BtnShow);
set.Apply ();
}
}
- 模型
public class MainViewModel: BaseViewModel
{
private readonly IMvxNavigationService _navigationService;
private IUserService userService;
public LoggedUser loggedUser;
public MainViewModel (IMvxNavigationService navigationService, IUserService userService)
{
_navigationService = navigationService;
this.userService = userService;
}
public IMvxCommand BtnShow
{
get
{
return new MvxCommand (show);
}
}
private void show ()
{
ShowViewModel <RootViewModel> ();
}
}
今天我配置了所有按钮,当点击时,转到RootViewModel - 视图
[MvxRootPresentation (WrapInNavigationController = true)]
public sealed partial class RootView: MvxTabBarViewController <RootViewModel>
{
private bool _isPresentedFirstTime = true;
public RootView ()
{
//ViewDidLoad ();
}
public override void ViewWillAppear (bool animated)
{
base.ViewWillAppear (animated);
if (ViewModel! = null && _isPresentedFirstTime)
{
_isPresentedFirstTime = false;
ViewModel.ShowInitialViewModels ();
}
}
}
- 模型
public class RootViewModel: MvxViewModel
{
public void ShowInitialViewModels ()
{
ShowViewModel <NewOrder2ViewModel> ();
ShowViewModel <AvailableOrdersViewModel> ();
ShowViewModel <MyOrdersViewModel> ();
ShowViewModel <MyTasksViewModel> ();
ShowViewModel <MoreViewModel> ();
}
}
我的RootViewModel中配置的所有页面都是MvxTabPresentation。
------编辑
通过单击主视图中的按钮,我应该指向您的TAB字段,例如,如果我点击“我的订单”,我应该转到“我的订单”标签。 正如我所解释的那样,通过点击我的任何按钮,我被定向到第一个TAB,因为当我点击时我转到RootViewModel而我无法处理我点击的按钮,所以我无法指向他们各自的TAB < / p>
我正在使用MvvmCross版本5.0.6
答案 0 :(得分:0)
抱歉延迟回答。
您可以使用 CommandParameter 在按钮点击时使用命令传递参数。
MainView中的
set.Bind (btnNewOrder) .To (vm => vm.BtnShow).CommandParameter(1);
set.Bind (btnAvailableOrders) .To (vm => vm.BtnShow).CommandParameter(2);
set.Bind (btnMyTasks) .To (vm => vm.BtnShow).CommandParameter(3);
set.Bind (btnMyOrders) .To (vm => vm.BtnShow).CommandParameter(4);
MainViewModel中的
public IMvxCommand BtnShow
{
get
{
return new MvxCommand<int>(show);
}
}
private void show (int _index)
{
//here we must wrap int into a model ,because the init method only accept object.
ShowViewModel <RootViewModel> (new ParametersModel { index = _index});
}
RootViewModel中的
public ParametersModel _model;
public void Init(ParametersModel model)
{
_model = model;
}
<强> ParametersModel 强>
public class ParametersModel
{
public int index { get; set; }
}
如上面的代码所示,你可以得到一个带参数1的模型,你可以决定此时应该显示哪个页面。
请参阅此link,创建TabBarController作为示例。
如何配置我的主按钮,以便将每个按钮定向到各自的视图。
修改ViewDidLoad
SelectedViewController = ViewControllers[ViewModel._model.index];