本文介绍了刷新WPF主窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些我想添加到正在开发的应用程序中的概念方面的帮助.  这是一个现有的应用,正在启动的应用包含很多部分.我希望能够刷新应用程序数据,但是基本上可以关闭主程序 窗口,然后重新打开以重新初始化它.  

I need some help with a concept that I would like to add to an app that I am working on.  It is an existing app, who's start up has a lot of parts.  I would like to have the ability to refresh the application data, but basically close the main window and reopen it to reinitialize it.  

有人可以提供一些见解,以了解在不打开应用程序而又没有打开主窗口的情况下如何将其关闭吗?

Could someone provide some insight on how this could be down without the application closing before the main window opens back up?

谢谢

推荐答案

您可能已经意识到,关闭主窗口将关闭您的应用程序.

As you probably already realise, closing the mainwindow is going to close your app down.

更常见的方法是再次从数据库中读取数据,并填充窗口作为数据上下文的集合/对象.

A much more usual approach is to go read the data out a database again and fill the collection/object the window has as datacontext.

就像这样:

http://social.technet.microsoft.com/wiki/contents/articles/28209.wpf-entity-framework-mvvm-walk-through-1.aspx

http://social.technet.microsoft.com/wiki/contents/articles/28209.wpf-entity-framework-mvvm-walk-through-1.aspx

主窗口实际上只包含一个菜单.用户控件作为内容控件上的内容交换,该控件填充了窗口的所有其余部分.

The mainwindow just really holds a menu. A Usercontrol is swapped in as content on a contentcontrol which fills all the rest of the window.

我想您可以完全更新该用户控件.

You could new up that usercontrol entirely I suppose.

在示例中,当前的用户控件响应按下的刷新按钮,并重新读取绑定到填充该用户控件的数据网格的itemsource上的集合:

In the sample, the current usercontrol responds to the refresh button being pressed and re-reads the collection which is bound to the itemssource of the datagrid which fills that usercontrol:

        protected async override void GetData()
        {
            db.Configuration.LazyLoadingEnabled= true;
            ThrobberVisible = Visibility.Visible;
            ObservableCollection<CustomerVM> _customers = new ObservableCollection<CustomerVM>();
            var customers = await (from c in db.Customers
                                    orderby c.CustomerName
                                    select c).ToListAsync();
            foreach (Customer cust in customers)
            {
                _customers.Add(new CustomerVM { IsNew = false, TheEntity = cust });
            }
            Customers = _customers;
            RaisePropertyChanged("Customers");
            ThrobberVisible = Visibility.Collapsed;
        }

在更新视图模型时,由构造函数以及单击刷新按钮时,将调用Getdata.

Getdata is called when the viewmodel is newed up, by the constructor and when the refresh button is clicked.


这篇关于刷新WPF主窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 12:20