2014-03-05 46 views
0

我目前有一個WPF應用程序需要接受來自URL中的參數,就像它在ASP.NET中一樣。我已經瀏覽過以前的帖子,但沒有看到像泥巴一樣清晰。我已經將「發佈」中的部分更改爲接受參數。以下是我正在使用的代碼:C#將查詢字符串中的參數傳遞給WPF應用程序

using System; 
using System.Deployment; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 
using ViewImageForm; 
using System.Windows.Forms.Integration; 
using System.Windows.Forms; 
using System.Web; 
using System.Collections.Specialized; 
using System.Diagnostics; 
using System.Runtime.InteropServices; 
using System.Deployment.Application; 

namespace WPFHost 
{ 
    /// <summary> 
    /// Interaction logic for Page1.xaml 
    /// </summary> 
    public partial class Page1 : Page 
    { 
     private readonly Form1 mainForm = new Form1(); 

    public Page1() 
    { 
     InitializeComponent(); 

     //Create a Windows Forms Host to host a form 
     WindowsFormsHost windowsFormsHost = new WindowsFormsHost(); 

     stackPanel.Width = mainForm.Width; 
     stackPanel.Height = mainForm.Height; 
     windowsFormsHost.Width = mainForm.Width; 
     windowsFormsHost.Height = mainForm.Height; 

     mainForm.TopLevel = false; 

     windowsFormsHost.Child = mainForm; 

     stackPanel.Children.Add(windowsFormsHost); 
    } 

    private void Page_Loaded(object sender, RoutedEventArgs e) 
    { 
     if (ApplicationDeployment.IsNetworkDeployed) 
     { 
     string url = 
     AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData[ 
     0]; 
     string queryString = (new Uri(url)).Query; 
     this.textBox1.Text = queryString; 
     } 
    } 
    } 
} 
+0

我走它的URL有一些你想要的數據? – BradleyDotNET

+0

或者是ActivationData只是不是你所期望的?,在你的代碼中,文本框只會將其文本設置爲ActivationData [0] URL的查詢字符串 – BradleyDotNET

+0

@LordTakkera,URL是http:\\ website.us? DKT_ID =參數。我真的很努力得到這個,只有param從URL到我的WPF應用程序中的文本框。 – clerktech

回答

1

仍然不確定你在問什麼,所以我會盡量回答。

如果 「URL」 字段是字符串 「HTTP:\ website.us DKT_ID =參數」,就可以得到字符串 「DKT_ID =參數」 通過使用

url.Split('?')[1] 

創建Uri對象呢這樣做比分析您的字符串轉換成一個特殊的對象,如果你想[執行從URL的HTTP GET和使用數據,使用來自MSDN類似的例子以外的任何其他:如果你想

 WebRequest request = WebRequest.Create (
      "http:\\website.us?DKT_ID=param"); 
     // If required by the server, set the credentials. 
     request.Credentials = CredentialCache.DefaultCredentials; 
     // Get the response. 
     WebResponse response = request.GetResponse(); 
     // Get the stream containing content returned by the server. 
     Stream dataStream = response.GetResponseStream(); 
     // Open the stream using a StreamReader for easy access. 
     StreamReader reader = new StreamReader (dataStream); 
     // Read the content. 
     string responseFromServer = reader.ReadToEnd(); 
     // Display the content. 
     textBox1.Text = responseFromServer; 
     // Clean up the streams and the response. 
     reader.Close(); 
     response.Close(); 

更新 只是在「參數」字符串,也不會有在查詢字符串的其他任何參數,只需要使用

url.Split('=')[1] 

如果有多個參數,那麼你需要做的是這樣

Dictionary<String,String> params; 
string[] queryParams = url.Split('?')[1].Split('&'); 
foreach (string s in queryParams) 
{ 
    string[] queryParameter = s.Split('='); 
    params.Add(queryParameter[0], queryParameter[1]); 
} 

textBox1.Text = queryParams["DKT_ID"]; 
相關問題