Xamarin表单设置插件可绑定

时间:2015-10-06 12:35:24

标签: xamarin binding xamarin.forms settings bindable

我一直是非常棒的Xamarin Cross Platform设置插件https://components.xamarin.com/view/SettingsPlugin(确切地说是PCL中的Nuget版本)

它运行良好,但现在使用Xamarin表单我真的希望能够绑定到设置值。 I.e我想在内容页面上有一个标签,上面写着:“设置值为:[value]”并在设置更改时更新。

设置类如下所示:

 public static class Settings {

   private static ISettings AppSettings
{
  get
  {
    return CrossSettings.Current;
  }
}

#region Setting Constants

    const string HOT_TIME_COUNT_KEY = "hotTimeCount";
    private static readonly int HOT_TIME_COUNT_DEFAULT = 0;



    #endregion



    public static int HotTimeCount
    {
        get { return AppSettings.GetValueOrDefault(HOT_TIME_COUNT_KEY, HOT_TIME_COUNT_DEFAULT); }
        set { AppSettings.AddOrUpdateValue(HOT_TIME_COUNT_KEY, value); }
    }

}

我似乎无法找出使这项工作需要的格式? BindableProperty.Create?它需要OnPropertyChanged方法吗? Settings Class是否必须派生自BindableObject?

1 个答案:

答案 0 :(得分:1)

我创建了这个库:)你总是可以在github上发布一个问题,但实质上你需要创建一个视图模型,其中包含你希望数据绑定到的公共属性,然后从那里调用设置并提升属性如果值更改,则更改通知。您的Settings.cs可以保持不变,但您需要创建视图模型,例如:

 public class MyViewModel : INotifyPropertyChanged
{

    public int Time
    {
        get { return Settings.HotTimeCount; }
        set
        {
            if (Settings.HotTimeCount == value)
                return;

            Settings.HotTimeCount = value;
            OnPropertyChanged();
        }

    }

    private Command increase;
    public Command IncreaseCommand
    {
        get 
        { 
            return increase ?? (increase = new Command(() =>Time++));
        }
    }

    #region INotifyPropertyChanged implementation

    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged([CallerMemberName]string name = "")
    {
        var changed = PropertyChanged;
        if (changed == null)
            return;
        changed(this, new PropertyChangedEventArgs(name));
    }

    #endregion


}

然后,您的内容页面中的XAML将如下所示:

<StackLayout Padding="25">

<Button Text="Increase" Command="{Binding IncreaseCommand}"/>
<Label Text="{Binding Time, StringFormat='The time is {0:F0}'}"/>

</StackLayout>

确保在页面的xaml.cs中设置BindingContext:

public partial class MyPage : ContentPage
{
    public MyPage()
    {
        InitializeComponent();
        BindingContext = new MyViewModel();
    }
}

实际上实际实现的代码实际上并不多,因为您的ViewModel会有一个实现INotifyProprety的BaseViewModel已更改,所以实际上您只是添加了

 public int Time
    {
        get { return Settings.HotTimeCount; }
        set
        {
            if (Settings.HotTimeCount == value)
                return;

            Settings.HotTimeCount = value;
            OnPropertyChanged();
        }

    }

更神奇的方式

但是,使用C#的强大功能并了解Databinding的工作原理,您可以先创建一个所有内容都将使用的BaseViewModel:

 public class BaseViewModel : INotifyPropertyChanged
{

    public Settings Settings
    {
        get { return Settings.Current; }
    }


    #region INotifyPropertyChanged implementation

    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged([CallerMemberName]string name = "")
    {
        var changed = PropertyChanged;
        if (changed == null)
            return;
        changed(this, new PropertyChangedEventArgs(name));
    }

    #endregion


}

请注意我对 Settings.Current 的引用,我们现在需要将其作为单例实现,但我们将使用BaseViewModel,因此我们不必重新实现INotifyPropertyChanged:< / p>

public class Settings : BaseViewModel
{
    static ISettings AppSettings
    {
        get
        {
            return CrossSettings.Current;
        }
    }

    static Settings settings;
    public static Settings Current
    {
        get { return settings ?? (settings = new Settings()); }
    }

    #region Setting Constants

    const string HOT_TIME_COUNT_KEY = "hotTimeCount";
    static readonly int HOT_TIME_COUNT_DEFAULT = 0;

    #endregion

    public int HotTimeCount
    {
        get
        { 
            return AppSettings.GetValueOrDefault<int>(HOT_TIME_COUNT_KEY, HOT_TIME_COUNT_DEFAULT); 
        }
        set
        { 
            if (AppSettings.AddOrUpdateValue<int>(HOT_TIME_COUNT_KEY, value))
                OnPropertyChanged();

        }
    }
}

当然,我们仍然希望创建一个我们的XAML视图将绑定到的唯一ViewModel:

 public class MyViewModel : BaseViewModel
{
    private Command increase;
    public Command IncreaseCommand
    {
        get 
        { 
            return increase ?? (increase = new Command(() =>Settings.HotTimeCount++));
        }
    }
}

请注意,我们现在继承自BaseViewModel,这意味着我们的命令实际上只能递增Settings.HotTimeCount!但是现在我们必须调整我们的Xaml,看看我们的标签实际上是什么数据绑定:

 <StackLayout Padding="25">

<Button Text="Increase" Command="{Binding IncreaseCommand}"/>
<Label BindingContext="{Binding Settings}" Text="{Binding HotTimeCount, StringFormat='The time is {0:F0}'}"/>

</StackLayout>
</ContentPage.Content>

注意我将BindingContext设置为我们的设置,它位于我们用于Label的BaseViewModel中,必须这样做,因为它就是它现在所在的位置。你有它。

我将使用此信息更新我的自述文件。