【问题标题】:How to load ViewModel/View in WPF under CaliburnMicro如何在 CaliburnMicro 下的 WPF 中加载 ViewModel/View
【发布时间】:2015-05-19 23:15:45
【问题描述】:

我的 wpf 应用程序中有一个计时器,每 5 分钟询问一次 WCF 服务。如果该服务有我的应用程序的消息,我会得到一个包含文本数据和特定代码的列表。

此代码提供了有关打印数据必须加载的视图的信息。

我有两个 ViewModel(两者的数据源相同):一个用于 Ticker > 一个视图和 一个用于 Popup > 两个视图

项目文件:

  1. 查看
    • 弹出窗口
      • PopHighView.xaml
      • PopMediumView.xaml
    • 代码
      • TickerLayout.xaml
      • TickerNormal.xaml
  2. 视图模型
    • PopViewModel
    • TickerViewModel
  3. 型号
    • AlertModel.cs
  4. 视图解析器
    • 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


【解决方案1】:

程序,任何程序,包括包含您要显示的视图的程序,都可以通过多种方式显示视图。这里有一些:

    var app = new App();        
    app.InitializeComponent();
    app.Run();  

或者你可以直接启动视图:

   var view = new MyView();
   view.Show(); 
   // or 
   view.ShowDialog();

如果视图是 MainWindow,那么您可以在视图中创建一个 ContentControl 区域来注入包含您想要显示的子视图的用户控件。这仍然需要打开 MainWindow... 所以上面的示例在将 UserControls 注入 MainWindow 时也可以工作。注入用户控件的行为是将 ContentControl 的内容设置为用户控件本身的一个实例。事件处理程序可以很好地处理这种情况...

public void NewUserControlInMainWindow(object sender, UserControl uc){
      //XCC = the Xaml content control in main window
      XCC.Content = uc;
}

我不太确定 Caliburn 是如何看待注射的......

【讨论】:

  • Caliburn 似乎有点复杂,我现在无法理解。 Mvvm light 和 Wpf notify Icon 似乎更容易在我的项目中一起使用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多