关闭和更改 ViewModel 时执行功能 - 使用 MVVM 的 C#
Do functions when closing and when changing ViewModel - C# using MVVM
我有一些问题,但找不到答案,所以我决定把它们放在这里。
Q1:每次应用程序关闭时我都必须调用一个函数,比如:点击退出按钮然后做一些事情。
Q2: 我在 shell viewmodel 中有一个控制 ViewModel 的 menuitemcontrol 但是在创建它们时我做了一些 web 服务请求,但假设我删除了一个朋友它被要求更新视图模型中的请求,如何从其他视图模型执行此调用?
编辑:场景 - 包含 HomeViewModel 和 FriendsViewModel 的 ShellViewModel,我在 FriendsViewModel 中接受了一个朋友 我希望当我单击 Home 时从 web 服务获取信息的功能运行 再次。 (如果我在代码隐藏中做,我会使用 Onclick[Home] > 运行login())
更新Q2:
public FriendsViewModel()
{
MessengerInstance.Register<NotificationMessage>(this, NotifyMe);
au = AuthSingleton.Instance.getAuthUser(); // Singleton that works like a session in the desktop App.
if (AuthSingleton.Instance.IsAuth == true)
loadFriends();
}
public void NotifyMe(NotificationMessage notificationMessage)
{
string notification = notificationMessage.Notification;
//do your work
loadFriends();
}
#endregion constructors
public async void loadFriends()
{
var response = await CommunicationWebServices.GetASM(au.idUser + "/friends", au.token);
var fh = JsonConvert.DeserializeObject<FriendsHandler>(response);
}
我决定使用评论者用户的建议,从第二个 ViewModel 向这个 ViewModel 发送一条消息,以再次订购 运行 的更新(非常酷且简单的解决方案),但是 它不起作用 因为我的单例被删除了 :O
消息已发送: MessengerInstance.Send(new NotificationMessage("notification message"));
此致,
Q1 - 您使用的是什么 MVVM 框架?我知道的所有 MVVM 框架都实现了自定义命令(又名 RelayCommands/DelegatingCommands),因此您可以将它们附加到 Window 事件。另一种解决方案是在您的 ViewModel 中实现 ClosingRequest 事件。像这样:
public class BaseViewModel
{
public event EventHandler ClosingRequest;
protected void OnClosingRequest()
{
if (this.ClosingRequest != null)
{
this.ClosingRequest(this, EventArgs.Empty);
}
}
}
因此,在您的视图中您将拥有:
public partial class MainWindow: Window
{
...
var vm = new BaseViewModel();
this.Datacontext = vm;
vm.ClosingRequest += (sender, e) => this.Close();
}
如果您使用的是 MVVM Light,则可以在 ViewModel 中执行以下操作:
public ICommand CmdWindowClosing
{
get
{
return new RelayCommand<CancelEventArgs>(
(args) =>{
});
}
}
在你的 Window 中:
<i:Interaction.Triggers>
<i:EventTrigger EventName="Closing">
<command:EventToCommand Command="{Binding CmdWindowClosing}" PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
Q2 - 此外,使用 MVVM 框架时这很容易。它们中的大多数实现了消息中介器模式。这对你意味着什么。这意味着您可以发送消息警告 "Request needs update" 并且接收器绑定到该消息,在收到消息时执行某些操作。看看这个来自 Microsoft
的 demo
我有一些问题,但找不到答案,所以我决定把它们放在这里。
Q1:每次应用程序关闭时我都必须调用一个函数,比如:点击退出按钮然后做一些事情。
Q2: 我在 shell viewmodel 中有一个控制 ViewModel 的 menuitemcontrol 但是在创建它们时我做了一些 web 服务请求,但假设我删除了一个朋友它被要求更新视图模型中的请求,如何从其他视图模型执行此调用?
编辑:场景 - 包含 HomeViewModel 和 FriendsViewModel 的 ShellViewModel,我在 FriendsViewModel 中接受了一个朋友 我希望当我单击 Home 时从 web 服务获取信息的功能运行 再次。 (如果我在代码隐藏中做,我会使用 Onclick[Home] > 运行login())
更新Q2:
public FriendsViewModel()
{
MessengerInstance.Register<NotificationMessage>(this, NotifyMe);
au = AuthSingleton.Instance.getAuthUser(); // Singleton that works like a session in the desktop App.
if (AuthSingleton.Instance.IsAuth == true)
loadFriends();
}
public void NotifyMe(NotificationMessage notificationMessage)
{
string notification = notificationMessage.Notification;
//do your work
loadFriends();
}
#endregion constructors
public async void loadFriends()
{
var response = await CommunicationWebServices.GetASM(au.idUser + "/friends", au.token);
var fh = JsonConvert.DeserializeObject<FriendsHandler>(response);
}
我决定使用评论者用户的建议,从第二个 ViewModel 向这个 ViewModel 发送一条消息,以再次订购 运行 的更新(非常酷且简单的解决方案),但是 它不起作用 因为我的单例被删除了 :O
消息已发送: MessengerInstance.Send(new NotificationMessage("notification message"));
此致,
Q1 - 您使用的是什么 MVVM 框架?我知道的所有 MVVM 框架都实现了自定义命令(又名 RelayCommands/DelegatingCommands),因此您可以将它们附加到 Window 事件。另一种解决方案是在您的 ViewModel 中实现 ClosingRequest 事件。像这样:
public class BaseViewModel
{
public event EventHandler ClosingRequest;
protected void OnClosingRequest()
{
if (this.ClosingRequest != null)
{
this.ClosingRequest(this, EventArgs.Empty);
}
}
}
因此,在您的视图中您将拥有:
public partial class MainWindow: Window
{
...
var vm = new BaseViewModel();
this.Datacontext = vm;
vm.ClosingRequest += (sender, e) => this.Close();
}
如果您使用的是 MVVM Light,则可以在 ViewModel 中执行以下操作:
public ICommand CmdWindowClosing
{
get
{
return new RelayCommand<CancelEventArgs>(
(args) =>{
});
}
}
在你的 Window 中:
<i:Interaction.Triggers>
<i:EventTrigger EventName="Closing">
<command:EventToCommand Command="{Binding CmdWindowClosing}" PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
Q2 - 此外,使用 MVVM 框架时这很容易。它们中的大多数实现了消息中介器模式。这对你意味着什么。这意味着您可以发送消息警告 "Request needs update" 并且接收器绑定到该消息,在收到消息时执行某些操作。看看这个来自 Microsoft
的 demo