【发布时间】:2017-08-31 04:39:41
【问题描述】:
我不是一个聪明人。我花了几个小时阅读许多以前提出的不同问题并试图让这个东西发挥作用,但我仍然缺少 something 并且我不确定它是什么。当我意识到它是什么时,我可能会感到尴尬……但我从下面的第二个链接中得到的印象是,真的不应该以任何其他方式进行更新。
这些是我已经读过的东西:
How do I refresh visual control properties
Refreshing a WPF window on demand
我正在尝试将一个文本块单向绑定到一个字符串源,并让它在我的代码运行时自动更新......但它似乎永远不会更新。至于所有对象我正在使用...我开始学习 C# 的最初愿望是创建自己的程序,该程序可以将视频从互联网上的任何类型的输入流流式传输到我的手机...显然我还有很长的路要走。非常感谢您的帮助!
XAML
<Window x:Class="WpfApp1.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:c="clr-namespace:WpfApp1"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<c:dataHolder x:Key="source"/>
</Window.Resources>
<Window.DataContext>
<Binding Source="{StaticResource source}"/>
</Window.DataContext>
<Grid>
<TextBox x:Name="tb1" HorizontalAlignment="Left" Height="22"
Margin="45,35,0,0" TextWrapping="Wrap" Text="Enter IP"
VerticalAlignment="Top" Width="195"/>
<Button x:Name="Connect" Content="Connect"
HorizontalAlignment="Left" Margin="390,239,0,0" VerticalAlignment="Top"
Width="75" Click="Connect_Click"/>
<TextBlock x:Name="mblock" Text="{Binding Path=Message, Mode=OneWay,
UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Height="47"
Margin="45,105,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="195"/>
</Grid>
</Window>
代码隐藏
public partial class MainWindow : Window
{
private dataHolder dh;
public MainWindow()
{
dh = new dataHolder();
dh.Message = "Initialize";
InitializeComponent();
}
Binding myBinding = new Binding("myDataProperty");
private void Connect_Click(object sender, RoutedEventArgs e)
{
TcpListener server = null;
try
{
myBinding.Source = dh;
mblock.SetBinding(TextBlock.TextProperty, myBinding);
//Set TcpListener on port 13000.
Int32 port = 13000;
IPAddress localAddr = IPAddress.Parse("192.168.32.137");
server = new TcpListener(localAddr, port);
server.Start();
Byte[] bytes = new Byte[256];
String data = null;
//Enter the listening loop.
while (true)
{
dh.Message = "Waiting for a connection";
TcpClient client = server.AcceptTcpClient();
data = null;
NetworkStream stream = client.GetStream();
int i;
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
data = System.Text.Encoding.ASCII.GetString(bytes,0,
i);
dh.Message = "Received:" + data;
data = data.ToUpper();
data = data + "Sucka";
byte[] msg =
System.Text.Encoding.ASCII.GetBytes(data);
stream.Write(msg, 0, msg.Length);
dh.Message = "Sent:" + data;
}
client.Close();
}
}
catch (SocketException ex)
{
string nastyE;
nastyE = ex.Message;
dh.Message = "Socket Exception" + nastyE;
}
finally
{
server.Stop();
}
}
}
数据持有者
public partial class dataHolder : INotifyPropertyChanged
{
private string message;
public string Message
{
get
{
return message;
}
set
{
message = value;
NotifyPropertyChanged("Message");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
【问题讨论】:
-
“我不是个聪明人”尝试新事物和四处试验是明智之举
标签: c# wpf xaml data-binding