2014-03-05 1 views
0

현재 ASP.NET에서와 같이 URL에서 매개 변수를 받아 들일 필요가있는 WPF 응용 프로그램이 있습니다. 나는 이전 게시물을 쳐다 보았으나 진흙처럼 분명하지는 않습니다. 필자는 이미 "게시"섹션을 매개 변수의 승인을 위해 변경했습니다. 다음은 내가 사용하고있는 코드입니다.WPF 응용 프로그램에 쿼리 문자열의 C# 매개 변수를 전달하십시오.

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.Split('=')[1] 

를 사용하여 쿼리 문자열의 다른 매개 변수가되지 않습니다 URL에 원하는 데이터가 있습니까? – BradleyDotNET

+0

또는 ActivationData가 예상 한 것과 다릅니다. 코드가있는 텍스트 상자의 텍스트가 ActivationData [0] URL – BradleyDotNET

+0

@LordTakkera의 쿼리 문자열로 설정됩니다. URL은 http : \\ website.us입니까? DKT_ID = param. 난 정말 WPF 응용 프로그램에서 URL에서 텍스트 상자에이 유일한 매개 변수를 얻으려고 고군분투하고 있습니다. – clerktech

답변

1

아직 묻고있는 것에 대해 확신 할 수 없으므로 두 가지 모두에 답할 것입니다. "? : \ website.us DKT_ID = PARAM HTTP", 당신은 "DKT_ID = PARAM을"문자열을 얻을 수있는 열린 개체가 않습니다 만들기

url.Split('?')[1] 

를 사용하여 "URL"필드는 문자열 인 경우

당신이 원하는 경우

 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에서 HTTP GET 및 사용 데이터를 peform받는 MSDN의 예와 같이 뭔가를 사용하려는 경우, 특수 객체로 문자열을 구문 분석 이외의 아무것도하지 다만 은 "PARAM"문자열은 그냥 여러 매개 변수가있는 경우, 당신은

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"];