马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
Java欺骗了我们那么多年,如今的多核时代,我认为它气数已尽!在第二章中我们有讲到有DesignHelper类,上面是该类的源码:
usingSystem;
usingSystem.ComponentModel;
usingSystem.Diagnostics;
usingSystem.Diagnostics.CodeAnalysis;
usingSystem.Text;
usingSystem.Windows;
namespaceMvvmTutorial.Infrastructure
{
publicstaticclassDesignHelper
{
#regionFields
privatestaticbool?_isInDesignMode;
privatestaticreadonlyRandomRand=newRandom(Guid.NewGuid().GetHashCode());
#endregion//Fields
#regionProperties
///<summary>
///Getsavalueindicatingwhetherthecontrolisindesignmode
///(runninginBlendorVisualStudio).
///</summary>
[SuppressMessage(
"Microsoft.Security",
"CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands",
Justification="Thesecurityriskhereisneglectible.")]
publicstaticboolIsInDesignMode
{
get
{
if(!_isInDesignMode.HasValue)
{
#ifSILVERLIGHT
_isInDesignMode=DesignerProperties.IsInDesignTool;
#else
#ifWIN8
_isInDesignMode=Windows.ApplicationModel.DesignMode.DesignModeEnabled;
#else
varprop=DesignerProperties.IsInDesignModeProperty;
_isInDesignMode
=(bool)DependencyPropertyDescriptor
.FromProperty(prop,typeof(FrameworkElement))
.Metadata.DefaultValue;
//Justtobesure
if(!_isInDesignMode.Value
&&Process.GetCurrentProcess().ProcessName.StartsWith("devenv",StringComparison.Ordinal))
{
_isInDesignMode=true;
}
#endif
#endif
}
return_isInDesignMode.Value;
}
}
#endregion//Properties
#regionPublicMethods
///<summary>
///Getsarandomstring,givenitsminimumlengthandmaximumlength
///</summary>
publicstaticstringGetRandomString(intminLen=15,intmaxLen=50)
{
StringBuilderbuilder=newStringBuilder();
intlength=Rand.Next(minLen,maxLen);
charch;
for(inti=0;i<length;i++)
{
ch=Convert.ToChar(Convert.ToInt32(Math.Floor(26*Rand.NextDouble()+65)));
builder.Append(ch);
}
returnbuilder.ToString().ToLower();
}
publicstaticstringGetRandomNumericString(intminLen=15,intmaxLen=25)
{
StringBuilderbuilder=newStringBuilder();
intlength=Rand.Next(minLen,maxLen);
for(inti=0;i<length;i++)
{
builder.Append(Rand.Next(0,10).ToString());
}
returnbuilder.ToString().ToLower();
}
#endregion//PublicMethods
}
}
注重IsInDesignMode属性:这是我抄袭MvvmLight的代码。它的感化在于挪用它,你能够晓得以后你是处于开辟视图下仍是runtime情况下,甚么意义呢,等会儿再注释。另有一个newContact().GenerateRandomData(),这个GenerateRandomData实在来自于一个Helper类:在Models下增加ModelHelper:
usingMvvmTutorial.Infrastructure;
namespaceMvvmTutorial.Models
{
publicstaticclassModelHelper
{
publicstaticContactGenerateRandomData(thisContacttarget)
{
target.Name=DesignHelper.GetRandomString(5,15);
target.PhoneNumber=DesignHelper.GetRandomNumericString(8,12);
returntarget;
}
}
}
这个应当很简单了解,就是给一个Contact的属性增加一些随机的数据罢了,如许便于我们计划和测试。有了ContactMasterViewModel,我们一定必要ContactMasterView,因而在Views下增加ContactMasterView,其XAML代码以下:
<UserControlx:Class="MvvmTutorial.Views.ContactMasterView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:vm="clr-namespace:MvvmTutorial.ViewModels"
mc:Ignorable="d"d:DataContext="{BindingSource={StaticResourceDesignContext}}"
d:DesignHeight="300"d:DesignWidth="300">
<UserControl.Resources>
<vm:ContactMasterViewModelx:Key="DesignContext"/>
</UserControl.Resources>
<Grid>
<ListViewItemsSource="{BindingItems,Mode=OneWay}">
<ListView.View>
<GridView>
<GridViewColumnHeader="Name"Width="200">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlockText="{BindingName,Mode=OneWay}"VerticalAlignment="Center"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumnHeader="Phone"Width="150">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlockText="{BindingPhoneNumber,Mode=OneWay}"VerticalAlignment="Center"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Grid>
</UserControl>
请注重d:DataContext="{BindingSource={StaticResourceDesignContext}}"这里,我们给View绑下去一个DataContext,但是这只是开辟视图情况下的DataContext罢了。良多时分,当你做完View今后,你但愿间接能够在开辟视图中看到了局,如许一来你便不必要不休编译,运转,你能够很快取得修正View今后的视觉反应(我供认我这里的中文发言程度真的不高)。但是,还记得之前的DesignHelper.IsInDesignMode吗?它能够决意你以后是在运转程序,仍是在开辟视图下,假如在开辟视图下,我们就给ContactMasterViewModel.Items中增加一些随机数据,假如不是开辟视图,我们才真实的要从数据层前往一些实在数据(不在本章先容数据层)。
如今还缺甚么呢?ShellViewModel:在ViewModels下增加ShellViewModel:
usingMvvmTutorial.Infrastructure;
namespaceMvvmTutorial.ViewModels
{
publicclassShellViewModel:ObservableObject
{
#regionFields
ContactMasterViewModel_contactMaster;
#endregion//Fields
#regionProperties
publicContactMasterViewModelContactMaster
{
get
{
if(_contactMaster==null)
{
_contactMaster=newContactMasterViewModel();
}
return_contactMaster;
}
}
#endregion//Properties
}
}
将ShellView.xaml修正以下:
<Windowx:Class="MvvmTutorial.Views.ShellView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:view="clr-namespace:MvvmTutorial.Views"WindowStartupLocation="CenterScreen"
Title="ContactBook"Height="600"Width="800">
<Grid>
<view:ContactMasterViewDataContext="{BindingContactMaster}"/>
</Grid>
</Window>
这里应当很好了解,我们将ContactMasterView放到ShellView中,并为其指定了DataContext。那末ShellView的DataContext从那里来呢?再次,为了简化教程,我们用一种对照无邪间接的举措:修正该App.xaml.cs以下:
usingSystem.Windows;
usingMvvmTutorial.ViewModels;
usingMvvmTutorial.Views;
namespaceMvvmTutorial
{
///<summary>
///InteractionlogicforApp.xaml
///</summary>
publicpartialclassApp:Application
{
protectedoverridevoidOnStartup(StartupEventArgse)
{
base.OnStartup(e);
varshell=newShellView();
varshellViewModel=newShellViewModel();
shell.DataContext=shellViewModel;
shell.Show();
}
}
}
如今按F5,运转程序,顺遂的话,你应当能够看到一些随机数据显现在界面上。好了大致就先讲到这里。实在人人能够看到,我并没有甚么奇思妙想,基础上就是一起“抄袭”各路妙手的代码,然后本人拼集了个小例子罢了。编程老是有一些很流动的器材,并且这么多年来,全球的编程妙手,专家,都已写出来很多拿来即用的器材,我们必要做的是把握好基本,多念书,多存眷新常识,然后“拿来主义”,让这些好用的工具为我们服务。如果英语好,口才好,加上女孩子的优势说不定有机会进去做做别的工具) |