查看以查看ContentPage和ContentView之间的数据绑定
我使用Xamarin Forms Book中的PlaneRotationDemoPage示例https://download.xamarin.com/developer/xamarin-forms-book/XamarinFormsBook-Ch21-Apr2016.pdf
我有一个ContentPage,并希望将Slider控件从ContentPage移动到另一个XAML / CS文件中的ContentView。内容视图本身似乎被正确引用,但Slider控件绑定似乎没有正确连接。我收到错误"无法解析Element"
上的名称<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:PlaneRotationDemo;assembly=PlaneRotationDemo"
x:Class="PlaneRotationDemo.PlaneRotationDemoPage" >
...
<local:RoundedBoxView x:Name="handA"
StrokeThickness="2"
CornerRadius = "40"
Stroke = "White"
Color = "Gray"
AbsoluteLayout.LayoutBounds = "152.0,248.0,15.0,15.0"
/>
...
<local:SliderTest /> //new ContentView
//Moving just the slider below into a ContentView
<!--
<Slider x:Name="rotationSlider"
AbsoluteLayout.LayoutBounds = "0,0,200,50"
Maximum="360"
Value="{Binding Source={x:Reference Name=handA},
Path=Rotation}"
/>-->
//the label below on the ContentPage should bind with the slider that was moved to the ContentView
<Label Text="{Binding Source={x:Reference rotationSlider},
Path=Value,
StringFormat='Rotation = {0:F0}'}"
HorizontalTextAlignment="Center"
/>
在ContentView文件中......
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="PlaneRotationDemo.SliderTest">
<ContentView.Content>
//slider moved here
<Slider x:Name="rotationSlider"
Maximum="360"
Value="{Binding Source={x:Reference Name=handA}, //should bind with handA in the ContentPage
Path=Rotation}"
/>
</ContentView.Content>
ContentPage中的标签需要引用现在位于ContentView中的滑块
现在在ContentView中的Slider需要引用ContentPage中的handA RoundedBoxView
这在XAML中是否可行?
答案 0 :(得分:2)
在用户控件(内容视图)中公开 BindableProperty 。将 BindableProperty 绑定到Slider Control中的Value属性。
XAML看起来像这样。
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="PlaneRotationDemo.SliderTest" x:Name=UC>
<ContentView.Content>
//slider moved here
<Slider x:Name="rotationSlider"
Maximum="360"
Value="{Binding MyBindableProperty, Source={x:Reference Name=UC},
Path=Rotation}"/>
</ContentView.Content>
用法就是这样。
<local:RoundedBoxView x:Name="handA"
StrokeThickness="2"
CornerRadius = "40"
Stroke = "White"
Color = "Gray"
AbsoluteLayout.LayoutBounds = "152.0,248.0,15.0,15.0"
/>
<local:SliderTest MyBindableProperty={Binding Source={x:Reference handA}}/>
希望这有帮助。
答案 1 :(得分:0)
当你创建一个Xaml时,它会创建一个cs和一个自动生成的cs(xaml.g.cs)文件,你的x:Name引用在那里定义。由于handA未定义您的SliderTest.xaml文件,因此您收到此错误。