[英]C# pass parameter in querystring to WPF application
I currently have a WPF application that needs to accept a parameter from a URL like it would in ASP.NET. 我目前有一个WPF应用程序需要接受来自URL的参数,就像在ASP.NET中一样。 I have looked through previous post on SO but nothing it seems to be as clear as mud. 我已经浏览过上一篇关于SO的帖子,但似乎没有像泥一样清晰。 I have already changed the section in "Publish" to for the acceptance of parameters as well. 我已经将“发布”中的部分更改为接受参数。 The follow is the code I am utilizing: 以下是我正在使用的代码:
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;
}
}
}
}
Still not sure about what you are asking, so I'll try to answer both. 仍然不确定你在问什么,所以我会尽力回答这两个问题。
If the "url" field is the string "http:\\website.us?DKT_ID=param", you can get the string "DKT_ID=param" by using 如果“url”字段是字符串“http:\\ website.us?DKT_ID = param”,则可以使用字符串“DKT_ID = param”
url.Split('?')[1]
Creating a Uri object does not do anything other than parse your string into a special object, if you want to peform an HTTP Get and use data from that URL, use something like the example from MSDN : 除了将字符串解析为特殊对象之外,创建Uri对象不会执行任何操作,如果要执行HTTP Get并使用该URL中的数据,请使用类似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 ();
Update If you want just the "param" string, and there won't be ANY other parameters in the query string, just use 更新如果您只想要“param”字符串,并且查询字符串中不会有任何其他参数,请使用
url.Split('=')[1]
If there are multiple parameters, then you need to do something like 如果有多个参数,那么你需要做类似的事情
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"];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.