【问题标题】:Xamarin forms - Pass argument to the bindingcontext viewmodel specified in a xaml fileXamarin 表单 - 将参数传递给 xaml 文件中指定的 bindingcontext 视图模型
【发布时间】:2020-06-21 11:42:44
【问题描述】:

我有一个带有条目的 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();
        });
    }

【问题讨论】:

  • 导航器是什么意思?
  • 视图模型需要一个导航。我使用该对象,以便可以在视图模型中弹出页面以返回上一页。我在帖子中添加了代码。

标签: xamarin.forms binding binding-context


【解决方案1】:

您不需要在构造函数中的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();
        });
    }
}

【讨论】:

    【解决方案2】:

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

    虚拟机:

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

    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}"/>
    ...
    

    【讨论】:

    • 但是我需要设置那个 Entry 视图的 bindingcontext 对吗?
    • xaml 文件有多个绑定上下文。这就是为什么我需要在视图中设置绑定上下文。如果可以询问,如何设置特定条目的绑定上下文?
    • 也许正确的问题是:如何从 xaml 文件中引用 SignaturePopupVMInstance 以便入口视图可以设置绑定上下文? (页面有多个视图,一些视图引用其他绑定上下文,这就是为什么)
    • 如果您关注 MVVM,您的内容页面需要为整个页面提供一个 BindingContext。然后,如果您需要页面内的控件绑定到其他 ViewModel,您可以在 ContentPage 的 vm 上创建其他 ViewModel 属性;父虚拟机,如果你愿意的话。
    • 我有以下问题:该应用基于标签页。其中一个标签页上有一个按钮。当我点击它时,会打开一个新页面。在那个页面上,我可以填写一个条目。当我按下提交时,页面会弹出,我会返回上一页,即选项卡式页面。在那个标签页上,我想显示条目中写的文本。
    猜你喜欢
    • 2022-01-16
    • 2018-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-09
    相关资源
    最近更新 更多