1

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

谢谢!

测试页.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}" />

测试页面模型.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>();
        });
    }
}

设置.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; }
}
4

1 回答 1

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" /> 

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

于 2018-04-12T21:59:59.600 回答