C# - 将 UI 与业务代码分开
我想知道以下情况的最佳实践是什么:
我正在开发一个需要在三种不同屏幕尺寸上使用的应用程序:240x320; 800x600; 1280x1024(仅限这三种),不同语言(使用本地化)。
因此,我所做的就是按屏幕大小创建一个库,每个库都实现在调用这些屏幕的项目中定义的接口。
第一个问题是,我的所有资源文件都是重复的,并且有很多文件需要保持最新(或具有重复的标签)。有没有一种简单的方法可以改变这种情况?
第二个问题是,我是否使用了好的方法,或者是否有更好的方法来完成我想做的事情?也许通过应用设计模式?
示例代码
调用者代码:
public int X { get { return 800; } }
public int Y { get { return 600; } }
public interface IScreenForm
{
Form FormComponent { get; }
}
public interface IFormA : IScreenForm
{
TextBox UserTextBox { get; } // For use
}
public void LoadForm<T>()
where T:IScreenForm
{
if (!typeof(T).IsInterface)
{
throw new ArgumentException(typeof(T).Name + " is not an interface");
}
Assembly screenAssembly;
string screenResolutionDll = string.Format("{0}_{1}_screens.dll", this.X, this.Y);
screenAssembly = Assembly.LoadFrom(screenResolutionDll);
Type formType = screenAssembly.GetTypes()
.FirstOrDefault(t => t.GetInterfaces().Where(i => i.Name == typeof(T).Name).Count() > 0);
if (formType != null)
{
Form form = (Form)formType.GetConstructor(new Type[] { }).Invoke(null);
if (form != null)
{
form.Show();
}
else
{
throw new ArgumentException("Form doesn't provide a new() ctor");
}
}
else
{
throw new ArgumentException("Any form doesn't implement the interface : " + typeof(T).Name);
}
}
屏幕 DLL:
public partial class MyFirstForm : Form, caller.IFormA
{
public Form1()
{
InitializeComponent();
}
/* caller.IFormA interface implementation */
}
I want to know what's the best practice for the following case :
I'm developing an application that need to be used on three different screen size : 240x320 ; 800x600 ; 1280x1024 (and only these three) in different languages (using localization).
So what I've done is to create a library by screen size, each implementing an interface defined in the project that calls these screens.
First question is, all my resources files are duplicated and that is many files to keep up to date (or with duplicate labels). Is there a simple way to change this?
Second question is, am I using the good approach or exists it a better way to do what I'm trying to do? By applying a design pattern maybe?
Sample code
The caller code :
public int X { get { return 800; } }
public int Y { get { return 600; } }
public interface IScreenForm
{
Form FormComponent { get; }
}
public interface IFormA : IScreenForm
{
TextBox UserTextBox { get; } // For use
}
public void LoadForm<T>()
where T:IScreenForm
{
if (!typeof(T).IsInterface)
{
throw new ArgumentException(typeof(T).Name + " is not an interface");
}
Assembly screenAssembly;
string screenResolutionDll = string.Format("{0}_{1}_screens.dll", this.X, this.Y);
screenAssembly = Assembly.LoadFrom(screenResolutionDll);
Type formType = screenAssembly.GetTypes()
.FirstOrDefault(t => t.GetInterfaces().Where(i => i.Name == typeof(T).Name).Count() > 0);
if (formType != null)
{
Form form = (Form)formType.GetConstructor(new Type[] { }).Invoke(null);
if (form != null)
{
form.Show();
}
else
{
throw new ArgumentException("Form doesn't provide a new() ctor");
}
}
else
{
throw new ArgumentException("Any form doesn't implement the interface : " + typeof(T).Name);
}
}
Screen DLL :
public partial class MyFirstForm : Form, caller.IFormA
{
public Form1()
{
InitializeComponent();
}
/* caller.IFormA interface implementation */
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Arnaud,根据我自己的经验、阅读以及与经验丰富的开发人员的交谈:在支持多种屏幕尺寸和本地化方面,没有灵丹妙药。为了本地化,将所有字符串放入资源文件,甚至数据文件中。
至于屏幕尺寸,我不会太聪明。是的,请确保您的业务/非 GUI 逻辑没有被重复,但重复的资源文件、表单等 - 我不会担心它。一些 GUI 框架(例如 Qt 和 GTK)会自动调整 GUI 小部件的大小和位置(例如,Qt 有“间隔”小部件)。大多数时候它工作正常,但我仍然更喜欢显式控制。在 GUI 编程中,经常会出现意想不到的故障,如果出现这些问题,拥有三组独立的 GUI 组件将允许您解决这些问题。问题来源的一些示例:
1)字体大小。
2) Windows 辅助功能设置。
3) 一些国家语言的平均单词数比其他语言长,长单词很难适应可用的空间。
如果我处于你的立场,我会看看浏览器如何处理这个问题(例如移动版本与桌面版本),并且我会尝试在网络上找到一些实用的建议(就像这里的SO)。我怀疑有关设计模式的书籍对此会有多大帮助。免责声明:我是设计模式怀疑论者。
Arnaud, from my own experience, from reading, and from talking to experienced developers: When it comes to supporting multiple screen sizes and with localization, there are no magic bullets. For localization put all your strings in resource files, or even in data files.
As far as the screen sizes, I would not try to be too clever. Yes, make sure that none of your business / non-GUI logic gets duplicated, but duplicating resource files, forms, etc - I would not worry about it. Some GUI frameworks like Qt and GTK auto-resize and auto-position GUI-widgets (e.g. Qt has 'spacer' widgets). It works OK most of the time, however I still prefer explicit control. With GUI programming, there are often unexpected glitches, having three independent sets of GUI components will allow you to address them, should they arise. Some example sources of problems:
1) Font sizing.
2) Windows accessibility settings.
3) Some national languages have longer average words than others, the long words have trouble fitting in the real estate available.
If I were in your shoes, I would look for example how browsers handle this (e.g. mobile vs. desktop version) and I would try to find some practical advice on the Web (like here at SO). I doubt that books on design patterns will help much with this. Disclaimer: I am a design-pattern skeptic.
在不了解全貌的情况下很难提出建议,但对于一些可能会方便的提示,请阅读此
http://msdn.microsoft.com/en-us/library/ff648753.aspx
有关 SmartClient 软件工厂的信息。
它附带了针对此类应用程序中常见问题的架构指南和解决方案
It is hard to advice without getting the whole picture but for some tips that might by handy read this
http://msdn.microsoft.com/en-us/library/ff648753.aspx
about the SmartClient software Factory.
It comes with architecural guidance and solutions for issue you often see in this kind of apps