0

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

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

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

viewmodel 需要一个导航对象。在运行一些代码逻辑后,我使用它来弹出页面以返回上一页。

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

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

2 回答 2

2

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

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

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();
        });
    }
}
于 2020-03-10T02:28:19.023 回答
1

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

虚拟机:

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

xml:

     <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”的虚拟机将具有以下内容:

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}"/>
...
于 2020-03-09T14:58:18.337 回答