您的位置:首页 > 编程语言 > C#

C#学习(九)- WP8.1开发的一些收获

2015-05-17 17:50 218 查看
本篇属于总结性记录,知识点会相对凌乱。

1. 关于SQLlite数据库的应用

  关于如何设置Visual Studio 2013使SQLlite数据库可用,参见http://www.cnblogs.com/tiny-home/p/4474861.html,在此不再赘述。主要分享我使用SQLlite过程中遇到的问题和收获。

  首先要建一个类,用以表示在SQLlite数据库中表示的记录,相当于普通数据库中的create语句,用来定义表中每条记录有多少列,每列是什么类型的量,每条记录就是一个对象。比如:

using SQLite;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Note.Model
{
public class Notes:ModelBase
{
     private int id;
[AutoIncrement, PrimaryKey] //主键并且自增
public int ID
{
get;
set;
}

private string name;
[MaxLength(1000)]
public string Name
{
get { return name; }
set
{ this.SetProperty(ref this.name, value); }
}

private string content;// = DateTime.Now.ToString();
[MaxLength(3000)]
public string Content

{
get { return content; }
set
{ this.SetProperty(ref this.content, value); }
}

private bool isImportant;
public bool IsImportant
{
get { return isImportant; }
set
{ this.SetProperty(ref this.isImportant, value); }
}
}
}


这个和普通的类声明很像,中括号中是对各属性(各列)加的限制。有一个细节可能需要注意,在你使用Visual Studio 2013开发WP8.1,当你更改了数据表的定义(也就是改了这个类),你需要重启虚拟机,否则会出现错误。

增加记录:

public async void Add(Notes note)//增加记事
{
SQLiteAsyncConnection conn = GetConn();
await conn.InsertAsync(note);
}


查找记录:

SQLiteAsyncConnection conn = GetConn();
var query = from note in conn.Table<Notes>()
where note.ID == id
select note;


删除记录:

public async void Delete(int id)//删除记事
{
SQLiteAsyncConnection conn = GetConn(); var query = from note in conn.Table<Notes>() where note.ID == id select note;
Notes notes = await query.FirstOrDefaultAsync();
await conn.DeleteAsync(notes);
}


2. 关于数据的Binding

{Binding}  表示与上一层所绑定的数据绑定

{Binding MyProperty}  表示与上一层所绑定的对象的特定属性绑定

{Binding Object.MyProperty}  表示与特定对象的特定属性绑定,与上一级所绑定的内容不再有关系

另外{Binding **** }有一个属性“Mode”,比如可以这么写:Text="{Binding NoteDemo.Content, Mode=TwoWay}"

表示Text与NoteDemo.Content绑定,并且是双向的,就是说当后台的NoteDemo.Conten发生变化,在Text的值会跟着变。而当你修改Text的值时,后台的NoteDemo.Conten也会跟着变。这种属性在两个量绑定时将会有一点十分方便,你不用再想还要去保存一个暂时量,双向绑定后相当于会自动保存。

3. RadioButton的使用

  RadioButton就是选择框控件,用来确定某项是否被选择。如:

<RadioButton Content="重要"

        IsChecked="{Binding NoteDemo.IsImportant,Mode=TwoWay}"

        Height="69"

        Width="213"

        FontSize="24" />

Content:指选择后的提示文字

IsChecked:选择框是否被选中,这里我将它与NoteDemo.IsImportant绑定,这是一个bool型变量

Height、Width:长和宽

FontSize:选择框的大小

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐