Xamarin 表单 - 将参数传递给 xaml 文件中指定的绑定上下文视图模型

Xamarin forms - Pass argument to the bindingcontext viewmodel specified in a xaml file

我有一个带有条目的 xaml 文件。我将特定条目绑定到特定视图模型。但是视图模型需要一个导航器。如何将导航器从 xaml 文件传递​​到视图模型?

     <Entry  Text="{Binding Signature, Mode=TwoWay}">
        <Entry.BindingContext>
            <vm:SignaturePopupViewModel>
                // I need to pass a navigator..

            </vm:SignaturePopupViewModel>
        </Entry.BindingContext>
    </Entry>

视图模型需要一个导航对象。在 运行 一些代码逻辑之后,我用它弹出页面以返回上一页。

    public SignaturePopupViewModel(INavigation navigation = null)
    {
        Navigation = navigation;

        SendSignatureCommand = new Command(async () =>
        {
            await SendSignature();
            await Navigation.PopAsync();
        });
    }

您能否在该页面的 ViewModel 中创建一个 SignaturePopupVM 实例,然后将文本绑定到那个 属性?

虚拟机:

SignaturePopupViewModel SignaturePopupVMInstance { get; private set; }
public ParentVM()//Constructor
{
    SignaturePopupVMInstance = new SignaturePopupViewModel(new Navigator());
}

Xaml:

     <Entry  Text="{Binding SignaturePopupVMInstance.Signature, Mode=TwoWay}"/>

编辑:

public class TabPageVM{
   public ChildVM TheVMForTabOne { get; set; }
   public AnotherChildVM TheVMForTabTwo { get; set; }
   public TabVM TheVMForTabThree { get; set; }

   public TabPageVM(){
      TheVMForTabOne = new ChildVM(/*parameters*/);
      TheVMForTabTwo = new AnotherChildVM(/*parameters*/);
      TheVMForTabThree = new TabVM(/*parameters*/);      
   }

}

Xaml 标签页:

<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:Views="clr-namespace:App.ViewsForMyTabs"
             x:Class="App.TabPageView"
             BarBackgroundColor="#EEEEEE"
             BarTextColor="Black"
             BindingContext="{Binding TheTabbedPageVMInstance}">
    <TabbedPage.Children>
        <Views:TheViewForTabOne x:Name="TabOneView"
                                BindingContext="{Binding TheVMForTabOne}"/>
        <Views:TheViewForTabTwo x:Name="TabTwoView"
                                BindingContext="{Binding TheVMforTabTwo}"/>
        <Views:TheViewForTabThree x:Name="TabThreeView"
                                  BindingContext="{Binding TheVMforTabThree}"/>
    </TabbedPage.Children>
</TabbedPage>

假设 TheViewForTabOne 上有将您带到新页面的按钮。该视图 "TheVMForTabOne" 的 VM 将具有如下内容:

public class ChildVM{
   public SignaturePopupViewModel SignaturePopupVMInstance { get; set; }
   public Command NavigateToNewPageWithEntry { get; private set; }

   public ChildVM(){
      SignaturePopupVMInstance = new SignaturePopupViewModel(/*parameters*/);

      NavigateToNewPageWithEntry = new Command(() =>{
          //Navigate to new page with SignaturePopupVMInstance as the BindingContext
      }
   }
}

TheViewForTabOne
...
<Label Text="{Binding SignaturePopupVMInstance.Signature}"/>
<Button Command="{Binding NavigateToNewPageWithEntry}"/>
...

您不需要在构造函数的 SignaturePopupViewModel 中使用 INavigation navigation 来实现导航。

只要使用一个简单的方法就是

await Application.Current.MainPage.Navigation.PopModalAsync();

await Application.Current.MainPage.Navigation.PopAsync()

喜欢下面的代码。

   public class SignaturePopupViewModel
{

    public ICommand SendSignatureCommand { protected set; get; }
    public SignaturePopupViewModel( )
    {


        SendSignatureCommand = new Command(async () =>
        {


            await SendSignature();
            // if you use the     MainPage = new NavigationPage( new MainPage()); in 
            //App.xaml.cs use following code.
            await Application.Current.MainPage.Navigation.PopAsync();

             // if not, just use await Application.Current.MainPage.Navigation.PopModalAsync();
        });
    }
}