【问题标题】:Rotating Label with DataBinding Xamarin Forms使用 DataBinding Xamarin 表单旋转标签
【发布时间】:2014-12-04 20:41:20
【问题描述】:

我正在尝试复制 following example 以了解 Xamarin 中的绑定。

运行以下脚本后,我的第一个标签不会随滑块一起旋转。

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="DataBinding.MyPage" Title="Slider Bindings Page">
 <StackLayout>
    <Label Text="ROTATION"
           BindingContext="{x:Reference Name=slider}"
           Rotation="{Binding Path=Value}"
           Font="Bold, Large"
           HorizontalOptions="Center"
           VerticalOptions="CenterAndExpand" />

    <Slider x:Name="slider"
            Maximum="360"
            VerticalOptions="CenterAndExpand" />

    <Label BindingContext="{x:Reference slider}"
           Text="{Binding Value, 
                          StringFormat='The angle is {0:F0} degrees'}"
           Font="Large"
           HorizontalOptions="Center"
           VerticalOptions="CenterAndExpand" />
  </StackLayout>


</ContentPage>

【问题讨论】:

  • 试试 Rotation="{绑定值}"
  • 我刚刚在所有 3 个平台上运行了该示例,它运行良好,即使在 iOS 上也是如此。您是想在自己的新项目中复制它吗?还是只是将其作为现有示例解决方案的一部分运行?
  • 您使用的是哪个版本的 Xamarin.Forms?
  • 我认为您可能正在使用旧版本,因为它曾经要求任何引用的控件必须在使用引用之前出现在标记中。看起来在最新版本 1.2.3.6257 上,他们现在已经修复了 xaml 页面,因此控件现在可以引用任何位置。我会把这个写下来作为答案。
  • 这很奇怪,因为它应该和我的示例版本一样工作,尤其是当我们使用相同的标记时?你可以通过电子邮件发送项目,我会看看吗?您的经验表明以前的版本总是这样工作的。

标签: xaml xamarin xamarin.forms


【解决方案1】:

以前在 XAML 页面中要求任何使用 {x:Reference} 的控件都必须在之前的标记中出现被引用的控件它被引用了。

Xamarin.Forms SliderBindings 示例中,我们有以下最初与 Xamarin.Forms 1.22x 配合使用的标记:-

<StackLayout>
    <Slider x:Name="slider"
            Maximum="360"
            VerticalOptions="CenterAndExpand" />

    <Label Text="ROTATION"
           BindingContext="{x:Reference Name=slider}"
           Rotation="{Binding Path=Value}"
           Font="Bold, Large"
           HorizontalOptions="Center"
           VerticalOptions="CenterAndExpand" />

    <Label BindingContext="{x:Reference slider}"
           Text="{Binding Value, 
                          StringFormat='The angle is {0:F0} degrees'}"
           Font="Large"
           HorizontalOptions="Center"
           VerticalOptions="CenterAndExpand" />
</StackLayout>

但是,现在在最新版本的 Xamarin.Forms v1.2.3.6257 中,您不再需要在控件成为 {x:Referenced} 之前放置控件一个文档,从他们更新的例子可以看出:-

<StackLayout>
    <Label Text="ROTATION"
           BindingContext="{x:Reference Name=slider}"
           Rotation="{Binding Path=Value}"
           Font="Bold, Large"
           HorizontalOptions="Center"
           VerticalOptions="CenterAndExpand" />

    <Slider x:Name="slider"
            Maximum="360"
            VerticalOptions="CenterAndExpand" />

    <Label BindingContext="{x:Reference slider}"
           Text="{Binding Value, 
                          StringFormat='The angle is {0:F0} degrees'}"
           Font="Large"
           HorizontalOptions="Center"
           VerticalOptions="CenterAndExpand" />
  </StackLayout>

因此,如果您将项目更新到最新的 Xamarin.Forms v1.2.3.6257,那么您将能够{x:Reference} 控件而无需订购控制很重要。可能仍然存在某些限制,但它似乎比以前的版本灵活得多。

【讨论】:

  • 我已经完全复制了您的最新代码,但仍然没有运气。
  • 我仍然认为您可能使用的是以前版本的 Xamarin.Forms。你能把你的例子发给我吗?
  • 确定我是通过电子邮件发送的。
  • 刚刚发送到您的邮箱,如果收到请告诉我。
  • 是的 - 已收到。您的 Xamarin.Forms.Platform.iOS 未更新到最新版本的 v.1.2.3(您的文件当前版本为 1.0.5256.2017)。因此,尽管您的 Xamarin.Forms.Core 和 Xamarin.Forms.Xaml 是 1.2.3 版,但由于您的包略有不同步,您并没有真正拥有完整的最新版本。如果你从 NuGet 获得最新版本,你应该没问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-27
  • 1970-01-01
相关资源
最近更新 更多