加载和卸载应用程序的内容 [英] Load and unload the content of an application

查看:81
本文介绍了加载和卸载应用程序的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个使用usercontrol的wpf,其中包含一个文本框和一个按钮,现在我想帮助用户恢复他之前在文本框中写的内容,你能告诉我如何做到这一点特别是我正在使用很多usercontrol,有没有一种方法可以保存然后恢复应用程序的状态?

Hi,Im programming a wpf that uses usercontrol tht contain a textbox and a button,and now I wanna help the user restaure what he wrote before in the textbox,Can u give me an idea how to do that especially that Im using many usercontrol,is there a method to save then restaure the state of the app?

推荐答案



没有方法可以我知道那会做你想要的。但是有一些方法可以实现我想你想要的东西。这是一个。


There is no method that I know of that will do what you want. But there are ways in which to accomplish what I think you want. Here's one.



使用ComboBox而不是TextBox。我假设您的按钮表示文本在TextBox中可用。这个概念适用于ComboBox。声明一个列表,其中包含用户先前输入的所有输入。它最初是空的。


Use a ComboBox, instead of a TextBox. I assume that your button signals that text is available in your TextBox. This same concept applies to the ComboBox. Declare a list that will contain all of the input entered earlier by your user. It will initially be empty.

List < string >     earlier_entries = new List < string > ( );



如果要保留先前执行的输入,请将列表保存到/从中恢复用户可访问的文件。


If you want to keep the input from earlier executions, save and restore the list to/from a user-accessible file.



让我们命名ComboBox user_entry_CB和Button user_entry_BUT。


Let's name the ComboBox user_entry_CB and the Button user_entry_BUT.



以下代码是user_entry_BUT点击事件的事件处理程序。


The following code is the event handler for the user_entry_BUT click event.

// ************************************** user_entry_BUT_Click

private void user_entry_BUT_Click ( object    sender,
                                    EventArgs e )
    {

    if ( String.IsNullOrEmpty ( user_entry_CB.Text ) )
        {
        MessageBox.Show ( "You must supply an entry",
                          "Missing Entry" );
        }
    else
        {
        string  text = user_entry_CB.Text;

        if ( !earlier_entries.Contains ( text ) )
            {
            earlier_entries.Add ( text );
            }

        user_entry_CB.Text = String.Empty;
        user_entry_CB.DataSource = null;
        user_entry_CB.DataSource = earlier_entries;
        user_entry_CB.Focus ( );

        // text contains the current user's entry
        }
    }



唯一的技巧是维护列表中的早期用户条目并设置ComboBox的DataSource到列表中。在重新分配列表之前,请记住将ComboBox的DataSource设置为null。


The only trick is to maintains the earlier user entries in the list and setting the DataSource of the ComboBox to the list. Remember to set the ComboBox's DataSource to null before reassigning the list to it.



希望有所帮助。


Hope that helps.


您可以尝试使用应用程序/用户设置。我在Winforms应用程序中做了很多次,但我认为在WPF应用程序中使用它也很容易。试着看看这里:

1. http://blogs.msdn.com/b/patrickdanino/archive/2008/07/23/user-settings-in-wpf.aspx [ ^ ]

2. http:// geekswithblogs.net/mbcrump/archive/2010/06/17/configuring-applicationuser-settings-in-wpf-the-easy-way.aspx [ ^ ]
You can try to use Application/User Settings. I've done that many time in Winforms apps, but i think that using it in WPF app is very easy too. Try to look here:
1. http://blogs.msdn.com/b/patrickdanino/archive/2008/07/23/user-settings-in-wpf.aspx[^]
2. http://geekswithblogs.net/mbcrump/archive/2010/06/17/configuring-applicationuser-settings-in-wpf-the-easy-way.aspx[^]


这篇关于加载和卸载应用程序的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆