Quellcode durchsuchen

mvvmlight基本结构
1.Model使用IDataService接口取
2.ViewModel使用ViewModelLocator类取

tanghai vor 14 Jahren
Ursprung
Commit
d5c4849c6d

+ 5 - 15
CSharp/CSharp.sln

@@ -1,28 +1,18 @@
 
 Microsoft Visual Studio Solution File, Format Version 11.00
 # Visual Studio 2010
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Editor", "Editor\Editor.csproj", "{8C049112-56BF-42E3-8E91-30EABDB86B16}"
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Editor", "Editor\Editor.csproj", "{B29100C8-6E7F-4C7D-91AB-0153D0D28663}"
 EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
-		Debug|Mixed Platforms = Debug|Mixed Platforms
-		Debug|x86 = Debug|x86
 		Release|Any CPU = Release|Any CPU
-		Release|Mixed Platforms = Release|Mixed Platforms
-		Release|x86 = Release|x86
 	EndGlobalSection
 	GlobalSection(ProjectConfigurationPlatforms) = postSolution
-		{8C049112-56BF-42E3-8E91-30EABDB86B16}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
-		{8C049112-56BF-42E3-8E91-30EABDB86B16}.Debug|Any CPU.Build.0 = Debug|Any CPU
-		{8C049112-56BF-42E3-8E91-30EABDB86B16}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
-		{8C049112-56BF-42E3-8E91-30EABDB86B16}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
-		{8C049112-56BF-42E3-8E91-30EABDB86B16}.Debug|x86.ActiveCfg = Debug|Any CPU
-		{8C049112-56BF-42E3-8E91-30EABDB86B16}.Release|Any CPU.ActiveCfg = Release|Any CPU
-		{8C049112-56BF-42E3-8E91-30EABDB86B16}.Release|Any CPU.Build.0 = Release|Any CPU
-		{8C049112-56BF-42E3-8E91-30EABDB86B16}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
-		{8C049112-56BF-42E3-8E91-30EABDB86B16}.Release|Mixed Platforms.Build.0 = Release|Any CPU
-		{8C049112-56BF-42E3-8E91-30EABDB86B16}.Release|x86.ActiveCfg = Release|Any CPU
+		{B29100C8-6E7F-4C7D-91AB-0153D0D28663}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{B29100C8-6E7F-4C7D-91AB-0153D0D28663}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{B29100C8-6E7F-4C7D-91AB-0153D0D28663}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{B29100C8-6E7F-4C7D-91AB-0153D0D28663}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE

+ 13 - 14
CSharp/Editor/App.xaml

@@ -1,16 +1,15 @@
 <Application x:Class="Editor.App"
-             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
-             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
-             xmlns:vm="clr-namespace:Editor.ViewModel"
-             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
-             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
-             StartupUri="MainWindow.xaml"
-             mc:Ignorable="d">
-    
-    <Application.Resources>
-        <!--Global View Model Locator-->
-        <vm:ViewModelLocator x:Key="Locator"
-                             d:IsDataSource="True" />
-    </Application.Resources>
-    
+		xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+		xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+		xmlns:vm="clr-namespace:Editor.ViewModel"
+		xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
+		xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
+		StartupUri="MainWindow.xaml"
+		mc:Ignorable="d">
+
+	<Application.Resources>
+		<!--Global View Model Locator-->
+		<vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
+	</Application.Resources>
+
 </Application>

+ 0 - 16
CSharp/Editor/Design/DesignDataService.cs

@@ -1,16 +0,0 @@
-using System;
-using Editor.Model;
-
-namespace Editor.Design
-{
-	public class DesignDataService : IDataService
-	{
-		public void GetData(Action<DataItem, Exception> callback)
-		{
-			// Use this to create design time data
-
-			var item = new DataItem("Welcome to MVVM Light [design]");
-			callback(item, null);
-		}
-	}
-}

+ 30 - 5
CSharp/Editor/Editor.csproj

@@ -5,7 +5,7 @@
     <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
     <ProductVersion>8.0.30703</ProductVersion>
     <SchemaVersion>2.0</SchemaVersion>
-    <ProjectGuid>{8C049112-56BF-42E3-8E91-30EABDB86B16}</ProjectGuid>
+    <ProjectGuid>{B29100C8-6E7F-4C7D-91AB-0153D0D28663}</ProjectGuid>
     <OutputType>WinExe</OutputType>
     <AppDesignerFolder>Properties</AppDesignerFolder>
     <RootNamespace>Editor</RootNamespace>
@@ -70,12 +70,14 @@
       <Generator>MSBuild:Compile</Generator>
       <SubType>Designer</SubType>
     </ApplicationDefinition>
-    <Compile Include="Design\DesignDataService.cs" />
     <Compile Include="Model\DataItem.cs" />
     <Compile Include="Model\DataService.cs" />
     <Compile Include="Model\IDataService.cs" />
     <Compile Include="ViewModel\MainViewModel.cs" />
     <Compile Include="ViewModel\ViewModelLocator.cs" />
+    <Compile Include="View\MainView.xaml.cs">
+      <DependentUpon>MainView.xaml</DependentUpon>
+    </Compile>
     <Page Include="MainWindow.xaml">
       <Generator>MSBuild:Compile</Generator>
       <SubType>Designer</SubType>
@@ -92,13 +94,36 @@
       <Generator>MSBuild:Compile</Generator>
       <SubType>Designer</SubType>
     </Page>
+    <Page Include="View\MainView.xaml">
+      <SubType>Designer</SubType>
+      <Generator>MSBuild:Compile</Generator>
+    </Page>
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="Properties\AssemblyInfo.cs">
+      <SubType>Code</SubType>
+    </Compile>
+    <Compile Include="Properties\Resources.Designer.cs">
+      <AutoGen>True</AutoGen>
+      <DesignTime>True</DesignTime>
+      <DependentUpon>Resources.resx</DependentUpon>
+    </Compile>
+    <Compile Include="Properties\Settings.Designer.cs">
+      <AutoGen>True</AutoGen>
+      <DependentUpon>Settings.settings</DependentUpon>
+      <DesignTimeSharedInput>True</DesignTimeSharedInput>
+    </Compile>
+    <EmbeddedResource Include="Properties\Resources.resx">
+      <Generator>ResXFileCodeGenerator</Generator>
+      <LastGenOutput>Resources.Designer.cs</LastGenOutput>
+    </EmbeddedResource>
+    <None Include="Properties\Settings.settings">
+      <Generator>SettingsSingleFileGenerator</Generator>
+      <LastGenOutput>Settings.Designer.cs</LastGenOutput>
+    </None>
     <AppDesigner Include="Properties\" />
   </ItemGroup>
-  <ItemGroup>
-    <Folder Include="Properties\" />
-  </ItemGroup>
+  <ItemGroup />
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
   <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
        Other similar extension points exist, see Microsoft.Common.targets.

+ 25 - 24
CSharp/Editor/MainWindow.xaml

@@ -1,24 +1,25 @@
-<Window x:Class="Editor.MainWindow"
-		xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
-		xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
-		xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
-		xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
-		mc:Ignorable="d"
-		Height="600"
-		Width="800"
-		Title=""
-		DataContext="{Binding Main, Source={StaticResource Locator}}">
-
-	<Window.Resources>
-		<ResourceDictionary>
-			<ResourceDictionary.MergedDictionaries>
-				<ResourceDictionary Source="Skins/MainSkin.xaml" />
-			</ResourceDictionary.MergedDictionaries>
-		</ResourceDictionary>
-	</Window.Resources>
-
-	<TabControl HorizontalAlignment="Stretch" VerticalAlignment="Stretch" VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" FlowDirection="LeftToRight" BorderThickness="0">
-		<TabItem Header="Main" Name="mainTabItem" Padding="10,2" Margin="0">
-		</TabItem>
-	</TabControl>
-</Window>
+<Window x:Class="Editor.MainWindow"
+		xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+		xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+		xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
+		xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
+		xmlns:v="clr-namespace:Editor.View"
+		mc:Ignorable="d" Height="600" Width="800" Title="MVVM Light Application"
+		DataContext="{Binding Main, Source={StaticResource Locator}}">
+
+	<Window.Resources>
+		<ResourceDictionary>
+			<ResourceDictionary.MergedDictionaries>
+				<ResourceDictionary Source="Skins/MainSkin.xaml" />
+			</ResourceDictionary.MergedDictionaries>
+		</ResourceDictionary>
+	</Window.Resources>
+
+	<Grid x:Name="LayoutRoot">
+		<TabControl HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
+			<TabItem Header="Main" Name="mainTabItem">
+				<v:MainView />
+			</TabItem>
+		</TabControl>
+	</Grid>
+</Window>

+ 2 - 2
CSharp/Editor/Skins/MainSkin.xaml

@@ -1,4 +1,4 @@
 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
-                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
-    
+					xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
+	
 </ResourceDictionary>

+ 9 - 0
CSharp/Editor/View/MainView.xaml

@@ -0,0 +1,9 @@
+<UserControl x:Class="Editor.View.MainView"
+		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" 
+		mc:Ignorable="d" d:DesignHeight="600" d:DesignWidth="800"
+		DataContext="{Binding Main, Source={StaticResource Locator}}" >
+	<Grid></Grid>
+</UserControl>

+ 27 - 0
CSharp/Editor/View/MainView.xaml.cs

@@ -0,0 +1,27 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Data;
+using System.Windows.Documents;
+using System.Windows.Input;
+using System.Windows.Media;
+using System.Windows.Media.Imaging;
+using System.Windows.Navigation;
+using System.Windows.Shapes;
+
+namespace Editor.View
+{
+	/// <summary>
+	/// MainView.xaml 的交互逻辑
+	/// </summary>
+	public partial class MainView : UserControl
+	{
+		public MainView()
+		{
+			InitializeComponent();
+		}
+	}
+}

+ 5 - 11
CSharp/Editor/ViewModel/MainViewModel.cs

@@ -33,14 +33,12 @@ namespace Editor.ViewModel
 			{
 				return welcomeTitle;
 			}
-
 			set
 			{
 				if (welcomeTitle == value)
 				{
 					return;
 				}
-
 				welcomeTitle = value;
 				RaisePropertyChanged(WelcomeTitlePropertyName);
 			}
@@ -52,24 +50,20 @@ namespace Editor.ViewModel
 		public MainViewModel(IDataService dataService)
 		{
 			this.dataService = dataService;
-			this.dataService.GetData(
-				(item, error) =>
+			dataService.GetData((item, error) =>
 				{
 					if (error != null)
 					{
 						// Report error here
 						return;
 					}
-
 					WelcomeTitle = item.Title;
 				});
 		}
 
-		////public override void Cleanup()
-		////{
-		////    // Clean up if needed
-
-		////    base.Cleanup();
-		////}
+		public override void Cleanup()
+		{
+		    base.Cleanup();
+		}
 	}
 }

+ 1 - 8
CSharp/Editor/ViewModel/ViewModelLocator.cs

@@ -33,14 +33,7 @@ namespace Editor.ViewModel
 		{
 			ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
 
-			if (ViewModelBase.IsInDesignModeStatic)
-			{
-				SimpleIoc.Default.Register<IDataService, Design.DesignDataService>();
-			}
-			else
-			{
-				SimpleIoc.Default.Register<IDataService, DataService>();
-			}
+			SimpleIoc.Default.Register<IDataService, DataService>();
 
 			SimpleIoc.Default.Register<MainViewModel>();
 		}