2014-10-09 66 views
1

我想完成一個C#/ XAML頁面,並且遇到了麻煩。基本上,當我嘗試編譯時,它抱怨變量名稱(Courier_List)在當前上下文中不存在。我從概念上理解爲什麼還沒有在代碼中的任何地方聲明。但是,我沒有聲明它的原因是因爲這個打包的.dll是爲了與包含該定義的管理包(XML文件)一起工作(Courier_List)定義爲一個列表幷包含指示視覺編譯器放置在何處的座標表格上的列表。在XAML中延遲變量聲明

我猜測解決方案必須是在窗體中聲明列表變量......但我不知道如何(以及它是否會工作)只聲明變量並不在.dll中的任何位置使用它,那麼當所有東西都放在一起時,它會從管理包中調用Courier_List,而不會混淆兩個相同的名稱變量。

我的描述可能會令人困惑,因爲這很難解釋,所以如果有人需要澄清,請讓我知道。我已經包含下面的代碼:

[assembly: CLSCompliant(true)] 
namespace Flexity.RMA 
{ 
    /// <summary> 
    /// Interaction logic for UserControl1.xaml 
    /// </summary> 
    class RMATask : CreateWithLinkHandler 
    { 
     public RMATask() 
     { 
     try 
     { 
      // Sealed Class GUID 
      this.createClassGuid = new Guid("9ebd95da-1b16-b9ea-274d-6b0c16ce1bf3"); 
      this.classToDelegate = new Dictionary<Guid, CreateLinkHelperCallback>() 
      { 
       { ApplicationConstants.WorkItemTypeId, new CreateLinkHelperCallback (this.WorkItemCallback) } 
      }; 
     } 
     catch (Exception exc1) 
     { 
      MessageBox.Show(exc1.Message, "Exception", MessageBoxButton.OK, MessageBoxImage.Error); 
     } 
    } 

    public void WorkItemCallback(IDataItem RMAForm, IDataItem IncidentForm) 
    { 
     try 
     { 
      // Note to self: RelatedWorkItems should be in MP XML as alias under TypeProjections 
      if (RMAForm != null && RMAForm.HasProperty("RelatedWorkItems")) 
      { 
       // Perform Linking 
       RMAForm["RelatedWorkItems"] = IncidentForm; 
       // Copy Incident Title to RMA Title 
       RMAForm["Title"] = IncidentForm["Title"]; 
       // Copy Incident Description to RMA Description 
       RMAForm["Description"] = IncidentForm["Description"]; 
       // Copy Incident ID to RMA Display Name 
       RMAForm["DisplayName"] = "From " + IncidentForm["Id"]; 
      } 
     } 
     catch (Exception exc2) 
     { 
      MessageBox.Show(exc2.Message, "Exception", MessageBoxButton.OK, MessageBoxImage.Error); 
     } 
    } 
} 

public partial class WITemplate: UserControl 
{ 
    private readonly RelatedItemsPane _relatedItemsPane; 

    public WITemplate() 
    { 
     InitializeComponent(); 
     var paneConfig = new WorkItemRelatedItemsConfiguration("RelatedWorkItems", "RelatedWorkItemSource", 
                  "RelatedConfigItems", "RelatedKnowledgeArticles", 
                  "FileAttachments"); 
     _relatedItemsPane = new RelatedItemsPane(paneConfig); 
     tabItemRelItems.Content = _relatedItemsPane; 
    } 

    private void Tracking_Button_Click(object sender, RoutedEventArgs e) 
    { 

     switch (Courier_List.SelectedValue.ToString()) 
     { 
      case "UPS": 

       System.Diagnostics.Process.Start("http://wwwapps.ups.com/ietracking/tracking.cgi?loc=CA_CA^&tracknum^=" + Tracking_Num.Text.ToString()); 
       break; 

      case "FedEX": 
       System.Diagnostics.Process.Start("https://www.fedex.com/fedextrack/index.html?tracknumbers^="+Tracking_Num.Text.ToString()+"^&locale=en_CA^&cntry_code=ca_english"); 
       break; 

      case "UPS SCS": 
       System.Diagnostics.Process.Start("https://www.upspostsaleslogistics.com/cfw/trackOrder.do?trackNumber^=" + Tracking_Num.Text.ToString()); 
       break; 

      default: 
       break; 
     } 
    } 
} 
} 
+0

你可能會嘗試另一個標題爲「編譯後通過xml聲明一個變量」不知道我可以比任何更多的幫助。 – paqogomez 2014-10-09 16:29:51

+0

我不想通過XML聲明它...它已經在XML中聲明瞭...我試圖在C#中編寫邏輯後端(這也讓我困惑,爲什麼有人將我的問題的標題改爲XAML)並且不確定如何聲明它,而不使用變量,直到從xml中調用它 – SirLearnAlot 2014-10-09 17:58:19

+0

它還需要被定義爲C#類。我建議您將XML的一部分添加到問題中。 – 2014-10-09 18:03:28

回答

0

我認爲你是正確的顯而易見的解決方案。您的按鈕點擊處理程序不知道管理包XML的內容,除非您明確地連接它們。

因此,您需要創建一個變量以用於該switch語句的Tracking_Button_Click處理程序。這是一個很常見的模式;如果在C#代碼中需要某處需要某個資源,則必須有一行或兩行代碼查找資源並將其分配給您聲明的變量。

做得不正確,你不會複製數據。相反,系統將設置對現有類的引用。這反過來將允許您在C#代碼中使用管理包XML。

我沒有管理包的示例,但這裏有一些遵循該模式的其他示例。如果你想在XAML資源,你這樣做:

var myList = (XAMLResourceType) this.TryFindResource("myResourceKey");

你並不需要的正是這種,但你需要在你的處理器非常相似的東西,在代碼中。然後你的switch聲明將起作用。