如何将 ContentView 中的 CommandParameter 绑定到 Xamarin Forms 中父 ContentPage 中的元素 [英] How to bind a CommandParameter in a ContentView to an element in the parent ContentPage in Xamarin Forms

查看:32
本文介绍了如何将 ContentView 中的 CommandParameter 绑定到 Xamarin Forms 中父 ContentPage 中的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个父 ContentPage,在不同的文件中有几个 ContentView.我试图将父 ContentView 中的一个元素作为引用传递给其中一个视图中的 CommandParameter,下面是我迄今为止的代码.

I have a parent ContentPage with several ContentViews in separate files. I am trying to pass as reference an element in the parent ContentView to a CommandParameter in one of the views and below is the code I have thus far.

这是父 ContentPage

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
x:Class="Acies.NitroT.Views.Shop.RefillPage"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:base="clr-namespace:Acies.NitroT.ViewModels.Base;assembly=Acies.NitroT.Mobile.Shared"
xmlns:behaviors="clr-namespace:Acies.NitroT.Behaviors;assembly=Acies.NitroT.Mobile.Shared"
xmlns:control="clr-namespace:Acies.NitroT.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:homeViews="clr-namespace:Acies.NitroT.Views.Home"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:tab="clr-namespace:Syncfusion.XForms.TabView;assembly=Syncfusion.SfTabView.XForms"
xmlns:transViews="clr-namespace:Acies.NitroT.Views.Transaction"
Title="Gas Refill"
Padding="0"
base:ViewManager.AutoWireViewModel="true"
BackgroundColor="{DynamicResource Gray-White}"
NavigationPage.HasNavigationBar="False"
Visual="Material"
mc:Ignorable="d">
<ContentPage.Content>
    <RelativeLayout Padding="0">
        <RelativeLayout.Margin>
            <OnPlatform x:TypeArguments="Thickness">
                <On Platform="iOS" Value="0, 40, 0, 0" />
            </OnPlatform>
        </RelativeLayout.Margin>
        <Grid
            Padding="0"
            RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent,
                                                                   Property=Height,
                                                                   Factor=1}"
            RowSpacing="0"
            VerticalOptions="FillAndExpand">
            <Grid.RowDefinitions>
                <RowDefinition Height="auto" />
                <RowDefinition Height="*" />
                <RowDefinition Height="auto" />
            </Grid.RowDefinitions>

            <!--#region Google Map [Row:0] Pins="{Binding Pins}"-->
            <control:GoogleMapView
                x:Name="Map"
                Grid.Row="0"
                Grid.RowSpan="2"
                Margin="0"
                Padding="0"
                HorizontalOptions="FillAndExpand"
                VerticalOptions="FillAndExpand" />
            <!--#endregion-->

            <!--#region Search Control [Row:1]-->
            <Grid
                Grid.Row="1"
                Margin="20"
                VerticalOptions="Start">

                <Grid.RowDefinitions>
                    <RowDefinition Height="*" />
                    <RowDefinition Height="auto" />
                    <RowDefinition Height="auto" />
                </Grid.RowDefinitions>

                <!--#region Search Bar-->
                <homeViews:SearchBarView Grid.Row="0" Padding="0" />
                <!--#endregion-->

                <!--#region Chip Group-->
                <homeViews:AddressChipView Grid.Row="1" />
                <!--#endregion-->

                <!--#region Search Results View-->
                <homeViews:SearchResultsView Grid.Row="2" Padding="0" />
                <!--#endregion-->
            </Grid>
            <!--#endregion-->

            <!--#region Bottom Control [Row:2]-->
            
            <homeViews:CommandButtonsView
                Grid.Row="1"
                Margin="0,0,0,10"
                VerticalOptions="End" />
            <!--#endregion-->
        </Grid>
    </RelativeLayout>
</ContentPage.Content>

这里是 ContentPage,我试图将 control:GoogleMapViewx:Name="Map" 作为 传递>命令参数

Here is the ContentPage where am trying to pass the control:GoogleMapView with x:Name="Map" as a CommandParameter

<?xml version="1.0" encoding="UTF-8" ?>
<ContentView
x:Class="Acies.NitroT.Views.Home.AddressChipView"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:behaviors="clr-namespace:Acies.NitroT.Behaviors"
xmlns:buttons="clr-namespace:Syncfusion.XForms.Buttons;assembly=Syncfusion.Buttons.XForms"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:controls="clr-namespace:Acies.NitroT.Controls;assembly=Acies.NitroT.Mobile.Shared"
mc:Ignorable="d">

<ContentView.Content>
    <!--#region Chip Group-->
    
    <StackLayout Grid.Row="1">
        <ScrollView
            Margin="{StaticResource ChipMargin}"
            HorizontalScrollBarVisibility="Never"
            Orientation="Horizontal">
            <buttons:SfChipGroup
                ChipPadding="2,0,0,0"
                Command="{Binding SelectedAddressCommand}"
                DisplayMemberPath="Nickname"
                ItemsSource="{Binding AddressItems}"
                SelectedChipBackgroundColor="{StaticResource BlueDark}"
                SelectedItem="{Binding SelectedAddress}"
                ShowIcon="True"
                Type="Choice">
                <buttons:SfChipGroup.Behaviors>
                    <behaviors:EventToCommandBehavior
                        Command="{Binding AddressSelectionChangedCommand}"
                        CommandParameter="{Binding Source={x:RelativeSource Mode=FindAncestor, AncestorType={x:Type controls:GoogleMapView}}, Path=.}"
                        EventName="SelectionChanged" />
                </buttons:SfChipGroup.Behaviors>
            </buttons:SfChipGroup>
        </ScrollView>
    </StackLayout>
    <!--#endregion-->
</ContentView.Content>

上面的代码给了我以下错误:

The above code is giving me the below error:

09-29 10:52:19.447 I/MonoDroid(25814): UNHANDLED EXCEPTION:
09-29 10:52:19.449 I/MonoDroid(25814): System.InvalidOperationException: Operation is not valid due to the current state of the object.
09-29 10:52:19.449 I/MonoDroid(25814):   at Xamarin.Forms.Binding.ApplyRelativeSourceBinding (Xamarin.Forms.BindableObject targetObject, Xamarin.Forms.BindableProperty targetProperty) [0x00041] in D:\a\1\s\Xamarin.Forms.Core\Binding.cs:153 
09-29 10:52:19.449 I/MonoDroid(25814):   at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__7_0 (System.Object state) [0x00000] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1021 
09-29 10:52:19.449 I/MonoDroid(25814):   at Android.App.SyncContext+<>c__DisplayClass2_0.<Post>b__0 () [0x00000] in <7d2292394f8c488b97f5bc2a0ac0240d>:0 
09-29 10:52:19.449 I/MonoDroid(25814):   at Java.Lang.Thread+RunnableImplementor.Run () [0x00008] in <7d2292394f8c488b97f5bc2a0ac0240d>:0 
09-29 10:52:19.449 I/MonoDroid(25814):   at Java.Lang.IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this) [0x00008] in <7d2292394f8c488b97f5bc2a0ac0240d>:0 
09-29 10:52:19.449 I/MonoDroid(25814):   at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.1(intptr,intptr)

我怎样才能达到预期的结果.

How can i achieve the desired result.

推荐答案

您可以查看以下代码

由于您使用了自定义 ContentView ,您需要使用 bindable 属性绑定ContentViewParent ContentPage

Since you had used Custom ContentView , you need use bindable property to binding value between the elements in ContentView and Parent ContentPage

<?xml version="1.0" encoding="UTF-8" ?>
<ContentView
x:Class="Acies.NitroT.Views.Home.AddressChipView"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:behaviors="clr-namespace:Acies.NitroT.Behaviors"
xmlns:buttons="clr-namespace:Syncfusion.XForms.Buttons;assembly=Syncfusion.Buttons.XForms"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:controls="clr-namespace:Acies.NitroT.Controls;assembly=Acies.NitroT.Mobile.Shared"
mc:Ignorable="d"

x:Name="ChipView" // set the name of AddressChipView

>

<buttons:SfChipGroup
                ChipPadding="2,0,0,0"
                Command="{Binding Source={x:Reference ChipView}, Path=SelectedAddressCommand}"
                //...
                SelectedItem="{Binding Source={x:Reference ChipView}, Path=SelectedAddress}"
                ShowIcon="True"
                Type="Choice">
                <buttons:SfChipGroup.Behaviors>
                    <behaviors:EventToCommandBehavior
                        Command="{Binding Source={x:Reference ChipView}, Path=AddressSelectionChangedCommand}"
                        CommandParameter="{Binding Source={x:Reference ChipView}, Path=CurrentMapView}"
                        EventName="SelectionChanged" />
                </buttons:SfChipGroup.Behaviors>
            </buttons:SfChipGroup>

在 AddressChipView.xaml.cs 中

public static readonly BindableProperty AddressSelectionChangedCommandProperty =
            BindableProperty.Create(nameof(AddressSelectionChangedCommand), typeof(ICommand), typeof(AddressChipView));

        public ICommand AddressSelectionChangedCommand
        {
            get => (ICommand)GetValue(AddressSelectionChangedCommandProperty );
            set => SetValue(AddressSelectionChangedCommandProperty , value);
        }

        public static BindableProperty CurrentMapViewProperty =
            BindableProperty.Create(nameof(CurrentMapView), typeof(object), typeof(AddressChipView));

        public object CurrentMapView
        {
            get => (object)GetValue(CurrentMapViewProperty );
            set => SetValue(CurrentMapViewProperty , value);
        }

        //... other bindable property  
       

现在可以在ContentPage中绑定

Now you can binding it in ContentPage

<homeViews:AddressChipView Grid.Row="1"  AddressSelectionChangedCommand={Binding xxxCommand}  CurrentMapView = {Binding Source={x:Reference Map}, Path=.} />

这篇关于如何将 ContentView 中的 CommandParameter 绑定到 Xamarin Forms 中父 ContentPage 中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆