English 中文(简体)
处理财产 改换成型安全
原标题:Handling PropertyChanged in a type-safe way

关于如何利用反省和林研中心以“安全”的方式,在不使用“护卫”的情况下,以“安全”的方式来提出财产被篡改的事件。

consume没有任何办法。 财产 以安全方式改变事件? 目前,我这样做了。

void model_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
    switch (e.PropertyName)
    {
        case "Property1":
            ...
        case "Property2":
            ...

        ....               
    }
}

在开关说明中是否有办法避免硬编码处理不同财产? 某些类似的准则或基于思考的方法?

最佳回答
问题回答

我最近提出的一种解决办法是将事件发送逻辑推向一个专门类别。

该类别有一种称为<代码>Handle的公开方法,其签名与相同。 PropertyChangedEventHandler 代表意思是可加入。 PropertyChanged 执行<代码>的任何类别的活动 INotificationPropertyChanged界面。

班级接受代表,如经常使用的<代码>DelegateCommand,这些代表被多数WPF执行者使用,意思是,无需设立子级即可使用。

这一类人认为:

public class PropertyChangedHandler
{
    private readonly Action<string> handler;
    private readonly Predicate<string> condition;
    private readonly IEnumerable<string> properties;

    public PropertyChangedHandler(Action<string> handler, 
        Predicate<string> condition, IEnumerable<string> properties)
    {
        this.handler = handler;
        this.condition = condition;
        this.properties = properties;
    }

    public void Handle(object sender, PropertyChangedEventArgs e)
    {
        string property = e.PropertyName ?? string.Empty;

        if (this.Observes(property) && this.ShouldHandle(property))
        {
            handler(property);
        }
    }

    private bool ShouldHandle(string property)
    {
        return condition == null ? true : condition(property);
    }

    private bool Observes(string property)
    {
        return string.IsNullOrEmpty(property) ? true :
            !properties.Any() ? true : properties.Contains(property);
    }
}

之后,你可以登记经改变的事件手:

var eventHandler = new PropertyChangedHandler(
    handler: p => { /* event handler logic... */ },
    condition: p => { /* determine if handler is invoked... */ },
    properties: new string[] { "Foo", "Bar" }
);

aViewModel.PropertyChanged += eventHandler.Handle;

<代码>PropertyChangedHandler 注意核对<代码>PropertyName>,并确保handler在产权变更中被援引。

发出通知,要求财产委员会也接受一个前提,以便有条件派遣手递代表。 这一类别还使你能够具体说明多种特性,使单一手提人能够被捆绑在一起。

可以通过一些推广方法,方便地登记更方便的手稿,使你能够创建活动手并订阅<代码>。 采用单一方法进行的财产标识/编码活动,用表达方式来说明财产,而不是为了达到类似情况:

aViewModel.OnPropertyChanged(
    handler: p => handlerMethod(),
    condition: p => handlerCondition,
    properties: aViewModel.GetProperties(
        p => p.Foo,
        p => p.Bar,
        p => p.Baz
    )
);

这基本上是说,当<代码>时。 Foo,BarBaz , nature change handlerMethod is real.

为涵盖不同的活动登记要求,提供了<代码>上调方法的超载。

举例来说,如果你想要登记一位手记,要求任何改变财产的活动,并且总是执行的话,你可以做以下工作:

aViewModel.OnPropertyChanged(p => handlerMethod());

举例来说,如果你想登记一位总会被处决的手记,但只有一种特定的财产变更,你才能做到:

aViewModel.OnPropertyChanged(
    handler: p => handlerMethod(),
    properties: aViewModel.GetProperties(p => p.Foo)
);

我发现,在撰写WPF MVLT申请时,这种做法非常有用。 如果三个财产发生变化,你就想使指挥无效。 采用正常方法,你必须做这样的事情:

void PropertyChangedHandler(object sender, PropertyChangedEventArgs e)
{
    switch (e.PropertyName)
    {
        case "Foo":
        case "Bar":
        case "Baz":
            FooBarBazCommand.Invalidate();
            break;
        ....               
    }
}

如果你改变任何观点名称,你需要记住更新活动手,选择正确的财产。

采用<代码>PropertyChangedHandler 以上所列类别可得出以下结果:

aViewModel.OnPropertyChanged(
    handler: p => FooBarBazCommand.Invalidate(),
    properties: aViewModel.GetProperties(
        p => p.Foo,
        p => p.Bar,
        p => p.Baz
    )
);

如今已经编纂了当时的安全问题。 如果对Model的任何一种特性都重新命名,则不会编纂。

Koh Smith s MVVM Foundation 包括1个财产。 观察员班,你想要做的事情。

I avoid the switch by combining the command pattern and some Expression logic. You encapsulate the case-action in a command. I ll illustrate this using a Model View Controller structure. real world code - WinForms,but it is the same idea

举例来说,当树木财产在模型中确定时,就装上树木。

http://www.ohchr.org。

void Execute();
string PropertyName  { get;  }

www.un.org/Depts/DGACM/index_spanish.htm 具体指挥部

 public TreeChangedCommand(TreeModel model, ISelectTreeView selectTreeView,Expression<Func<object>> propertyExpression )
    {
        _model = model;
        _selectTreeView = selectTreeView;

        var body = propertyExpression.Body as MemberExpression;
        _propertyName = body.Member.Name;

    }

http://www.ohchr.org。

 //handle notify changed event from model
  _model.PropertyChanged += _model_PropertyChanged;
  //init commands
  commands = new List<ICommand>();
  commands.Add(new TreeChangedCommand(_model,_mainView,()=>_model.Tree));

<>propertyChangedhandler>

void _model_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
    //find the corresponding command and execute it. (instead of the switch)
    commands.FirstOrDefault(cmd=>cmd.PropertyName.Equals(e.PropertyName)).Execute();
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签