I got an assignment to make a Desktop application using C#. It has to be done using MVC design pattern, but I can't find any tutorial which can show how to do it with Desktop based application. All the tutorials which I can find show how to do it for web (asp.net).
我接到一个任务,要用c#创建一个桌面应用程序。它必须使用MVC设计模式,但是我找不到任何可以演示如何使用基于桌面的应用程序的教程。我能找到的所有教程都展示了如何为web (asp.net)做这个。
So I was wondering if someone can suggest me a book or online tutorial which is doing this?
所以我想知道是否有人能给我推荐一本书或在线教程来做这个?
3 个解决方案
#1
4
I always learned by doing so here's a very basic example. Web or Windows, doesn't matter...
我一直在学习,这是一个很基本的例子。Web或Windows,都不重要……
Model
模型
// basic template for what your view will do
public interface IProgram
{
public string FirstName { get; set; }
}
View
视图
public class Program : IProgram
{
ProgramController _controller;
public Program()
{
// pass itself to the controller
_controller = new ProgramController(this);
}
public string FirstName
{
get { return firstNameTextBox.Value; }
set { firstNameTextBox.Value = value; }
}
}
Controller
控制器
public class ProgramController
{
IProgramView _view;
public ProgramController(IProgramView view)
{
// now the controller has access to _view.FirstName
// to push data to and get data from
_view = view;
}
}
You can bind any members this way, such as events
您可以以这种方式绑定任何成员,例如事件
#2
3
Since this is homework, I believe that you teacher and, more importantly, yourself, desire to learn the tricks behind MVC. So, while checking MVC Frameworks might help, I recommend you implement basic functionality on your own.
因为这是家庭作业,我相信你们老师,更重要的是你们自己,都希望学习MVC背后的技巧。因此,尽管检查MVC框架可能会有所帮助,但我建议您自己实现基本功能。
That said, take a look at Wikipedia's article first (which, unfortunately isn't that good), then check Microsoft's take on it.
也就是说,先看看*的文章(可惜不是很好),然后看看微软的看法。
Once you grasp the concepts, try implement a basic triplet that does "something", nothing really fancy. If you have doubts, come back to SO so that we can solve then. And don't forget the new chat functionality of SO.
一旦你掌握了这些概念,试着实现一个基本的三元组,它可以做一些“事情”,没有什么特别的东西。如果你有疑问,回来找我们,这样我们就能解决了。不要忘记SO的新聊天功能。
#3
1
There are a few different avenues you might look at.
你可能会看到一些不同的途径。
- You might consider implementing the pattern yourself. In order to implement the pattern from scratch, you really would need to understand what MVC is trying to solve.
- Consider reading about Design Patterns. I know that Head First Design Patterns includes an introduction to the MVC pattern.
- 考虑阅读设计模式。我知道Head First设计模式包括对MVC模式的介绍。
- You may consider exploring ASP.NET MVC. Even though it is for the web and not the desktop, understanding what problems ASP.NET MVC is solving by using the MVC pattern will help you to understand how to best implement a solution for the desktop.
- 您可以考虑探索ASP。净MVC。即使它是为了web而不是桌面,理解什么问题ASP。NET MVC是通过使用MVC模式来解决的,它将帮助您理解如何最好地实现桌面解决方案。
- 您可以考虑自己实现这个模式。为了实现从头开始的模式,您需要理解MVC试图解决的问题。考虑阅读设计模式。我知道Head First设计模式包括对MVC模式的介绍。您可以考虑探索ASP。净MVC。即使它是为了web而不是桌面,理解什么问题ASP。NET MVC是通过使用MVC模式来解决的,它将帮助您理解如何最好地实现桌面解决方案。
- See if anyone has implemented MVC with WinForms
- For example, see the question Looking for a MVC Sample for WinForms
- 例如,可以看到关于WinForms的MVC示例的问题。
- 例如,看看是否有人已经用WinForms实现了MVC,请参见为WinForms寻找MVC示例的问题
- You also might consider looking for suggestions on implementing the MVC Pattern with WPF or Silverlight.
- Although the common buzzword pattern surrounding WPF and Silverlight is MVVM (Model-View-ViewModel), some people still prefer to use an MVC implementation with these technologies.
- 虽然围绕WPF和Silverlight的常见流行词模式是MVVM(模型-视图-视图-视图模型),但有些人仍然喜欢使用MVC实现。
- In fact, if you consciously create View Models to accompany your views even when using the MVC pattern, you've essentially an "M-V-VM-C" pattern that ensures that your views only know about data from the model that pertains to that particular view.
- 事实上,如果您在使用MVC模式时,有意识地创建视图模型来支持您的视图,那么您实际上就是一个“M-V-VM-C”模式,它确保您的视图仅从与该特定视图相关的模型中了解数据。
- 您还可以考虑寻找关于使用WPF或Silverlight实现MVC模式的建议。虽然围绕WPF和Silverlight的常见流行词模式是MVVM(模型-视图-视图-视图模型),但有些人仍然喜欢使用MVC实现。事实上,如果您在使用MVC模式时,有意识地创建视图模型来支持您的视图,那么您实际上就是一个“M-V-VM-C”模式,它确保您的视图仅从与该特定视图相关的模型中了解数据。
#1
4
I always learned by doing so here's a very basic example. Web or Windows, doesn't matter...
我一直在学习,这是一个很基本的例子。Web或Windows,都不重要……
Model
模型
// basic template for what your view will do
public interface IProgram
{
public string FirstName { get; set; }
}
View
视图
public class Program : IProgram
{
ProgramController _controller;
public Program()
{
// pass itself to the controller
_controller = new ProgramController(this);
}
public string FirstName
{
get { return firstNameTextBox.Value; }
set { firstNameTextBox.Value = value; }
}
}
Controller
控制器
public class ProgramController
{
IProgramView _view;
public ProgramController(IProgramView view)
{
// now the controller has access to _view.FirstName
// to push data to and get data from
_view = view;
}
}
You can bind any members this way, such as events
您可以以这种方式绑定任何成员,例如事件
#2
3
Since this is homework, I believe that you teacher and, more importantly, yourself, desire to learn the tricks behind MVC. So, while checking MVC Frameworks might help, I recommend you implement basic functionality on your own.
因为这是家庭作业,我相信你们老师,更重要的是你们自己,都希望学习MVC背后的技巧。因此,尽管检查MVC框架可能会有所帮助,但我建议您自己实现基本功能。
That said, take a look at Wikipedia's article first (which, unfortunately isn't that good), then check Microsoft's take on it.
也就是说,先看看*的文章(可惜不是很好),然后看看微软的看法。
Once you grasp the concepts, try implement a basic triplet that does "something", nothing really fancy. If you have doubts, come back to SO so that we can solve then. And don't forget the new chat functionality of SO.
一旦你掌握了这些概念,试着实现一个基本的三元组,它可以做一些“事情”,没有什么特别的东西。如果你有疑问,回来找我们,这样我们就能解决了。不要忘记SO的新聊天功能。
#3
1
There are a few different avenues you might look at.
你可能会看到一些不同的途径。
- You might consider implementing the pattern yourself. In order to implement the pattern from scratch, you really would need to understand what MVC is trying to solve.
- Consider reading about Design Patterns. I know that Head First Design Patterns includes an introduction to the MVC pattern.
- 考虑阅读设计模式。我知道Head First设计模式包括对MVC模式的介绍。
- You may consider exploring ASP.NET MVC. Even though it is for the web and not the desktop, understanding what problems ASP.NET MVC is solving by using the MVC pattern will help you to understand how to best implement a solution for the desktop.
- 您可以考虑探索ASP。净MVC。即使它是为了web而不是桌面,理解什么问题ASP。NET MVC是通过使用MVC模式来解决的,它将帮助您理解如何最好地实现桌面解决方案。
- 您可以考虑自己实现这个模式。为了实现从头开始的模式,您需要理解MVC试图解决的问题。考虑阅读设计模式。我知道Head First设计模式包括对MVC模式的介绍。您可以考虑探索ASP。净MVC。即使它是为了web而不是桌面,理解什么问题ASP。NET MVC是通过使用MVC模式来解决的,它将帮助您理解如何最好地实现桌面解决方案。
- See if anyone has implemented MVC with WinForms
- For example, see the question Looking for a MVC Sample for WinForms
- 例如,可以看到关于WinForms的MVC示例的问题。
- 例如,看看是否有人已经用WinForms实现了MVC,请参见为WinForms寻找MVC示例的问题
- You also might consider looking for suggestions on implementing the MVC Pattern with WPF or Silverlight.
- Although the common buzzword pattern surrounding WPF and Silverlight is MVVM (Model-View-ViewModel), some people still prefer to use an MVC implementation with these technologies.
- 虽然围绕WPF和Silverlight的常见流行词模式是MVVM(模型-视图-视图-视图模型),但有些人仍然喜欢使用MVC实现。
- In fact, if you consciously create View Models to accompany your views even when using the MVC pattern, you've essentially an "M-V-VM-C" pattern that ensures that your views only know about data from the model that pertains to that particular view.
- 事实上,如果您在使用MVC模式时,有意识地创建视图模型来支持您的视图,那么您实际上就是一个“M-V-VM-C”模式,它确保您的视图仅从与该特定视图相关的模型中了解数据。
- 您还可以考虑寻找关于使用WPF或Silverlight实现MVC模式的建议。虽然围绕WPF和Silverlight的常见流行词模式是MVVM(模型-视图-视图-视图模型),但有些人仍然喜欢使用MVC实现。事实上,如果您在使用MVC模式时,有意识地创建视图模型来支持您的视图,那么您实际上就是一个“M-V-VM-C”模式,它确保您的视图仅从与该特定视图相关的模型中了解数据。