日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

第一个MVVM wp7程序

發(fā)布時(shí)間:2025/3/15 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 第一个MVVM wp7程序 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

創(chuàng)建一個(gè)MVVM wp7程序,從手動(dòng)組建MVVM程序到使用MVVM Light Toolkit快速創(chuàng)建MVVM程序

一、一步步走向MVVM(一個(gè)簡(jiǎn)單的好友列表)

打開vs2010 Express for windows phone,創(chuàng)建一個(gè)Windows Phone Application

這是開始的項(xiàng)目結(jié)構(gòu)

創(chuàng)建連個(gè)文件夾Model和ViewModel,并在ViewModel中添加類ViewModel,實(shí)現(xiàn)INotifyPropertyChanged接口,并對(duì)PropertyChanged進(jìn)行一點(diǎn)封裝

MainViewModel.cs

MainViewModelusing System; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Ink; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using System.ComponentModel; using System.Collections.ObjectModel;using HelloWp7.Model; namespace HelloWp7.ViewModel {public class MainViewModel : INotifyPropertyChanged{public MainViewModel(){//初始化數(shù)據(jù)_title = "Friends Book";_friends = new ObservableCollection<Friend>(friendService.GetFriends());}#region dataServicesprivate FriendService friendService=new FriendService();#endregion/// <summary>/// 應(yīng)用名稱/// </summary>private string _appName;public string AppName{get{return _appName;}set{if (_appName == value){return;}_appName = value;RaisePropertyChanged("AppName");}}private string _title;/// <summary>/// 標(biāo)題/// </summary>public string Title{get{return _title;}set{if (_title == value){return;}_title = value;RaisePropertyChanged("Title");}}private ObservableCollection<Friend> _friends;public ObservableCollection<Friend> Friends{get{return _friends;}set{if (value == _friends){return;}_friends = value;RaisePropertyChanged("Friends");}}public event PropertyChangedEventHandler PropertyChanged;public void RaisePropertyChanged(string propertyName){if (PropertyChanged != null){PropertyChanged(this, new PropertyChangedEventArgs(propertyName));}}} }

?

前臺(tái)界面:

1.先添加對(duì)ViewModel的命名空間的引用

xmlns:mv="clr-namespace:HelloWp7.ViewModel"

?

2.將MainViewModel作為資源配置在頁(yè)面上

<phone:PhoneApplicationPage.Resources><!--配置ViewModel為資源,執(zhí)行時(shí)會(huì)直接初始化--><mv:MainViewModel x:Name="MainViewModel"></mv:MainViewModel></phone:PhoneApplicationPage.Resources>

?

3.將MainViewModel作為DataContext綁定到頁(yè)面最頂層的Controll上

<Grid x:Name="LayoutRoot" Background="Transparent" DataContext="{StaticResource MainViewModel}">4.到具體要顯示的控件上綁定要顯示的內(nèi)容就行了MainPage.xaml<phone:PhoneApplicationPage x:Class="HelloWp7.MainPage"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:mv="clr-namespace:HelloWp7.ViewModel"mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"FontFamily="{StaticResource PhoneFontFamilyNormal}"FontSize="{StaticResource PhoneFontSizeNormal}"Foreground="{StaticResource PhoneForegroundBrush}"SupportedOrientations="Portrait" Orientation="Portrait"shell:SystemTray.IsVisible="True"><phone:PhoneApplicationPage.Resources><!--配置ViewModel為資源,執(zhí)行時(shí)會(huì)直接初始化--><mv:MainViewModel x:Name="MainViewModel"></mv:MainViewModel><DataTemplate x:Name="FriendTemplate" ><StackPanel Width="400" Margin="0,5" VerticalAlignment="Top"><TextBlock FontSize="39" Text="{Binding Name}"></TextBlock><StackPanel Orientation="Horizontal"><TextBlock Text="{Binding Address}" VerticalAlignment="Top"></TextBlock> <TextBlock Text="{Binding PhoneNum}" Margin="10,0" VerticalAlignment="Top"></TextBlock></StackPanel></StackPanel></DataTemplate><Style x:Key="frdlist" TargetType="ListBox"><Setter Property="BorderBrush" Value="White"></Setter></Style></phone:PhoneApplicationPage.Resources><!--LayoutRoot is the root grid where all page content is placed--><Grid x:Name="LayoutRoot" Background="Transparent" DataContext="{StaticResource MainViewModel}"><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="{Binding AppName}" Style="{StaticResource PhoneTextNormalStyle}"/><TextBlock x:Name="PageTitle" Text="{Binding Title}" 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> <Canvas Grid.Row="1"><ListBox ItemsSource="{Binding Friends}" ItemTemplate="{StaticResource FriendTemplate}" Width="468" Height="370" Canvas.Top="0" /> </Canvas></Grid><!--Sample code showing usage of ApplicationBar--><!--<phone:PhoneApplicationPage.ApplicationBar><shell:ApplicationBar IsVisible="True" IsMenuEnabled="True"><shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Button 1"/><shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="Button 2"/><shell:ApplicationBar.MenuItems><shell:ApplicationBarMenuItem Text="MenuItem 1"/><shell:ApplicationBarMenuItem Text="MenuItem 2"/></shell:ApplicationBar.MenuItems></shell:ApplicationBar></phone:PhoneApplicationPage.ApplicationBar>--></phone:PhoneApplicationPage>

?

MainPage.xaml.cs中沒(méi)有任何代碼(MVVM中不會(huì)有事件處理了而是用Command綁定就可以了)

using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using Microsoft.Phone.Controls;namespace HelloWp7 {public partial class MainPage : PhoneApplicationPage{// Constructorpublic MainPage(){InitializeComponent();}} }

?

另外兩個(gè)Model類

Friend.cs

using System; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Ink; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes;namespace HelloWp7.Model {public class Friend{ public string Name { get; set; }public string Address { get; set; }public string Pic { get; set; }public string PhoneNum { get; set; }} }

?

FriendService.cs

using System; using System.Collections.Generic;namespace HelloWp7.Model {public class FriendService{public List<Friend> GetFriends(){List<Friend> friends = new List<Friend>() { new Friend(){ Name="Johnny", Address="北京", PhoneNum="13621010899"}, new Friend(){ Name="David", Address="上海", PhoneNum="23621010899"}, new Friend(){ Name="John", Address="天津", PhoneNum="33621010899"}, new Friend(){ Name="Susan", Address="武漢", PhoneNum="43621010899"}, new Friend(){ Name="Badboy", Address="深圳", PhoneNum="53621010899"},new Friend(){ Name="Jobs", Address="廣州", PhoneNum="11621010899"}, new Friend(){ Name="kevin", Address="深圳", PhoneNum="53621010899"}};return friends;}} }

?

項(xiàng)目目前結(jié)構(gòu)為:

按F5發(fā)布到模擬器(Windows Phone Emulator)中效果為

?

下載源碼

轉(zhuǎn)載于:https://www.cnblogs.com/yoainet/archive/2011/12/07/2279232.html

總結(jié)

以上是生活随笔為你收集整理的第一个MVVM wp7程序的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。