Xamarin表单中的数据绑定条目

时间:2018-12-26 14:02:18

标签: xamarin data-binding xamarin.ios

我正在尝试在Xamarin Forms的Login(登录)文本框中捕获条目的值。我已经创建了类,数据绑定在xmal中,但是在为iOS编译时遇到构建错误。任何指针都很感激。

Login.xmal:

@media screen {
  @font-face {
    font-family: 'DINPro-Light';
    src: url('http://yourfont.com/fonts/din/DINPro-Light.eot');
    src: url('http://yourfont.comfonts/din/DINPro-Light.eot?#iefix') format('embedded-opentype'),
    url('http://yourfont.comfonts/din/DINPro-Light.woff2') format('woff2'),
    url('http://yourfont.comfonts/din/DINPro-Light.woff') format('woff'),
    url('http://yourfont.comfonts/din/DINPro-Light.ttf') format('truetype'),
    url('http://yourfont.comfonts/din/DINPro-Light.svg#DINPro-Light') format('svg');
    font-weight: normal;
    font-style: normal;
  }

FormEntry Class.cs

使用系统; 命名空间SGC.Class

 <Entry x:Name="{Binding Source = AppLoginUsername;}" Placeholder="Username" 
                   Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="3"
                   HorizontalOptions="StartAndExpand" WidthRequest="300" />

Home.cs(获取在“登录”页面中输入的用户名并找出客户ID)

{
    public class FormEntryBindings
    {
        public FormEntryBindings()
        {


        }


        public static string AppLoginUserName { get; set; }
    }
}

错误是:

类,结构或接口成员声明中的令牌'{'无效

无效的表达式术语'='

类型或名称空间定义,或预期的文件结尾

3 个答案:

答案 0 :(得分:2)

您为什么不给它起一个名字,然后绑定文本呢? 例如 x:Name =“用户” Text =“ {Binding AppLoginUsername}” ...

答案 1 :(得分:1)

您无法绑定名称。名称是生成代码的内容,您不能随时更改变量的名称。

答案 2 :(得分:1)

根据评论,我认为您的问题是可以在登录页面中获得AppLoginUserName,但是,您想在另一页面(另一视图)中使用AppLoginUserName。 / p>

我会给您一些建议:

1。您可以将值传递到要使用的页面。

2。使用Xamarin.Essentials: Preferences

用户登录后,您可以保存AppLoginUserName

using Xamarin.Essentials;

Preferences.Set("AppLoginUserName", "value");

在要使用的另一个视图上,您可以获取值:

var myValue = Preferences.Get("AppLoginUserName", "default_value");

并在用户注销或在其他情况下从首选项中删除键:

Preferences.Remove("my_key");

您可以参考:essentials/preferences