簡體   English   中英

文本框(或列表框)中的簡單Web客戶端響應

[英]Simple webclient response in textbox (or listbox)

到目前為止,無論我嘗試過什么,都無法使其正常工作。

我想在Visual Studio 2015中制作一個Remote Camera Commander應用程序(WPF / C#),該應用程序由一個數字按鈕組成,單擊這些按鈕可以向網絡攝像機發出Web請求。 我不是程序員,所以我從頭開始。

現在已經在互聯網上搜索了許多天並測試了許多示例,但是當在我的代碼中插入一段示例代碼時,總是會出現新問題。

我舉了一個例子,希望可以解釋我的問題:

MainWindow.xaml:

<Window x:Class="RCC_1v1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:RCC_1v1"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Button x:Name="button" Content="GetCameraType_Button" Click="GetType" HorizontalAlignment="Left" Margin="44,108,0,0" VerticalAlignment="Top" Width="168" Height="86"/>
    <RichTextBox x:Name="response" HorizontalAlignment="Left" Height="78" VerticalAlignment="Top" Width="169" Margin="246,112,0,0" TextChanged="response_TextChanged">
        <FlowDocument>
            <Paragraph>
                <Run Text ="How to get the response from webclient into this textbox, instead of in the messagebox??"/>
            </Paragraph>
        </FlowDocument>
    </RichTextBox>

</Grid>

MainWindow.xaml.cs:

using System;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;

namespace RCC_1v1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private void GetType(object sender, RoutedEventArgs e)
        {
            WebClient wc = new WebClient();
            wc.Credentials = new NetworkCredential("stackO", "12345");
            wc.DownloadStringAsync(new Uri("http://eremote-cam1.eu.ngrok.io/axis-cgi/param.cgi?action=list&group=root.Brand"));
            wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
        }

        private void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            MessageBox.Show(e.Result.ToString());
        }

        private void response_TextChanged(object sender, TextChangedEventArgs e)
        {

        }
    }
}

您可以自己以臨時方式運行它。 嵌入的帳戶。 不知道我是否應該使用TextBox或其他任何對象,例如listview或。

預期的響應是文本字符串和單個數字。

在此處輸入圖片說明

任何提示,方向或幫助,不勝感激。

遵循您的一些建議,現在的代碼是:

enter code hereusing System;
using System.Net;
using System.Windows;

namespace RCC_1v2
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>

public partial class MainWindow : Window
{

    private void GetType(object sender, RoutedEventArgs e)

    {

        WebClient wc = new WebClient();
        wc.Credentials = new NetworkCredential("stackO", "54321");
        wc.DownloadStringAsync(new Uri("http://eremote-cam1.eu.ngrok.io/axis-cgi/param.cgi?action=list&group=root.Brand.ProdFullName"));
        wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
    }


    private void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {

        this.Dispatcher.BeginInvoke(new Action(() => {
            String run_text = e.Result.ToString();
            Run1.Text = run_text.Substring(24, 13);

        }));
    }

   }
 }

在此處輸入圖片說明

使用x:Name屬性將名稱“ Run1”命名為XAML中包含的運行。

替換您的代碼:MessageBox.Show(e.Result.ToString()); 與此:

this.Dispatcher.BeginInvoke(new Action(() => {
            this.Run1.Text = e.Result.ToString();
        }));

事件DownloadStringCompleted在后台線程中運行,因此,如果要更新UI,則必須使用Window類的Dispatcher屬性。

好的,我剛剛發現了發布答案的想法。 在海報的幫助下,我創建了下一個示例/解決方案:

  using System;
  using System.Net;
  using System.Windows;

 namespace RCC_1v2
 {
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>

public partial class MainWindow : Window
{

    private void GetType(object sender, RoutedEventArgs e)

    {

        WebClient wc = new WebClient();
        wc.Credentials = new NetworkCredential("xxx", "xxx");
        wc.DownloadStringAsync(new Uri("http://eremote-cam1.eu.ngrok.io/axis-cgi/param.cgi?action=list&group=root.Brand.ProdFullName"));
        wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
    }


    private void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {

        this.Dispatcher.BeginInvoke(new Action(() => {
            String run_text = e.Result.ToString();
            Run1.Text = run_text.Substring(24, 13);

        }));
    }

}
}

Mainwindow.xaml:

<Window x:Class="RCC_1v2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:RCC_1v2"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
  <Grid>
    <Button x:Name="button" Content="GetCameraType_Button" Click="GetType"     HorizontalAlignment="Left" Margin="44,108,0,0" VerticalAlignment="Top" Width="140" Height="86"/>

    <TextBox x:Name="Run1"  HorizontalAlignment="Left" Height="78" VerticalAlignment="Top" Width="285" Margin="206,113,0,0" >
    </TextBox>
</Grid>
</Window>

如您所見,我現在在texbox(而不是RichTextBox)中有響應

nb。 我被禁止張貼圖片:-(

在此處輸入圖片說明

謝謝!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM