如何使用 MVVM 为 Windows Phone 8 创建本地数据库应用

如何使用 MVVM 为 Windows Phone 8 创建本地数据库应用,第1张

选择开始菜单中→程序→【Management

SQL

Server

2008】→【SQL

Server

Management

Studio】命令,打开【SQL

Server

Management

Studio】窗口,并使用Windows或

SQL

Server身份验证建立连接。

在【对象资源管理器】窗口中展开服务器,然后选择【数据库】节点

右键单击【数据库】节点,从d出来的快捷菜单中选择【新建数据库】命令。

执行上述 *** 作后,会d出【新建数据库】对话框。在对话框、左侧有3个选项,分别是【常规】、【选项】和【文件组】。完成这三个选项中的设置会后,就完成了数据库的创建工作,

在【数据库名称】文本框中输入要新建数据库的名称。例如,这里以“新建的数据库”。

在【所有者】文本框中输入新建数据库的所有者,如sa。根据数据库的使用情况,选择启用或者禁用【使用全文索引】复选框。

在【数据库文件】列表中包括两行,一行是数据库文件,而另一行是日记文件。通过单击下面的【添加】、【删除】按钮添加或删除数据库文件。

切换到【选项页】、在这里可以设置数据库的排序规则、恢复模式、兼容级别和其他属性。

切换到【文件组】页,在这里可以添加或删除文件组。

完成以上 *** 作后,单击【确定】按钮关闭【新建数据库】对话框。至此“新建的数据”数据库创建成功。新建的数据库可以再【对象资源管理器】窗口看到。

创建应用 UI

若使用 Windows Phone SDK,则使用 Windows Phone 应用 模板创建新项目。

创建好项目后,从 “项目” 菜单中选择 “添加现有项”。该 *** 作将打开“添加现有项”菜单,我们可以从中选择用于删除待办事项的图标

在“添加现有项”窗口中,导航到以下路径并选择命名为 appbar.delete.rest.png 的图标。该图标设计用于深色背景并且颜色为白色;该图标在“添加现有项”窗口的白色背景中可能显示为空白。

C:\Program Files (x86)\Microsoft SDKs\Windows Phone\v8.0\Icons\dark

注意:

此步骤假定采用的是以默认方式安装的 Visual Studio。如果您将其安装在其他位置,则在相应的位置查找图标。

单击“添加”。该 *** 作将图标添加到解决方案资源管理器中的列表。

在“解决方案资源管理器”中,右键单击图标并设置文件属性,以便以“内容”的形式生成图标并且始终复制到输出目录(“始终复制”)。

在 MainPage.xaml 上,删除名为 LayoutRoot 的 Grid 的 XAML 代码,并替换为以下代码。

XAML

<!--LayoutRoot is the root grid where all page content is placed.-->

<Grid x:Name="LayoutRoot" Background="Transparent">

<Grid.RowDefinitions>

<RowDefinition Height="Auto"/>

<RowDefinition Height="*"/>

</Grid.RowDefinitions>

<!--TitlePanel contains the name of the application and page title.-->

<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">

<TextBlock x:Name="ApplicationTitle" Text="LOCAL DATABASE EXAMPLE" Style="{StaticResource PhoneTextNormalStyle}"/>

<TextBlock x:Name="PageTitle" Text="to-do list" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>

</StackPanel>

<!--ContentPanel - place additional content here.-->

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">

<Grid.RowDefinitions>

<RowDefinition Height="Auto" />

<RowDefinition Height="Auto" />

</Grid.RowDefinitions>

<!-- Bind the list box to the observable collection. -->

<ListBox x:Name="toDoItemsListBox" ItemsSource="{Binding ToDoItems}"

Grid.Row="0" Margin="12, 0, 12, 0" Width="440">

<ListBox.ItemTemplate>

<DataTemplate>

<Grid HorizontalAlignment="Stretch" Width="440">

<Grid.ColumnDefinitions>

<ColumnDefinition Width="50" />

<ColumnDefinition Width="*" />

<ColumnDefinition Width="100" />

</Grid.ColumnDefinitions>

<CheckBox

IsChecked="{Binding IsComplete, Mode=TwoWay}"

Grid.Column="0"

VerticalAlignment="Center"/>

<TextBlock

Text="{Binding ItemName}"

FontSize="{StaticResource PhoneFontSizeLarge}"

Grid.Column="1"

VerticalAlignment="Center"/>

<Button

Grid.Column="2"

x:Name="deleteTaskButton"

BorderThickness="0"

Margin="0"

Click="deleteTaskButton_Click">

<Image Source="appbar.delete.rest.png"/>

</Button>

</Grid>

</DataTemplate>

</ListBox.ItemTemplate>

</ListBox>

<Grid Grid.Row="1">

<Grid.ColumnDefinitions>

<ColumnDefinition Width="*" />

<ColumnDefinition Width="Auto" />

</Grid.ColumnDefinitions>

<TextBox

x:Name="newToDoTextBox"

Grid.Column="0"

Text="add new task"

FontFamily="{StaticResource PhoneFontFamilyLight}"

GotFocus="newToDoTextBox_GotFocus"/>

<Button

Content="add"

Grid.Column="1"

x:Name="newToDoAddButton"

Click="newToDoAddButton_Click"/>

</Grid>

</Grid>

</Grid>

XAML 代码将向应用添加两个 Grid 元素。其中一个 Grid 包含显示待办事项的 toDoItemsListBox。随着绑定到列表的待办事项越多,ListBox 的尺寸将有所增加,逐渐将第二个 Grid 推向屏幕下方。另一个 Grid 元素包含用来输入新待办事项的 newToDoTextBox 和Button。

生成数据上下文

在本节中,将指定一个确定数据库架构的对象模型并创建数据上下文。

生成数据上下文

打开主页的代码隐藏文件(名为 MainPage.xaml.cs)。该页面将包含大部分应用逻辑。为了简便起见,该应用将特意保留在一个页面中。实际应用通常会使用模型-视图-视图模型 (MVVM) 编程模式。有关更多信息,请参见实现面向 Windows Phone 8 的模型视图查看模型模式。

该应用需要引用 Windows Phone 的 LINQ to SQL 程序集。从“项目”菜单中,单击“添加引用”,从“程序集/框架”列表中选择“System.Data.Linq”,然后单击“确定”。

注意:

当面向 Windows Phone 8 时,使用 Windows Phone 应用 模板的新应用自动引用该程序集。

在页面顶部添加以下指令。

C#

using System.Data.Linq

using System.Data.Linq.Mapping

using System.ComponentModel

using System.Collections.ObjectModel

在 MainPage 类下,添加以下代码。这是一个名为 ToDoItem 的实体类,表示本地数据库中应用的数据库表。该类实现INotifyPropertyChanged 以进行更改跟踪。实现 INotifyPropertyChanging 有助于限制与更改跟踪相关的内存占用。表属性 [Table] 指示 LINQ to SQL 运行时将类映射到本地数据库表。

C#

[Table]

public class ToDoItem : INotifyPropertyChanged, INotifyPropertyChanging

{

// Define ID: private field, public property and database column.

private int _toDoItemId

[Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]

public int ToDoItemId

{

get

{

return _toDoItemId

}

set

{

if (_toDoItemId != value)

{

NotifyPropertyChanging("ToDoItemId")

_toDoItemId = value

NotifyPropertyChanged("ToDoItemId")

}

}

}

// Define item name: private field, public property and database column.

private string _itemName

[Column]

public string ItemName

{

get

{

return _itemName

}

set

{

if (_itemName != value)

{

NotifyPropertyChanging("ItemName")

_itemName = value

NotifyPropertyChanged("ItemName")

}

}

}

// Define completion value: private field, public property and database column.

private bool _isComplete

[Column]

public bool IsComplete

{

get

{

return _isComplete

}

set

{

if (_isComplete != value)

{

NotifyPropertyChanging("IsComplete")

_isComplete = value

NotifyPropertyChanged("IsComplete")

}

}

}

// Version column aids update performance.

[Column(IsVersion = true)]

private Binary _version

#region INotifyPropertyChanged Members

public event PropertyChangedEventHandler PropertyChanged

// Used to notify the page that a data context property changed

private void NotifyPropertyChanged(string propertyName)

{

if (PropertyChanged != null)

{

PropertyChanged(this, new PropertyChangedEventArgs(propertyName))

}

}

#endregion

#region INotifyPropertyChanging Members

public event PropertyChangingEventHandler PropertyChanging

// Used to notify the data context that a data context property is about to change

private void NotifyPropertyChanging(string propertyName)

{

if (PropertyChanging != null)

{

PropertyChanging(this, new PropertyChangingEventArgs(propertyName))

}

}

#endregion

}

ToDoItem 类具有三个公共属性,分别与三个数据库列相对应:

重要说明:

为了限制更改跟踪所需的内存占用,请始终对数据上下文中的对象实现 INotifyPropertyChanging 接口。

ToDoItemId:由数据库自动填充的标识符列。同时,该列也是一个主键,系统会为其自动创建数据库索引。这些以及其他设置均使用在属性语法上方编写的 LINQ to SQL Column 映射属性指定。

ItemName:存储待办事项文本的列。

IsComplete:存储待办事项完成状态的列。

在 MainPage 类下,添加以下代码。这是一个名为 ToDoDataContext 的类。该类继承自 DataContext,被称为数据上下文。最重要的是,该代码将调用基构造函数并声明名为 ToDoItems 的数据库表。

C#

public class ToDoDataContext : DataContext

{

// Specify the connection string as a static, used in main page and app.xaml.

public static string DBConnectionString = "Data Source=isostore:/ToDo.sdf"

// Pass the connection string to the base class.

public ToDoDataContext(string connectionString)

: base(connectionString)

{ }

// Specify a single table for the to-do items.

public Table<ToDoItem>ToDoItems

}

注意:

不要求数据库连接字符串必须为静态字段,该特定示例中仅为方便使用而要求。有关连接字符串的更多信息,请参阅 Windows Phone 8 的本地数据库连接字符串。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/9922913.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-03
下一篇2023-05-03

发表评论

登录后才能评论

评论列表(0条)

    保存