【发布时间】:2015-05-19 23:15:45
【问题描述】:
我的 wpf 应用程序中有一个计时器,每 5 分钟询问一次 WCF 服务。如果该服务有我的应用程序的消息,我会得到一个包含文本数据和特定代码的列表。
此代码提供了有关打印数据必须加载的视图的信息。
我有两个 ViewModel(两者的数据源相同):一个用于 Ticker > 一个视图和 一个用于 Popup > 两个视图
项目文件:
- 查看
- 弹出窗口
- PopHighView.xaml
- PopMediumView.xaml
- 代码
- TickerLayout.xaml
- TickerNormal.xaml
- 弹出窗口
- 视图模型
- PopViewModel
- TickerViewModel
- 型号
- AlertModel.cs
- 视图解析器
- AlertParser.cs
数据来源:
public class AlertParser : IAlertParser{
AlertServiceClient service;
public List<AlertModel> TickerAlertData()
{
try
{
service = new AlertServiceClient();
List<AlertModel> items = (from item in service.GetActiveAlert() select new AlertModel
{
Alertid= item.AlertId,
Alertstartdate = item.AlertStartDate,
Alerttitle = item.AlertTitle,
Alerttxt = item.AlertText
}).ToList();
return items;
}
catch
{
return null;
}
}
}
当我的应用程序启动时,没有加载视图,系统托盘中只有一个图标(带有 wpf notifyicon)。
我的问题是,在这些情况下,当我的计时器从我的服务返回 true 时,我不明白如何加载几个 ViewModel/View,并将数据传递给它们。
网络上的许多示例都加载了主视图,这就是我迷路的原因(例如 caliburn 微页面上的指挥示例)。
感谢您的帮助!
编辑:
好的,我的计时器是这样的:
if (service.IsAlertReady()=true)
{
string hostName = Dns.GetHostName();
string myIP = Dns.GetHostEntry(hostName).AddressList[0].ToString();
service.IsAlertForMe(myIP);
if(service.IsAlertForMe(myIP) == true)
{
ShellViewModel shell = new ShellViewModel();
shell.ShowMediumPop();
}
else
...
ShellViewModel
public class ShellViewModel : Conductor<object>
{
public void ShowMediumPop()
{
ActivateItem(new PopViewModel());
}
}
PopViewModel
public class PopViewModel : screen
{
protected override void OnActivate()
{
base.OnActivate();
}
}
PopView.Medium
<UserControl x:Class="TerminalClientProto.View.PopView"
...
cal:View.Model="{binding}"
cal:View.Context="Medium"
>
很抱歉,但我不明白如何在我的 Ticker 打勾时启动我的视图。我已经阅读了文档,但我需要一些提示来理解这种机制。
【问题讨论】:
-
当考虑尝试启动视图的事件时,在 WPF 中至少有两种方法可以做到这一点......我会发布一个答案,看看它是否适合你。
标签: c# wpf mvvm data-binding caliburn.micro