Rob*_*zak 5 c# silverlight mvvm
我可以看到两种方法将ViewModel连接到View.一个在XAML中,另一个在后面的代码中通过依赖注入.
哪种方法更优选?我更喜欢xaml方法,因为我根本不需要代码中的任何代码,但是其中一个代码有什么问题吗?
<navigation:Page x:Class="MyNamespace.MyViewModel"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ViewModel="clr-namespace:MyNameSpace.MyViewModel"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
Title="ViewModel Page" >
<navigation:Page.Resources>
<ViewModel:MyViewModel x:Key="ViewModel"></ViewModel:MyViewModel>
</navigation:Page.Resources>
<Grid x:Name="LayoutRoot" Background="White"
DataContext="{StaticResource ViewModel}">
</Grid>
</navigation:Page>
Run Code Online (Sandbox Code Playgroud)
要么
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 System.Windows.Navigation;
namespace MyNamespace
{
public partial class MyView : Page
{
public MyView()
{
InitializeComponent(MyViewModel viewModel);
this.DataContext = viewModel;
}
}
}
Run Code Online (Sandbox Code Playgroud)
Shawn 首先有一篇关于View 或 ViewModel 的好文章。在 XAML 中使用 VM 可以为您提供可混合性(在 Blend 中查看示例数据),这很酷,但代价是必须将信息推回到视图中。由于这个原因,约翰·帕帕已经放弃了这种方法。
我正在使用肖恩的婚姻想法(请参阅上面的链接)。
HTH-埃里克
| 归档时间: |
|
| 查看次数: |
2647 次 |
| 最近记录: |