private void DefineTimer_Tick(object sender, EventArgs e)
{
try
{
JArray jsonArray = new JArray();
JArray jsonArray2 = new JArray();
for (int i = 0; i < 6; i++) {
System.Threading.Thread.Sleep(10);
Random random = new Random();
int randomNumber = random.Next(10, 51);//随机数10-50
jsonArray.Add(randomNumber);
}
for (int i = 0; i < 7; i++)
{
System.Threading.Thread.Sleep(10);
Random random = new Random();
int randomNumber = random.Next(1000, 1501);//随机数10-50
jsonArray2.Add(randomNumber);
}
this.Dispatcher.BeginInvoke(new Action(() =>
{
if (webView.CoreWebView2 != null)
{
try
{
string jsonData = JsonConvert.SerializeObject(jsonArray);
Console.WriteLine(jsonData);
webView.CoreWebView2.PostWebMessageAsJson(jsonData);
}
catch (Exception ex)
{
Console.WriteLine("Error sending message: " + ex.Message);
}
}
if (webView2.CoreWebView2 != null)
{
try
{
string jsonData = JsonConvert.SerializeObject(jsonArray2);
//Console.WriteLine(jsonData);
webView2.CoreWebView2.PostWebMessageAsJson(jsonData);
}
catch (Exception ex)
{
Console.WriteLine("Error sending message: " + ex.Message);
}
}
}));
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
?
<Window x:Class="WPF_WebView2.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:WPF_WebView2"
xmlns:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"
mc:Ignorable="d"
WindowStartupLocation="CenterScreen"
Title="WPF_Echarts完整例子" Height="500" Width="1000" Loaded="Window_Loaded">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="150"/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="0">
<TextBox x:Name="inputUrl" VerticalContentAlignment="Center" KeyDown="inputUrl_KeyDown" />
</Grid>
<StackPanel Grid.Column="2" Orientation="Horizontal" HorizontalAlignment="Left" Margin="5 0 0 0">
<Button Content="GO" Width="60" Height="30" Click="Button_Click"/>
<Button Click="Button_Click_1" Content="发送信息" Margin="10 0 0 0" Width="60" />
</StackPanel>
</Grid>
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="1*" />
</Grid.ColumnDefinitions>
<Grid Grid.Column="0">
<wv2:WebView2 Name="webView" Source="https://www.baidu.com/" />
</Grid>
<Grid Grid.Column="1">
<wv2:WebView2 Name="webView2" Source="https://www.baidu.com/" />
</Grid>
</Grid>
</Grid>
</Window>