【问题标题】:Xamarin FreshMvvm Simple BindingXamarin FreshMvvm 简单绑定
【发布时间】:2018-03-21 01:34:19
【问题描述】:

我有一个简单的问题。如何使用 FreshMvvm 框架从页面模型中的 .xaml 文件访问条目值。我希望在 settings.cs 构造函数中设置的默认值是用户在输入字段中输入的值。

谢谢!

testpage.xaml:

<Label Text="Set Server Address (FreshMvvm Binding):" />
<Entry Text="{Binding Settings.SyncServiceAddress}" Placeholder="Server IP Address" />       
<Button Text="Sync Web Service - (FreshMvvm Binding)" Command="{Binding SyncButtonFreshMvvmBinding_Clicked}" />

testpagemodel.cs:

public override void Init(object initData)
{
    if (initData != null)
    {
        Settings = (Settings)initData;
    }
    else
    {
        Settings = new Settings(); 
    }
}

public Command SyncButtonFreshMvvmBinding_Clicked
{
    get
    {
        return new Command(async () =>
        {
            string serverAddress = Settings.SyncServiceAddress;
            SyncService.PullNewXMLData(serverAddress);
            await CoreMethods.PushPageModel<DashboardPageModel>();
        });
    }
}

settings.cs:

public class Settings : ObservableObject
{
    // Constructor
    public Settings()
    {
        // Default value
        SyncServiceAddress = "http://localhost/psm/service.aspx";
    }

    // Properties
    public string SyncServiceAddress { get; set; }
    public string UserIDSettings { get; set; }
}

【问题讨论】:

  • 您好 useruseruser:您的 Settings 属性是如何在 testpagemodel.cs 中定义的,它是公开的吗?您能否将任何简单的字符串属性绑定到 Entry 文本框以检查您的页面 BindingContext 是否已全部连接起来。

标签: xaml xamarin binding freshmvvm


【解决方案1】:

请记住,UI 只需要了解一组有限的属性,可以在 ViewModel 中创建可绑定属性,这样您就不会尝试绑定到嵌套属性。

在 ViewModel 中创建一个名为 SyncServiceAddress 的新字符串属性;

public string SyncServiceAddress
{
    get{
        return Settings.SyncServiceAddress;
    }
    set{
       Settings.SyncServiceAddress = value;
    }
}

然后将您的 XAML 更新为此。

<Entry Text="{Binding SyncServiceAddress}" Placeholder="Server IP Address" /> 

这应该可以解决您的问题。

【讨论】:

    猜你喜欢
    • 2018-02-16
    • 2018-06-13
    • 2017-01-22
    • 2016-10-10
    • 2018-12-16
    • 1970-01-01
    • 2018-09-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多