简体   繁体   English

C#MVVM,了解何时不制作模型

[英]C# MVVM, Understanding when not to make a model

I Everyone. 我大家。

I am a big fan of MVVM But need to clarify how I should Implement a general method that could be used across several view models. 我是MVVM的忠实拥护者,但需要澄清如何实现可在多个视图模型中使用的通用方法。

Currently I have implemented a method in to a model to take a screen shot like so EG 目前,我已经在模型中实现了一种方法,例如EG

Interface 接口

public interface ICapture {
 void CaptureMethod();
}

This is the model that implements that interface 这是实现该接口的模型

public class CaptureModel : ICapture {
    void CaptureMethod(){ //implement the code to take screen shot }
}

Now I want to execute a screen shot on one or multiple view models, so I would need to instantiate the and call the function like so. 现在,我想在一个或多个视图模型上执行屏幕截图,因此我需要实例化并像这样调用函数。

public ViewModel(){
 void TakeScreenShotMethodOrCommandDontCare(){
   ICapture captureClass = new CaptureClass();
   captureClass.captureMethod
  }
}

Which just feels WRONG having to instantiate an instance of an object in order to call a function which will be taking a screenshot. 只是觉得错误必须实例化一个对象的实例才能调用将要截屏的函数。

I suppose i could go in the view model and have capture screenshot method but that would lead to code duplication across all view models with that feature. 我想我可以进入视图模型并使用捕获屏幕快照方法,但这将导致具有该功能的所有视图模型之间的代码重复。

Maybe a static utility class but surely that would be expensive on the app. 也许是静态实用程序类,但肯定会在应用程序上昂贵。

I feel if I had an interface that was implemented by a base class then my view model could inherit from that base class and implement the base implementation on any view model I desire. 我觉得如果我有一个由基类实现的接口,则我的视图模型可以从该基类继承,并在所需的任何视图模型上实现基实现。

So to sum up my question, if you wanted to implement a method that captures screen shots and you like mvvm then where would you implement ?? 因此,总结一下我的问题,如果您想实现一种捕获屏幕快照的方法,并且您喜欢mvvm,那么您将在哪里实现?

Again many thanks to all that have taken the time to shine some light on this for me. 再次感谢所有花时间为我提供帮助的人。

This is actually what I would consider well within the MVVM standard. 实际上,这是我在MVVM标准中考虑的范围。 When dealing with MVVM, I find it best to think about it this way. 在处理MVVM时,我发现最好以这种方式考虑它。 Your View should know only about your ViewModel and the ViewModel should know nothing of the View. 您的视图应该只知道您的ViewModel,而视图模型应该不了解该视图。 The ViewModel should know about your model (or models) but the model should know nothing of the ViewModel or View. ViewModel应该了解您的模型(或多个模型),但是模型应该不了解ViewModel或View。

So, here your Model is being instantiated in the ViewModel which is perfectly valid! 因此,这里的Model是在ViewModel中实例化的,这是完全有效的! In this case, your Model is the CaptureClass and the ViewModel knows that it can take a string from that and provide it to some View that may want to use it. 在这种情况下,您的Model是CaptureClass,并且ViewModel知道它可以从中获取字符串,并将其提供给某些可能要使用它的View。

Model: 模型:

class CaptureClass : ICapture
{       
  public string CaptureShot()
  {
     string s = DependencyService.Get<ICapture>().CaptureShot();
     return s;
  }      
}

ViewModel: 视图模型:

public class CaptureViewModel(){

   private _captureText;

   public CaptureText
   {
       get{ return _captureText; }
       set{
           _captureText = value;
       }
   }

   public CaptureViewModel()
   {
       CaptureClass cc = new CaptureClass();
       _captureText = cc.CaptureShot();
   }
}

Now all you've got to do is hook it up to your View. 现在,您要做的就是将其连接到您的视图。 Remember that the View knows which property to bind to so just set your DataContext either in the code behind or in XAML and then just bind the property in XAML like so: 请记住,View知道要绑定到哪个属性,因此只需在XAML后面的代码或XAML中设置您的DataContext,然后像下面这样在XAML中绑定该属性:

<Label Content="{Binding CaptureText}" />

The answer has been found and involved injecting a service class in to the view model I wish to use the methods in. 找到了答案,并涉及将服务类注入到我希望在其中使用方法的视图模型中。

Link to answer. 链接回答。

https://softwareengineering.stackexchange.com/questions/380036/c-mvvm-understanding-when-not-to-make-a-model https://softwareengineering.stackexchange.com/questions/380036/c-mvvm-understanding-when-not-to-make-a-model

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

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