Xaml 如何将ContentView中的CommandParameter绑定到Xamarin表单中父ContentPage中的元素
我有一个父ContentPage,在单独的文件中有几个ContentView。我试图将父ContentView中的一个元素作为引用传递给其中一个视图中的CommandParameter,下面是我迄今为止的代码 这是父内容页Xaml 如何将ContentView中的CommandParameter绑定到Xamarin表单中父ContentPage中的元素,xaml,xamarin.forms,Xaml,Xamarin.forms,我有一个父ContentPage,在单独的文件中有几个ContentView。我试图将父ContentView中的一个元素作为引用传递给其中一个视图中的CommandParameter,下面是我迄今为止的代码 这是父内容页 <?xml version="1.0" encoding="utf-8" ?> <ContentPage x:Class="Acies.NitroT.Views.Shop.RefillPage" x
<?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>
<?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
>
上面的代码给出了以下错误:
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)
09-29 10:52:19.447 I/MonoDroid(25814):未处理的异常:
09-29 10:52:19.449 I/MonoDroid(25814):System.InvalidOperationException:由于对象的当前状态,操作无效。
09-29 10:52:19.449 I/MonoDroid(25814):位于Xamarin.Forms.Binding.ApplyRelativeSourceBinding(Xamarin.Forms.BindableObject targetObject,Xamarin.Forms.BindableProperty targetProperty)[0x00041],位于D:\a\1\s\Xamarin.Forms.Core\Binding.cs:153
09-29 10:52:19.449 I/MonoDroid(25814):在/Users/builder/jenkins/workspace/archive mono/2020-02/android/release/mcs/class/referencesource/mscorlib/System/Runtime/CompilerServices/asynchmethodbuilder.cs:1021中的System/Runtime/CompilerServices/Asynchmethod
09-29 10:52:19.449 I/MonoDroid(25814):在Android.App.SyncContext+c_uuudisplayClass2_0.b_uu0()[0x00000]in:0
09-29 10:52:19.449 I/MonoDroid(25814):在Java.Lang.Thread+RunnableImplementor.Run()[0x00008]中:0
09-29 10:52:19.449 I/MonoDroid(25814):在Java.Lang.IRunnableInvoker.n_运行(System.IntPtr jnienv,System.IntPtr native_uuthis)[0x00008]in:0
09-29 10:52:19.449 I/MonoDroid(25814):at(包装器动态方法)Android.Runtime.DynamicMethodNameCounter.1(intptr,intptr)
如何实现所需的结果。您可以检查以下代码 在AddressChipView.xaml中 由于您使用了自定义ContentView,因此需要在ContentView和父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>
<?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
>
现在您可以在ContentPage中绑定它了
<homeViews:AddressChipView Grid.Row="1" AddressSelectionChangedCommand={Binding xxxCommand} CurrentMapView = {Binding Source={x:Reference Map}, Path=.} />
嗨,是的,成功了。谢谢这很有效!问题,如何更改从ContentView发送回ContentPage的CommandParameter?我尝试将propertychanged:添加到BindableProperty声明的末尾,但这不起作用。需要明确的是,CommandParameter按原样工作,但我想在返回之前在ContentView.xamal.cs页面中将其修改为不同的模型。
<homeViews:AddressChipView Grid.Row="1" AddressSelectionChangedCommand={Binding xxxCommand} CurrentMapView = {Binding Source={x:Reference Map}, Path=.} />