MVVM模式ViewModel之间的交互及View与ViewModel的关联
概要:
将所有的ViewModel在加载到Application的StaticResource中,然后在View中用标签指定。(StaticResource)
实现:
1)采用特性指定要添加到StaticResource中的对象
public class StaticResourceAttribute : Attribute
{
public string Key { get; set; }
public StaticResourceAttribute(string key)
{
this.Key = key;
}
}
2)从当前的程序集中,把所有标记了StaticResourceAttribute的ViewModel加载到AppResource中
public class ViewModelManager
{
private static Application app = Application.Current;
public static void InjectViewModelsToResources()
{
Assembly executingAssembly = Assembly.GetCallingAssembly();
foreach (Type type in executingAssembly.GetTypes())
{
var attributes = type.GetCustomAttributes(false);
foreach (var attribute in attributes)
{
if (attribute is StaticResourceAttribute)
{
var obj = Activator.CreateInstance(type);
if (!app.Resources.Contains(type.Name))
app.Resources.Add(type.Name, obj);
}
}
}
}
public static T GetViewModelFromResources()
{
var key = typeof(T).Name;
if (app.Resources.Contains(key))
return (T)app.Resources[key];
else
return default(T);
}
}
主窗体调用:
public partial class MainPage : UserControl
{
public MainPage()
{
ViewModelManager.InjectViewModelsToResources();
InitializeComponent();
}
}
View中绑定:
<UserControl x:Class="XXX .LoginView">
<UserControl.DataContext>
<Binding Source="{StaticResource LoginViewModel}"/>
</UserControl.DataContext>
</UserControl>
结论:
这样处理后,实现了VM的"单例",多个View关联同一个VM时可以共享数据。
本文来自jianyi的博客,原文地址:http://www.cnblogs.com/jianyi0115/archive/2011/06/08/2074992.html
原文链接:http://www.7fenx.com/between-the-association-and-the-view-and-viewmodel.html
将所有的ViewModel在加载到Application的StaticResource中,然后在View中用标签指定。(StaticResource)
实现:
1)采用特性指定要添加到StaticResource中的对象
public class StaticResourceAttribute : Attribute
{
public string Key { get; set; }
public StaticResourceAttribute(string key)
{
this.Key = key;
}
}
2)从当前的程序集中,把所有标记了StaticResourceAttribute的ViewModel加载到AppResource中
public class ViewModelManager
{
private static Application app = Application.Current;
public static void InjectViewModelsToResources()
{
Assembly executingAssembly = Assembly.GetCallingAssembly();
foreach (Type type in executingAssembly.GetTypes())
{
var attributes = type.GetCustomAttributes(false);
foreach (var attribute in attributes)
{
if (attribute is StaticResourceAttribute)
{
var obj = Activator.CreateInstance(type);
if (!app.Resources.Contains(type.Name))
app.Resources.Add(type.Name, obj);
}
}
}
}
public static T GetViewModelFromResources()
{
var key = typeof(T).Name;
if (app.Resources.Contains(key))
return (T)app.Resources[key];
else
return default(T);
}
}
主窗体调用:
public partial class MainPage : UserControl
{
public MainPage()
{
ViewModelManager.InjectViewModelsToResources();
InitializeComponent();
}
}
View中绑定:
<UserControl x:Class="XXX .LoginView">
<UserControl.DataContext>
<Binding Source="{StaticResource LoginViewModel}"/>
</UserControl.DataContext>
</UserControl>
结论:
这样处理后,实现了VM的"单例",多个View关联同一个VM时可以共享数据。
本文来自jianyi的博客,原文地址:http://www.cnblogs.com/jianyi0115/archive/2011/06/08/2074992.html
原文链接:http://www.7fenx.com/between-the-association-and-the-view-and-viewmodel.html
还没人转发这篇日记