【问题标题】:C# pass parameter in querystring to WPF applicationC# 将查询字符串中的参数传递给 WPF 应用程序
【发布时间】:2014-04-07 22:37:09
【问题描述】:

我目前有一个 WPF 应用程序,它需要像在 ASP.NET 中一样接受来自 URL 的参数。我已经浏览了之前关于 SO 的帖子,但似乎没有什么比泥巴更清楚了。我已经将“发布”中的部分更改为接受参数。以下是我正在使用的代码:

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;
        }
    }
  }
}

【问题讨论】:

  • 我认为 URL 有一些你想要的数据?
  • 或者 ActivationData 不是您所期望的?使用您的代码,文本框只会将其文本设置为 ActivationData[0] URL 的查询字符串
  • @LordTakkera URL 是 http:\\website.us?DKT_ID=param。我真的很难从 URL 到我的 WPF 应用程序中的文本框获取这个也是唯一的参数。
  • @LordTakkera 我从未使用过 WPF,所以这对我来说是全新的。你能解释一下吗,因为这不像 ASP.NET,我只能调用查询字符串。 :-(
  • 没问题,我很确定我可以帮助你,我只需要知道你是否想要字符串“DKT_ID=param”,并且可以或看不到你提供的网址你的调试器;或者,如果您想要来自该 URL 的数据,例如在 HTTP GET 中以某种方式使用响应。

标签: c# wpf query-string querystringparameter


【解决方案1】:

还是不知道你在问什么,所以我会尽量回答。

如果“url”字段为字符串“http:\website.us?DKT_ID=param”,则可以通过使用获取字符串“DKT_ID=param”

url.Split('?')[1]

创建一个 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 ();

更新 如果您只想要“param”字符串,并且查询字符串中不会有任何其他参数,只需使用

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-16
    • 2014-11-02
    • 1970-01-01
    • 2021-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多