简体   繁体   English

如何将错误消息从C#属性设置器“返回”给用户?

[英]How to “return” an error message from a C# property setter to the user?

I have a windows form that allows the user to input an integer value. 我有一个Windows窗体,允许用户输入一个整数值。 This value is used to update a member of a separate class (foo) so long as it falls within a certain valid range. 该值用于更新单独类(foo)的成员,只要它在某个有效范围内即可。 If the user inputs something invalid, I want an error message to tell the user that the default value for the parameter will be used. 如果用户输入的内容无效,我希望收到一条错误消息,告诉用户将使用参数的默认值。

In order to accomplish this, I have been error checking the user input within the code for the windows form before letting foo set the field in question, and simply displaying a message if the input fails the check. 为了做到这一点,在让foo设置有问题的字段之前,我一直在错误检查Windows窗体代码中的用户输入,并在输入未通过检查时简单地显示一条消息。

But what I would like to do is send the raw user input to a property setter within the class foo and have the property setter handle all the error checking. 但是我想做的是将原始用户输入发送到foo类中的属性设置器,并让属性设置器处理所有错误检查。 For other functions in foo that may require a message be displayed on the user interface, I am simply returning a string, but to my knowledge there is no way to return any sort of status or value from a property setter. 对于foo中可能需要在用户界面上显示消息的其他函数,我只是返回一个字符串,但是据我所知,无法从属性设置器返回任何状态或值。 Is there a graceful way to bubble up a status message of some sort from a property setter to a different function? 是否有一种优美的方式将某种状态消息从属性设置器传递到其他函数? I don't want foo to modify the user interface directly. 我不希望foo直接修改用户界面。

Thanks in advance for any help or tips, and I apologize if my question is too vague or should be asked differently as I am new to StackOverflow 在此先感谢您的帮助或提示,如果我的问题太含糊或不正确,我深表歉意,因为我是StackOverflow的新手

I fixed my problem (as well as removed the need to return strings from nearly all my functions) by implementing INotifyPropertyChanged in my class foo and raising a PropertyChanged event to be caught by the windows form whenever it was necessary to let the user know something had changed. 我通过在类foo中实现INotifyPropertyChanged并提出一个PropertyChanged事件以在需要让用户知道某些事情时被Windows窗体捕获来解决我的问题(并消除了从几乎所有函数中返回字符串的需要)改变。

in class foo- 在foo类中

public class foo : INotifyPropertyChanged {

    public event PropertyChangedEventHandler PropertyChanged;
    public int bar;

    private void NotifyPropertyChanged(string type) {
        if (PropertyChanged != null) {
            PropertyChanged(this, new PropertyChangedEventArgs(type));
        }
    }

    private void test(){
        bar = 1;
        NotifyPropertyChanged("changed int");
    }

}

in the windows form: 在Windows窗体中:

public partial class GUI : Form {
     foo fooinstance = new foo();

     public GUI(){
           InitializeComponent();
           fooinstance.PropertyChanged += doEvent;
     }

     private void doEvent(object sender, PropertyChangedEventArgs e){
        foo updated = sender as foo;
        if (object.ReferenceEquals(e.PropertyName, "changed int")) {
            ShowWhatChanged(updated.bar); //show on GUI
        }
     }

} }

EDIT: A more graceful and cleaner way that I ended up using was to utilize callbacks. 编辑:我最终使用的一种更优美,更干净的方法是利用回调。 The GUI passed a function pointer into foo upon instantiation and foo used it as a delegate to pass strings back to the GUI whenever necessary. GUI在实例化时将函数指针传递给foo,而foo将其用作委托以在必要时将字符串传递回GUI。 No events required: 无需事件:

public class foo {

    public delegate void UpdateCallback(string msg);
    private UpdateCallback _ucb;
    public foo(UpdateCallback cb){
        _ucb = cb;
    }

    private void test(){
        if(_ucb != null) {
           _ucb("Message Here");
        }
    }
}

public partial class GUI : Form {

     public GUI(){
           InitializeComponent();
           foo fooinstance = new foo(showmessage);

     }

     private void showmessage(string msg){
           //do whatever with the message
     }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM