简体   繁体   English

WPF调用方法与属性

[英]WPF call method with property

I'm not very proficient with WPF. 我对WPF不太熟练。 What I want to do is bind to a property from my view and then set that value with something. 我想要做的是从我的视图绑定到属性,然后用某些东西设置该值。 I've got that part working without trouble, but I have a feeling I'm not properly using the MVVM pattern here. 我已经没有遇到麻烦,但我觉得我没有正确使用MVVM模式。 I have my property in the ViewModel, bound to the View however I can't seem to get the Model part working as I intend as the method where the property gets its value from is currently also in the ViewModel. 我在ViewModel中拥有我的属性,绑定到View但是我似乎无法使Model部分工作,因为我打算将属性从其获取其值的方法当前也在ViewModel中。

Here's what I currently have: 这是我现在拥有的:

public class MainViewModel : ViewModelBase
{
    private Awesome _model; //this is my model
    private string _score;

    public string Score
    {
        get { return GetScore(); }
        set
        {
            _score = value;
        }
    }

    public string GetScore()
    {
        try
        {
            using (StreamReader sr = new StreamReader(@"C:\somepath"))
            {
                String line = sr.ReadToEnd();
                return line;
            }
        }
        catch (Exception)
        {
            MessageBox.Show("File could not be found! :(");
            throw;
        }
    }
}

This works fine, but everything is in the ViewModel right now. 这很好,但现在一切都在ViewModel中。 As far as I understand, GetScore() should be in the Model, but then I'm not sure how to set the property with it. 据我所知,GetScore()应该在Model中,但是我不知道如何用它来设置属性。 What am I missing here? 我在这里错过了什么?

The GetScore() -Method should not be in the Model. GetScore() - GetScore()不应该在模型中。 The model is the data-layer so there are only data-objects with there properties. 模型是数据层,因此只有具有属性的数据对象。 Methods and other stuff are coordinated by the ViewModel. 方法和其他东西由ViewModel协调。 So you can let your GetScore-Method in your ViewModel or move it to another class and call it from your ViewModel. 因此,您可以在ViewModel中放置GetScore-Method,或将其移至另一个类并从ViewModel中调用它。

By the way: Your property is a little bit weird. 顺便说一句:你的财产有点奇怪。 becaus in your setter you're setting a backend-field which will never be used again. 因为你的二传手你正在设置一个永远不会再使用的后端区域。 Are you sure that this is what you want? 你确定这是你想要的吗? You should also not always read a file in the getter. 您也不应该总是在getter中读取文件。

Maybe you want to do something like: 也许你想做的事情如下:

public string Score
{
    get { return _score ?? (_score = GetScore()); }
}

So you only read the file once and keep the value saved in _score. 因此,您只需读取一次文件并将值保存在_score中。

Your GetScore() belongs to the ViewModel, as it is the data layer. 您的GetScore()属于ViewModel,因为它是数据层。 (Of course you could move it to another class, but it's not wrog from my POV in the ViewModel, see it as an extended getter ;)) (当然你可以将它移动到另一个类,但它不是来自我在ViewModel中的POV的wrog,将它视为扩展的getter;))

But

  1. You should make GetScore private, because you have a property for it 您应该将GetScore设为私有,因为您有一个属性
  2. You should not influence the UI from the ViewModel, so I would advise you to not open the MessageBox from the ViewModel. 您不应该从ViewModel影响UI,因此我建议您不要从ViewModel打开MessageBox。
  3. Use return this.GetScore() 使用return this.GetScore()

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

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