2010-02-09 55 views
0

有沒有人遇到過一個問題,當你有一個從窗體繼承的類,然後讓所有窗體繼承自類,然後嘗試加載窗體設計模式在Visual Studio?它是否正確加載,因爲在我的它顯示「路徑中的非法字符。」。 我以調試模式運行visual studio,發現它試圖檢查文件的路徑,我猜想並獲得通用定義的'>'。 那麼有可能在窗體上實現泛型,或者我必須爲此找到解決方法,我已經在網上搜索了一些例子或線索,在哪裏可以找到,但可以找到任何重要的線索。任何指針都會很感激,因爲應用程序工作正常,但不是Visual Studio的設計模式。 謝謝。Visual Studio加載設計表格

基類

public class BaseForm<T, TU> : Form, IFormLanguageStrings 
     where T : class 
     where TU : class 
    { 
     #region Variables 

     public IApplicationValues applicationValues; 
     public T businessObject; 
     public IEventBroker eventBroker; 
     private bool hasChangesToCommit = false; 
     public ILanguageManager languageManager; 
     public IMessageBox messageBox; 
     public TU objService; 
     public DatabaseOperationType operationType; 
     public event EventHandler OnNeedToCommitChanges; 

     #endregion 

     #region Constructors 

     /// <summary> 
     /// Initializes a new instance of the <see cref="BaseForm"/> class. 
     /// </summary> 
     public BaseForm() 
     { 
      Closing += BaseForm_Closing; 
      Load += BaseForm_Load; 
      if (IsInDesignMode) return; 
      SetService(); 
     } 

     #endregion 

     #region Form Events 

     /// <summary> 
     /// Handles the Load event of the BaseForm control. 
     /// </summary> 
     /// <param name="sender">The source of the event.</param> 
     /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> 
     private void BaseForm_Load(object sender, EventArgs e) 
     { 
      if (IsInDesignMode) return; 
      SetLabels(); 
      SetControls(); 
      LoadCombos(); 
      LoadDataForForm(); 
      ValidateDataInput(); 

      if (operationType == DatabaseOperationType.View) 
      { 
       eventBroker.Unregister(this); 
       DisableAllControls(); 
       InvisibleSelectControls("BtnAction"); 
      } 
     } 

     /// <summary> 
     /// Handles the Closing event of the BaseForm control. 
     /// </summary> 
     /// <param name="sender">The source of the event.</param> 
     /// <param name="e">The <see cref="System.ComponentModel.CancelEventArgs"/> instance containing the event data.</param> 
     private void BaseForm_Closing(object sender, CancelEventArgs e) 
     { 
      if (hasChangesToCommit) 
      { 
       if (
        messageBox.ShowQuestion(languageManager.GetString("MSG_PerformPendingOperations"), 
              MessageBoxButton.YesNo, MessageBoxResult.No) == MessageBoxResult.Yes) 
       { 
        RaiseCommitChanges(); 
       } 
      } 
      if (eventBroker != null) 
       eventBroker.Unregister(this); 
     } 

     #endregion 

     #region Virtual Methods 

     /// <summary> 
     /// Sets the labels. 
     /// </summary> 
     public virtual void SetLabels() 
     { 
      Controls.All().OfType<Control>().ToList().ForEach(
       ctl => 
        { 
         if (!string.IsNullOrEmpty(ctl.Name)) 
         { 
          string label = languageManager.GetString(ctl.Name); 
          if (!string.IsNullOrEmpty(label)) 
          { 
           ctl.Text = label; 
          } 
          if (ctl is ElementHost) 
          { 
           if (((ElementHost) ctl).Child is IFormLanguageStrings) 
           { 
            (((ElementHost) ctl).Child as IFormLanguageStrings).SetLabels(); 
           } 
          } 
         } 
        }); 
     } 

     /// <summary> 
     /// Sets the service. 
     /// </summary> 
     public virtual void SetService() 
     { 
      applicationValues = ServiceGateway.GetService<IApplicationValues>(); 
      languageManager = ServiceGateway.GetService<ILanguageManager>(); 
      messageBox = ServiceGateway.GetService<IMessageBox>(); 
      eventBroker = ServiceGateway.GetService<IEventBroker>(); 
      eventBroker.Register(this); 
      objService = ServiceGateway.GetService<TU>(); 
     } 

     /// <summary> 
     /// Validates the data input. 
     /// </summary> 
     public virtual void ValidateDataInput() 
     { 
     } 

     /// <summary> 
     /// Determines whether [has to commit changes] [the specified commit]. 
     /// </summary> 
     /// <param name="commit">if set to <c>true</c> [commit].</param> 
     public virtual void HasToCommitChanges(bool commit) 
     { 
      switch (operationType) 
      { 
       case DatabaseOperationType.Update: 
        hasChangesToCommit = commit; 
        break; 
      } 
     } 

     /// <summary> 
     /// Loads the combos. 
     /// </summary> 
     public virtual void LoadCombos() 
     { 
     } 

     /// <summary> 
     /// Loads the data for form. 
     /// </summary> 
     public virtual void LoadDataForForm() 
     { 
     } 

     /// <summary> 
     /// Sets the controls. 
     /// </summary> 
     public virtual void SetControls() 
     { 
     } 

     #endregion 

     #region Private Methods 

     /// <summary> 
     /// Raises the commit changes. 
     /// </summary> 
     private void RaiseCommitChanges() 
     { 
      if (OnNeedToCommitChanges != null) 
      { 
       OnNeedToCommitChanges(this, EventArgs.Empty); 
      } 
     } 

     /// <summary> 
     /// Closes the form. 
     /// </summary> 
     protected void CloseForm() 
     { 
      FireEventBroker(EventTopics.CloseMdiChild, new CloseMdiChildEventArgs(this)); 
     } 

     /// <summary> 
     /// Enables or disable controls. 
     /// </summary> 
     /// <typeparam name="T">type of control</typeparam> 
     /// <param name="isEnable">if set to <c>true</c> [is enable].</param> 
     private void EnableDisableControls<TX>(bool isEnable) where TX : Control 
     { 
      Controls.All() 
       .OfType<TX>() 
       .ToList() 
       .ForEach(ctl => ctl.Enabled = isEnable); 
     } 

     /// <summary> 
     /// Visibles or invisible all controls. 
     /// </summary> 
     /// <param name="isVisible">if set to <c>true</c> [is visible].</param> 
     private void VisibleInvisibleAllControls(bool isVisible) 
     { 
      Controls.All() 
       .OfType<Control>() 
       .ToList() 
       .ForEach(ctl => ctl.Visible = isVisible); 
     } 

     #endregion 

     #region Public Methods 

     /// <summary> 
     /// Gets a value indicating whether this instance is in design mode. 
     /// </summary> 
     /// <value> 
     ///  <c>true</c> if this instance is in design mode; otherwise, <c>false</c>. 
     /// </value> 
     public bool IsInDesignMode 
     { 
      get { return (Process.GetCurrentProcess().ProcessName == "devenv"); } 
     } 

     /// <summary> 
     /// Fires the event broker. 
     /// </summary> 
     /// <param name="eventTopic">The event topic.</param> 
     /// <param name="eventArgs">The <see cref="System.EventArgs"/> instance containing the event data.</param> 
     public void FireEventBroker(string eventTopic, EventArgs eventArgs) 
     { 
      eventBroker.Fire(eventTopic, this, eventArgs); 
     } 

     /// <summary> 
     /// Clears all bindings. 
     /// </summary> 
     public void ClearAllBindings() 
     { 
      Controls.All().OfType<Control>().ToList().ForEach(
       ctl => { if (ctl.DataBindings.Count != 0) ctl.DataBindings.Clear(); }); 
     } 

     /// <summary> 
     /// Enables all controls. 
     /// </summary> 
     public void EnableAllControls() 
     { 
      EnableDisableAllControls(true); 
     } 

     /// <summary> 
     /// Disables all controls. 
     /// </summary> 
     public void DisableAllControls() 
     { 
      EnableDisableAllControls(false); 
     } 

     /// <summary> 
     /// Enables or disable all controls. 
     /// </summary> 
     /// <param name="isEnable">if set to <c>true</c> [is enable].</param> 
     private void EnableDisableAllControls(bool isEnable) 
     { 
      Controls.All() 
       .OfType<Control>() 
       .ToList() 
       .ForEach(ctl => ctl.Enabled = isEnable); 
     } 

     /// <summary> 
     /// Enables all buttons. 
     /// </summary> 
     public void EnableAllButtons() 
     { 
      EnableDisableControls<Button>(true); 
     } 

     /// <summary> 
     /// Disables all buttons. 
     /// </summary> 
     public void DisableAllButtons() 
     { 
      EnableDisableControls<Button>(false); 
     } 

     /// <summary> 
     /// Enables all text boxes. 
     /// </summary> 
     public void EnableAllTextBoxes() 
     { 
      EnableDisableControls<TextBox>(true); 
     } 

     /// <summary> 
     /// Disables all text boxes. 
     /// </summary> 
     public void DisableAllTextBoxes() 
     { 
      EnableDisableControls<TextBox>(false); 
     } 

     /// <summary> 
     /// Enables the select controls. 
     /// </summary> 
     /// <param name="controlNames">The control names.</param> 
     public void EnableSelectControls(params string[] controlNames) 
     { 
      EnableDisableSelectedControls(true, controlNames); 
     } 

     /// <summary> 
     /// Disables the select controls. 
     /// </summary> 
     /// <param name="controlNames">The control names.</param> 
     public void DisableSelectControls(params string[] controlNames) 
     { 
      EnableDisableSelectedControls(false, controlNames); 
     } 

     /// <summary> 
     /// Enables or disable selected controls. 
     /// </summary> 
     /// <param name="isEnable">if set to <c>true</c> [is enable].</param> 
     /// <param name="controlNames">The control names.</param> 
     public void EnableDisableSelectedControls(bool isEnable, params string[] controlNames) 
     { 
      Controls.All() 
       .OfType<Control>() 
       .ToList() 
       .ForEach(ctl => { if (controlNames.Contains(ctl.Name)) ctl.Enabled = isEnable; }); 
     } 

     /// <summary> 
     /// Visibles the select controls. 
     /// </summary> 
     /// <param name="controlNames">The control names.</param> 
     public void VisibleSelectControls(params string[] controlNames) 
     { 
      VisibleInvisibleSelectedControls(true, controlNames); 
     } 

     /// <summary> 
     /// Invisibles the select controls. 
     /// </summary> 
     /// <param name="controlNames">The control names.</param> 
     public void InvisibleSelectControls(params string[] controlNames) 
     { 
      VisibleInvisibleSelectedControls(false, controlNames); 
     } 

     /// <summary> 
     /// Visibles or invisible selected controls. 
     /// </summary> 
     /// <param name="isVisible">if set to <c>true</c> [is visible].</param> 
     /// <param name="controlNames">The control names.</param> 
     private void VisibleInvisibleSelectedControls(bool isVisible, params string[] controlNames) 
     { 
      Controls.All() 
       .OfType<Control>() 
       .ToList() 
       .ForEach(ctl => { if (controlNames.Contains(ctl.Name)) ctl.Visible = isVisible; }); 
     } 

     /// <summary> 
     /// Visibles all controls. 
     /// </summary> 
     public void VisibleAllControls() 
     { 
      VisibleInvisibleAllControls(true); 
     } 

     /// <summary> 
     /// Invisibles all controls. 
     /// </summary> 
     public void InvisibleAllControls() 
     { 
      VisibleInvisibleAllControls(false); 
     } 

     #endregion 
    } 
} 
+0

你能告訴我們的基類? – 2010-02-09 17:18:24

+0

剛更新了帖子 – user267752 2010-02-09 17:34:13

回答

0

好了,所以問題是, 「MyForm的」 設計師不工作:

public class MyForm : BaseForm<MyPresenter, MyView> 
    { 
     //designer doesn't work 
    } 

的解決方法是創建輔助類:

public class MyFormWithDesign : BaseForm<MyPresenter, MyView> 
    { 
     //designer doesn't work 
    } 

所以我們從助手類派生而不是泛型類:

public class MyForm : MyFormWithDesign 
    { 
     //designer WORKS! 
    } 

有關該問題的更多細節可以在這裏找到:
Windows Form Designer can't use a component derived from a generic class

相關問題