在Data.cs

namespace PicApp
{
[DataContract]
class DataItem : PropertyChangeNotification
{
public event PropertyChangedEventHandler PropertyChanged; [DataMember]
public int Id
{
get
{
return (this.id);
}
set
{
base.SetProperty(ref this.id, value);
}
}
[DataMember]
public Uri ImageUri
{
get
{
return (this.imageUri);
}
set
{
base.SetProperty(ref this.imageUri, value);
}
}
[DataMember]
public string Title
{
get
{
return (this.title);
}
set
{
base.SetProperty(ref this.title, value);
}
}
int id;
Uri imageUri;
string title;
}
// trying to keep this as simple, static and out of the way as possible.
static class Data
{
static List<DataItem> dataItems; public static async Task<IList<DataItem>> GetItemsAsync()
{
if (dataItems == null)
{
var localFolder = ApplicationData.Current.LocalFolder; try
{
var file = await localFolder.GetFileAsync(FILENAME); using (Stream netStream = await file.OpenStreamForReadAsync())
{
DataContractSerializer serializer = new DataContractSerializer(typeof(List<DataItem>));
dataItems = (List<DataItem>)serializer.ReadObject(netStream);
}
}
catch (FileNotFoundException)
{
}
if (dataItems == null)
{
dataItems = await BuildDataFromPackageAsync();
}
}
return (dataItems);
}
public static async Task SaveItemsAsync()
{
if (dataItems != null)
{
var localFolder = ApplicationData.Current.LocalFolder;
var file = await localFolder.CreateFileAsync(FILENAME, CreationCollisionOption.ReplaceExisting);
using (Stream netStream = await file.OpenStreamForWriteAsync())
{
DataContractSerializer serializer = new DataContractSerializer(typeof(List<DataItem>));
serializer.WriteObject(netStream, dataItems);
}
}
}
static async Task<List<DataItem>> BuildDataFromPackageAsync()
{
var folder = await Package.Current.InstalledLocation.GetFolderAsync("Images");
var files = await folder.GetFilesAsync();
return (files.Select(
(f,i) => new DataItem()
{
Id = i,
ImageUri = new Uri(string.Format(URI_BASE_FORMAT_STRING, f.Name)),
Title = f.Name
}).ToList());
}
static readonly string URI_BASE_FORMAT_STRING = "ms-appx:///Images/{0}";
static readonly string FILENAME = "pictures.xml";
}
}

在ViewModel.cs

namespace PicApp
{
class ViewModel : PropertyChangeNotification
{
public ViewModel()
{
this.isReadVisible = true;
}
public DataItem DataItem
{
get
{
return (this.dataItem);
}
set
{
base.SetProperty(ref this.dataItem, value);
this.titleCopy = this.dataItem.Title;
}
}
public void ConfirmTitleChanges()
{
this.titleCopy = this.dataItem.Title;
}
public void RevertTitleChanges()
{
this.dataItem.Title = this.titleCopy;
}
public void SetReadMode()
{
this.isReadVisible = true;
this.RaiseVisibilityChanges();
}
public void SetEditMode()
{
this.isReadVisible = false;
this.RaiseVisibilityChanges();
}
void RaiseVisibilityChanges()
{
base.OnPropertyChanged("IsReadVisible");
base.OnPropertyChanged("IsEditVisible");
}
public Visibility IsReadVisible
{
get
{
return (BoolToVisibility(this.isReadVisible));
}
}
public Visibility IsEditVisible
{
get
{
return (BoolToVisibility(!this.isReadVisible));
}
}
public bool IsEditing
{
get
{
return (!this.isReadVisible);
}
}
static Visibility BoolToVisibility(bool value)
{
return (value ? Visibility.Visible : Visibility.Collapsed);
}
string titleCopy;
DataItem dataItem;
bool isReadVisible;
}
}

wp8.1 C#技巧: Data和ViewModel类编写的更多相关文章

  1. WP8.1 Study5:Data binding数据绑定

    一.数据绑定 最简单的编程UI控件的方法是写自己的数据来获取和设置控件的属性,e.g. , textBox1.Text = "Hello, world"; 但在复杂的应用程序,这样 ...

  2. 设计模式之美:Private Class Data(私有类数据)

    索引 意图 结构 参与者 适用性 效果 实现 实现方式(一):实现对初始化数据的封装. 意图 封装类的初始化数据,控制对类的属性的更改,并保持类数据与使用数据的方法间的隔离. Encapsulate ...

  3. C# 利用MS的 EntLib的Database类编写的DbHelper

    C# 利用MS的 EntLib的Database类编写的DbHelper,由于MS的EntLib对Oracle.SQL Server和MySql已经封装,所以可以该DbHelper可以适用这三种数据库 ...

  4. IntelliJ IDEA 2017版 SpringBoot测试类编写

    SpringBoot的测试类编写Demo 源码见 https://github.com/liushaoye/baseone.git

  5. [深入浅出WP8.1(Runtime)]网络编程之HttpClient类

    12.2 网络编程之HttpClient类 除了可以使用HttpWebRequest类来实现HTTP网络请求之外,我们还可以使用HttpClient类来实现.对于基本的请求操作,HttpClient类 ...

  6. WP8解析JSON格式(使用DataContractJsonSerializer类)(推荐)

    DataContractJsonSerializer是.NET自带的类,在解析JSON格式的时候使用起来方便快捷,至于生成方面由于暂时没用到就没去看了.使用需要引用System.Runtime.Ser ...

  7. .Net批量插入数据到SQLServer数据库,System.Data.SqlClient.SqlBulkCopy类批量插入大数据到数据库

    批量的的数据导入数据库中,尽量少的访问数据库,高性能的对数据库进行存储. 采用SqlBulkCopy来处理存储数据.SqlBulkCopy存储大批量的数据非常的高效,将内存中的数据表直接的一次性的存储 ...

  8. wp8.1 C#技巧: 计时器

    public MainPage() { this.InitializeComponent(); this.timer = new DispatcherTimer();//新建委托时间实例 timer. ...

  9. Zabbix实战-简易教程--技巧一(操作类技巧)

    一.常用操作技巧 1.克隆 监控项.模板.触发器都可以进行克隆.其中,模板克隆又分为克隆和完全克隆.完全克隆会将LLD也一起复制一份,就是和之前的模板一模一样,而模板的克隆只是克隆监控项,不复制低层次 ...

随机推荐

  1. ios8中,相册创建后手动删除,不能再进行创建显示

    // Add a new ALAssetsGroup to the library. // The name of the ALAssetsGroup is name and the type is ...

  2. jQuery UI 实例 - 对话框(Dialog)(zhuan)

    http://www.runoob.com/jqueryui/example-dialog.html ************************************************* ...

  3. 批处理+VBS+注册表实现开机自动启动EXE程序

    批处理+VBS+注册表实现WINDOWS开机自动启动EXE程序 以下都是基于WINDOWS系统. 我们都知道当我们有想某个程序在开机时自动运行,只能有三个方式: 1.做成服务,然后对服务进行配置为自动 ...

  4. MVC HtmlHelper用法大全

    MVC HtmlHelper用法大全HtmlHelper用来在视图中呈现 HTML 控件.以下列表显示了当前可用的一些 HTML 帮助器. 本主题演示所列出的带有星号 (*) 的帮助器. ·Actio ...

  5. Hbase之Exception

    [hadoop@master hbase-1.2.2]$ ./bin/hbase shell2016-08-25 13:53:56,898 WARN [main] util.NativeCodeLoa ...

  6. 抛弃vboot不格盘用grub4dos+firadisk安装Ghost版XP到VHD,轻松RAMOS!

    http://bbs.wuyou.net/forum.php?mod=viewthread&tid=363198&extra=抛弃vboot不格盘用grub4dos+firadisk安 ...

  7. jsp 颜色和表格控制

    表格控制: table{table-layout:fixed; word-wrap:break-word; word-break:break-all;} 颜色列表:

  8. 日期操作类--DateFormat类

    简单的DateFormat格式化编码 时间模式字符串用来指定时间格式.在此模式中,所有的ASCII字母被保留为模式字母,定义如下: 字母 描述 示例 G 纪元标记 AD y 四位年份 2001 M 月 ...

  9. [redis] redis 存取键值对常用的三种使用方式 - Jedis、JedisPool、Jedis分布式

    |-Jedis 普通方式 |-JedisPool 连接池方式(需要引入pool相关jar) |-Jedis 分布式 (需要引入pool相关jar) 引入jedis2.7.0和commons.pool2 ...

  10. nyoj------203三国志

    三国志 时间限制:3000 ms  |  内存限制:65535 KB 难度:5  描述 <三国志>是一款很经典的经营策略类游戏.我们的小白同学是这款游戏的忠实玩家.现在他把游戏简化一下,地 ...