Xamarin FreshMvvm简单绑定

时间:2017-10-09 18:59:25

标签: xaml xamarin binding freshmvvm

我有一个简单的问题。如何使用FreshMvvm框架从pagemodel中的.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; }
}

1 个答案:

答案 0 :(得分:0)

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

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

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

然后将您的XAML更新为此。

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

这可以解决您的问题。