上一篇文章已经建立了基本的实体类,并且搞定了多语言的问题,以后在app里用字符串的时候就可以从资源文件中取了。现在继续进行。

一、添加一个页面

CurrencyExchanger首页是一个货币兑换的列表,这个列表比较复杂,我们先不管,先从简单的页面做起。首先要有一个添加货币的页面,显示所有可添加的货币列表。在WP项目中右键添加一个页面,命名为AddCurrencyPage.xaml。

然后MVVM-Sidekick自动添加了一些东西:除了这个页面之外,在WP项目的ViewModels目录中添加了一个AddCurrencyPage_Model.cs文件,在StartUps目录中添加了一个AddCurrencyPage.cs文件。打开看一下:

public static void ConfigAddCurrencyPage()
        {
            ViewModelLocator<AddCurrencyPage_Model>
                .Instance
                .Register(context =>
                    new AddCurrencyPage_Model())
                .GetViewMapper()
                .MapToDefault<AddCurrencyPage>();

}

 

看到了吧,所有的View和ViewModel都要进行一下配置才能用。第一次用MVVM-Sidekick的时候我没装vs扩展,直接引用的类库,自己手动建立VM,不知道要进行配置,结果死活绑定不上。问作者才知道有这么个东西,所以一定要装vs扩展插件才可以享受到这个便利。

二、实现第一个Binding列表

在AddCurrencyPage_Model.cs文件中,通过使用propvm代码段的方式添加以下两个属性:

public string AppName
        {
            get { return _AppNameLocator(this).Value; }
            set { _AppNameLocator(this).SetValueAndTryNotify(value); }
        }
        #region Property string AppName Setup
        protected Property<string> _AppName = new Property<string> { LocatorFunc = _AppNameLocator };
        static Func<BindableBase, ValueContainer<string>> _AppNameLocator = RegisterContainerLocator<string>("AppName", model => model.Initialize("AppName", ref model._AppName, ref _AppNameLocator, _AppNameDefaultValueFactory));
        static Func<string> _AppNameDefaultValueFactory = () => { return AppResources.AppName; };
        #endregion


        public string PageName
        {
            get { return _PageNameLocator(this).Value; }
            set { _PageNameLocator(this).SetValueAndTryNotify(value); }
        }
        #region Property string PageName Setup
        protected Property<string> _PageName = new Property<string> { LocatorFunc = _PageNameLocator };
        static Func<BindableBase, ValueContainer<string>> _PageNameLocator = RegisterContainerLocator<string>("PageName", model => model.Initialize("PageName", ref model._PageName, ref _PageNameLocator, _PageNameDefaultValueFactory));
        static Func<string> _PageNameDefaultValueFactory = () => { return AppResources.AddCurrency_PageName; };
        #endregion
View Code

相关文章: